Saturday, January 9, 2010

States

(As a side note: Some of these tutorials may seem very basic to some, and that is intentional. All of these blog posts on how I am constructing the game are intended for those that have never programmed, or are very new to programming. So be patient if some of the initial topics, or even later topics, are things you already know.)

What I assume you understand before you read this post:
Object-Oriented Programming
How a barbershop works
How to play Tic-Tac-Toe

Most games (and computer programs in general) can be considered “state machines”. As the name implies, a state machine is a machine, or processor, that handles states. Think of a barbershop; there are a few different “states” you could be in while there. You could be:
  • Waiting for the next available barber.
  • Getting your hair cut
  • Paying at the register

All of these states could be represented in a computer program if you wanted to simulate a hair salon.* (For example, each state could be represented by a class if you are working with an object-oriented program.)

A game like “Tic Tac Toe” could also be put into a state machine. Some states might include player one taking a turn, player two taking a turn, a winning state, and a tie game state (“cats game”).

This game is no different: There are different states represented in the program as shown in the flowchart below.



Each state is represented by a box. I personally have represented a state as a class in the game. I have a game object that creates an instance of each of these states and will transition between states when necessary. For instance, there is an “exit” event that is fired when a person wins (“victory”) and a “loss” event that happens when the player loses (“fail”). When those happen, a state transition happens:

  • In the “victory” case, I display the victory screen, and then switch the screen back to the level selection
  • In the “fail” case, I display the retry screen, which changes the state based on whether the player would like to retry or not.

The cycle happens again and again, until the player decides to exit the game.

* Thanks to my internet programming class for inspiration for this one! Go Dr. Snell! :)

0 comments:

Post a Comment