Advertisement

Tic-Tac-Toe

Started by March 01, 2003 12:42 AM
2 comments, last by GravtyKlz 21 years, 8 months ago
I just made a Tic-Tac-Toe game but theres one part I cant figure out. Im trying to think of a way to let me program be able to tell whether or not a line has been made. I''ve been thinking for about an hour and nothing is really clicking. The way I have it settup is this...I have 9 openings for either an X or an O. For each of those openings I keep track of the player that put their X or O there and I have a variable that says if its even a player that has put one there.

struct move{
bool free;  //see if anyone has moved there yet
int player; // 0 for player 1 and 1 for player 2
            // 0 = X, 1 = O
};

struct move mvs[9];
 
once a move is made I store who put their piece there and I mark that it is no longer free(so the other player cant move there). Any ideas?
You have your grid like this:


[ ][ ][ ]
[ ][ ][ ]
[ ][ ][ ]


A winning line is 3 uniform charactors in your grid that are next to each other in the field. For example:

TTTGrid[0][0] && TTTGrid[0][1] && TTTGrid[0][2]

^ That is a won game if you find either a X or O in all three of those spots. Just loop through each winnable spot in your grid and check for 3 uniform charactors. Then find out whihc charactor is t that was found to be in a line and award the player a point.


Advertisement
I''m wondering what the meaning is with having that boolean telling whether the space is empty or not?

You might aswell tell that
0 = empty
1 = player 1
2 = player 2

That way there is no need to first check if it''s empty or not, and then who''s taken it.
If you add graphics to it, draw the whole matrix, then you could just draw it based on the values in the matrix
0 = empty block
1 = O- picture
2 = X- picture

Just a hint, nothing says that you have to do it like this though

  	for(int i = 1; i<8; i+=3)	{		if(*(player1+i) && *(player1+i+1) && *(player1+i+2)){		blit(PlayerWhite, buffer,0,0,0,0,222,230);			game_won = true;		}	}	///check downwards///	for(int j = 7; j<10; j++)	{		if(*(player1+j) && *(player1+(j-3)) && *(player1+(j-6))){		blit(PlayerWhite, buffer,0,0,0,0,222,230);			game_won = true;		}	}		if(	*(player1+7) && *(player1+5) && *(player1+3)||		*(player1+9) && *(player1+5) && *(player1+1)){		blit(PlayerWhite, buffer,0,0,0,0,222,230);			game_won = true;	}   


I made this for an tic tac toe game in allegro,
basicly I have the board setup like the numpad

789
456
123

then I have a for loop that goes first from left to right, then one that checks downwards, then I check the 753 and the 951 to se if player1 has taken those.

[edited by - kappa the imp on March 1, 2003 5:52:52 AM]

This topic is closed to new replies.

Advertisement