TicTacToe Game State

Started by
4 comments, last by J-Ral 19 years, 2 months ago
Hello, I have seen the threads about game states and thought that was a bit beyond both myself and the scope of TicTacToe. I have just been thinking about how I am going to represent the state of the TicTacToe board throughout the game. This state will passed to the MiniMaxAB algorithm as a means of determining the computers next best move. I have been considering two options: 1. A 3x3 Array - threes rows of three columns just like to board. This way, it will be easy to see in the code what is going on e.g. board[1][2] will obviously represent the location in the first row, second column. 2. A 9 element array - where index 0 = pos(1,1), 1 = pos(1,2),..., 8 = pos(3,3) This approach just seemed like an alternative that should be considered. What do people think? Am I going about this the wrong way? Any and all comments will be most appreciated! Thanks, J-Ral!
Trace back the elements...
Advertisement
I'd go with the 3x3 array. Both methods would work just fine, the 3x3 just seems more intuitative
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
When I did TicTacToe I went with the 9 element array, simply because it is easier to determine winning a winning scenario that way.
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
The 9-element array is typically a bit nicer for UI, since you don't have to calculate row and column values to locate the player's move (or worse, ask the user for two numbers in order to make a move). However, it's not necessarily as intuitive.

Keep in mind that you also need to think about how to store the state of each square. If you are going to be outputting stuff to the console, my strong recommendation is to have each tile state represented by a single character which is the one that you will output on display: 'O' or 'X' for occupied squares, and values '1' through '9' for vacant ones. (Try to frame all your tests for square state in terms of presence/absence of 'O' or 'X', rather than checking for the digit values.)
I like the 9 element array myself, as you can pass less data around. For example, to represent a diagonal, you can pass (start at element 2, increase 2 elements each time) instead of 2, 0, -1, 1, (start at 2,0, move -1,1 each time). It's a little less obvious what the heck is happening, but it's simpler (in terms of code at least) in my opinion.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Hey people,

Thanks for the input!

Just so you know, I am using Visual Basic.NET to program TicTacToe just to begin with so that I can concentrate on the game code as opposed to taking ages to perfect the UI.

So input from the user will be handled by label click events etc. which will in turn give the appropriate square (represented by place in either a 3x3 array or 9 element array) a value of 1 or 2.

The value's for each square are 0 (meaning empty), 1 (meaning occupied by player1) and 2 (occupied by player 2).

The game state evaluation function will simply test the values of squares to check for certain conditions which define the 'goodness' of the game state (from the PC's perspective that is).

With that in mind, I think one of the biggest determining factors in deciding whether to use a 3x3 or 9 element array will be the implications that surface wilst testing for winning conditions.

I am confused as to whether checking for winning conditions would be easier, harder or the same for each representation of game state.

What do people think?

Thanks a lot for all your input!

Jral
Trace back the elements...

This topic is closed to new replies.

Advertisement