Public Group

# Simpler Way To Do This?

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

## Recommended Posts

Okay, given the tetris thing was a bit complex I decided to try tic-tac-toe. Anyway, I seem to have encountered a similar issue I did with my last two attempts at a game: something being more complex than it should be. Now this isn't that complex, but I have set up a function called Won to check if a player has won the game on thier move. It looks like this:
bool Won()
{
int x = 1;
if(player == two) ++x;
if(boardarray[0][0] == x && boardarray[1][1] == x && boardarray[2][2] == x) return true;
if(boardarray[0][2] == x && boardarray[1][1] == x && boardarray[2][0] == x) return true;
if(boardarray[0][0] == x && boardarray[0][1] == x && boardarray[0][2] == x) return true;
if(boardarray[1][1] == x && boardarray[1][1] == x && boardarray[1][1] == x) return true;
if(boardarray[2][0] == x && boardarray[2][1] == x && boardarray[2][2] == x) return true;
if(boardarray[0][0] == x && boardarray[1][0] == x && boardarray[2][0] == x) return true;
if(boardarray[0][1] == x && boardarray[1][1] == x && boardarray[2][1] == x) return true;
if(boardarray[0][2] == x && boardarray[1][2] == x && boardarray[2][2] == x) return true;
return false;
}


This works out great in this case, but in a more complex problem, like say if it was a 5 X 5 board, it would be an exponentially larger function. Is there a different way that I should approac this? Rather than individually checking each possible way of winning is there some kind of formula that I should use?

##### Share on other sites
Yes. First, you want to check if there is a row in which all the x's/o's line up. So a simple for loop will do. Ditto for the columns. And then the diagonals (aka all boardarray).

##### Share on other sites
Quote:
 Original post by yaroslavdYes. First, you want to check if there is a row in which all the x's/o's line up. So a simple for loop will do. Ditto for the columns. And then the diagonals (aka all boardarray).

Hmm..it either had something to do with a loop or a class. How would that work with the diagonals though?

##### Share on other sites
to be honest, that's probably not too bad. I mean, it's straight to the point, and with Tic Tac Toe, there's not going to be any expansion.

However, I recall one method, where you give each square a number value, so the entire thing is a magic square (value of 15, I think). Then you add up each column and row for each x, and if any of them equal 15, you have a winner. Diagonals are special cases. That one's scalable, although from memory, Tic Tac Toe doesn't really work for 4x4 or 5x5 grids.

... I just checked a few things, and I think the reason you do this is for more advanced AI.

##### Share on other sites
Quote:
 Original post by _goatto be honest, that's probably not too bad. I mean, it's straight to the point, and with Tic Tac Toe, there's not going to be any expansion.However, I recall one method, where you give each square a number value, so the entire thing is a magic square (value of 15, I think). Then you add up each column and row for each x, and if any of them equal 15, you have a winner. Diagonals are special cases. That one's scalable, although from memory, Tic Tac Toe doesn't really work for 4x4 or 5x5 grids.... I just checked a few things, and I think the reason you do this is for more advanced AI.

Hmm..that's very interesting. Don't really understand that..but I'll take your word for it.

##### Share on other sites
Quote:
 Original post by BringBackFuturamaHmm..it either had something to do with a loop or a class. How would that work with the diagonals though?

Try coding it for the columns and the rows. That should give you a clue as to what you'll need to do for the diagonals. And I imagine you'll learn more from figuring it out yourself than you would from having someone just give you the code.

_goat's solution is interesting, but unnecessarily complicated for what you want to do. You're already on the right track.

##### Share on other sites
Since I'm bored:
bool checkWon(int piece){	int r, c;	bool won;	// check rows	for (r = 0; r < 3; r++) {		won = true;				for (c = 0; c < 3; c++)			if (board[r][c] != piece) won = false;		if (won) return true;			}	// check columns	for (c = 0; c < 3; c++) {		won = true;				for (r = 0; r < 3; r++)			if (board[r][c] != piece) won = false;		if (won) return true;			}	// check diagonals	won = true;	for (r = 0, c = 0; r < 3; r++, c++) {		if (board[r][c] != piece) won = false;	}	if (won) return true;	won = true;	for (r = 0, c = 2; r < 3; r++, c--) {		if (board[r][c] != piece) won = false;	}	if (won) return true;	return false;}

##### Share on other sites
Thanks for all the help guys. Now back on to coding this thing together.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

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

• Total Topics
632929
• Total Posts
3009280
• ### Who's Online (See full list)

There are no registered users currently online

×