Public Group

# Tic Tac Toe check???

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

## Recommended Posts

hey everyone, so I was wondering if there was a better way to check to see if someone has won a game of tic tac toe(which im pretty sure there is) than actually writing down every single possibility like so:

public static boolean winCheck(char[][] grid){
if(grid[0][0] == 'x' && grid[0][1] == 'x' && grid[0][2] = 'x') return true;
if(grid[1][0] == 'x' && grid[1][1] == 'x' && grid[1][2] = 'x') return true;
if(grid[2][0] == 'x' && grid[2][1] == 'x' && grid[2][2] = 'x') return true;
// ......
// ......
// etc
}

would appreciate the help :)

##### Share on other sites
You can use a look-up table and a for loop:

/*The board and its indexes:  0 | 1 | 2 ---+---+---  3 | 4 | 5 ---+---+---  6 | 7 | 8*/const int winningSequences[8][3] = {	{ 0, 1, 2 }, // Top row	{ 3, 4, 5 }, // Middle row	{ 6, 7, 8 }, // Bottom row	{ 0, 3, 6 }, // First column	{ 1, 4, 7 }, // Second column	{ 2, 5, 8 }, // Third column	{ 0, 4, 8 }, // Top-left to bottom right	{ 2, 4, 6 }  // Top-right to bottom left};for each seq in winningSequence	check if board[seq[0]] == board[seq[1]] == board[seq[2]]	if they are the same, then we have a winner

As the loop progresses, it'll check the top row, the middle row, and etc for cells that are the same throughout those cells.

Hint:
You can reinterpret a 3x3 array into a sequential array to make the checking the board with just one integer (the index) instead of two integers (row, column):
char board[3][3];char* arr = &board[0][0];arr[4] = 'x'; // The middle squareassert( board[1][1] == 'x' ); // Also the middle square

##### Share on other sites
I posted code that uses a much faster check here.

I basically keep an 32-bit accumulator per player, which is actually 8 different 4-bit accumulators (one for each way in which you can get 3 in a row). When one of them gets to 3, the player won. To make the check a little faster, I initialize the accumulators to 1 instead of 0, so then I can check them all simultaneously like this:
if (player_sum[0]&0x44444444)

##### Share on other sites
And...what if you want a 100x100 field? Perhaps at the moment you might not think of expanding the game, but it's good to have stuff not hard coded. The winning sequences should be determined with an algorithm, I think. This way scaling the game won't have any problem, plus you won't miss out any sequences you *oops forgot* (not really the case, as you can easily check there must be 8 solutions).

##### Share on other sites
Thanx for the ideas guys, im still trying to wrap my head around the look-up table but i think i get the jist of it.
********************************************
char board[3][3];
char* arr = &board[0][0];

arr[4] = 'x'; // The middle square
assert( board[1][1] == 'x' ); // Also the middle square
*********************************************
i had no idea you could this this, it's going to make recieving input from a player so much easier and less complicated than "Enter the row you want the x in?", "now Enter the col you want the x in?" lol

##### Share on other sites
Quote:
 Original post by Reservoir Sainti had no idea you could this this, it's going to make recieving input from a player so much easier and less complicated than "Enter the row you want the x in?", "now Enter the col you want the x in?" lol

If this is the case, then change your board to be a 9-element array:

// char board[3][3];
char board[9];

##### Share on other sites
What would be better to have? a 2 dimensional array, or just an array? or does it just depend on what the person prefers? to me it just seems like its a bit easier to visualize a 2 dimensional array in terms of a tic tac toe game.

##### Share on other sites
Quote:
 Original post by Reservoir SaintWhat would be better to have? a 2 dimensional array, or just an array? or does it just depend on what the person prefers? to me it just seems like its a bit easier to visualize a 2 dimensional array in terms of a tic tac toe game.

In my experience programming board games, it's always better to use a one-dimensional array.

##### Share on other sites
Quote:
 Original post by DecriusAnd...what if you want a 100x100 field? Perhaps at the moment you might not think of expanding the game, but it's good to have stuff not hard coded.

We are talking about a specific game. There's no point in anticipating changes to the board size, because there is no natural extension of the rules to larger boards. If we were programming a game like go, you would have a point.

Premature generalization is a particular case of premature optimization, and I strongly discourage it.

##### Share on other sites
thanks again, and I'll try and give it a go both ways!(using an array and a 2 dimensional array that is)

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633724
• Total Posts
3013556
×