• Advertisement

Archived

This topic is now archived and is closed to further replies.

Tic Tac Toe Game -- Programming Practices

This topic is 5939 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

Hey all! I've got my Tic Tac Toe game working (thanks to the people who helped me out with the array mishap crap in a previous post). Anyways! This is what my checking for a winner function looks like, and I'm just wondering if this is the most effecient way of doing this (thus why its not in the game programming forum) or if theres a more effecient way that someone can think of, so I can change my thought process:
// Do we have a winner?
int CheckWinnerBoard(int iBoard[][3], int iPlayerTurn)
{
	// Horizontal
	if((iBoard[0][0] == iPlayerTurn) && (iBoard[0][1] == iPlayerTurn) && (iBoard[0][2] == iPlayerTurn))
		return iPlayerTurn;
	if((iBoard[1][0] == iPlayerTurn) && (iBoard[1][1] == iPlayerTurn) && (iBoard[1][2] == iPlayerTurn))
		return iPlayerTurn;
	if((iBoard[2][0] == iPlayerTurn) && (iBoard[2][1] == iPlayerTurn) && (iBoard[2][2] == iPlayerTurn))
		return iPlayerTurn;

	// Vertical
	if((iBoard[0][0] == iPlayerTurn) && (iBoard[1][0] == iPlayerTurn) && (iBoard[2][0] == iPlayerTurn))
		return iPlayerTurn;
	if((iBoard[0][1] == iPlayerTurn) && (iBoard[1][1] == iPlayerTurn) && (iBoard[2][1] == iPlayerTurn))
		return iPlayerTurn;
	if((iBoard[0][2] == iPlayerTurn) && (iBoard[1][2] == iPlayerTurn) && (iBoard[2][2] == iPlayerTurn))
		return iPlayerTurn;

	// Diagonal
	if((iBoard[0][0] == iPlayerTurn) && (iBoard[1][1] == iPlayerTurn) && (iBoard[2][2] == iPlayerTurn))
		return iPlayerTurn;
	if((iBoard[0][2] == iPlayerTurn) && (iBoard[1][1] == iPlayerTurn) && (iBoard[2][0] == iPlayerTurn))
		return iPlayerTurn;

	// No winner yet apparently.
	return 0;
}
 

"Where genius ends, madness begins."
Estauns Edited by - Estauns on November 21, 2001 12:02:43 AM

Share this post


Link to post
Share on other sites
Advertisement
        

int checkWinner(int B[3][3], int turn)
{
if (turn > 4) {
for (t = 0; t < 3; t++) {
if (B[t][0])
if (B[t][0] == B[t][1] && B[t][0] == B[t][2])
return B[t][0];
if (B[0][t])
if (B[0][t] == B[1][t] && B[0][t] == B[2][t])
return B[0][t];
}
if (B[0][0])
if (B[0][0] == B[1][1] && B[0][0] == B[2][2])
return B[0][0];
if (B[0][2])
if (B[0][2] == B[1][1] && B[0][2] == B[2][0])
return B[0][2];
}

return 0;
}




___________________________________



Edited by - bishop_pass on November 21, 2001 12:49:00 AM

Share this post


Link to post
Share on other sites
Good job.
A quick way to shorten it.

You can make a "for loop" for each horizontal and vertical checking.

  
// Horizontal

for (int iCnt = 0 ; iCnt < 3 ; iCnt++)
if((iBoard[iCnt][0] == iPlayerTurn) && (iBoard[iCnt][1] == iPlayerTurn) && (iBoard[iCnt][2] == iPlayerTurn))
return iPlayerTurn;

Share this post


Link to post
Share on other sites
If I were you I''d make a recursive function that checks for the linked chains of X''s or O''s, that way you could have any size board you want and your check for win function will still work.

The way I did it when I was programming my hex game was I had a HasBlackWon() function and a HasWhiteWon() function, and then I had some helper functions, namely DoesBlackChainExist() and DoesWhiteChainExist(), and I would check whether a chain existed iteratively from the base of one side and see if it reached the other side. I also used a second array for keeping track of which squares I had already been to in my chain searching.

So for tic-tac-toe, something like the following might work:

  
// checked_squares[3][3] is a global variable, as is board[3][3]

bool HasXWon() {
MarkAllSquaresAsNotChecked();
for(int row = 0; row < MAX_ROWS; row++) {
if(DoesXChainExist(row, 0))
return true;
}
return false;
}


The DoesXChainExist() function marks each square that it checks so you know that you don''t keep checking the same squares over and over and go around in cirlces.

The DoesXChainExist() function calls itself recursively from each possible square that a connection could be made from, and if the column value equals MAX_COL (the number of columns) then you''ve reached the other side and a chain exists, and you''ve found a win.

I''ll give the DoesXChainExist() function a try, but I might leave something important out. It''s 4AM after all

  
bool DoesXChainExist(int row, int col) {
checked_squares[row][col] = CHECKED;
if(col == MAX_COL)
return true;
if(DoesXChainExist(row+1, col) || DoesXChainExist(row-1,col) || DoesXChainExist(row,col+1) || DoesXChainExist(row,col-1))
return true;
return false;
}


I think that''s right, but no guarentees. You''d have to add in some edge detection to make sure that col-1 isn''t off the edge of the board (and out of bounds on your array).

Anyway, hope this helps and hope it makes some sense.

If you''re looking for the fastest way to detect a win on a board that you know the size of, this probably isn''t the fastest way, but this way will allow you to let the user choose what size of board he wants to play on, and you''d just write your functions with the proper MAX_ROWS and MAX_COLS values.

Russell

Share this post


Link to post
Share on other sites

  • Advertisement