Jump to content
• Advertisement

Public Group

# TicTacToe Game State

This topic is 4910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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!

#### Share this post

##### Share on other sites
Advertisement
I'd go with the 3x3 array. Both methods would work just fine, the 3x3 just seems more intuitative

#### Share this post

##### Share on other sites
When I did TicTacToe I went with the 9 element array, simply because it is easier to determine winning a winning scenario that way.

#### Share this post

##### Share on other sites
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.)

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5
• Advertisement

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002140
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!