Simpler Way To Do This?

Started by
6 comments, last by BringBackFuturama 17 years, 7 months ago
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?
Originality is dead.
Advertisement
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).
Quote:Original post by yaroslavd
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).


Hmm..it either had something to do with a loop or a class. How would that work with the diagonals though?
Originality is dead.
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.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
Quote:Original post by _goat
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.


Hmm..that's very interesting. Don't really understand that..but I'll take your word for it.
Originality is dead.
Quote:Original post by BringBackFuturama
Hmm..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.
"Sweet, peaceful eyelash spiders! Live in love by the ocean of my eyes!" - Jennifer Diane Reitz
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;}
Thanks for all the help guys. Now back on to coding this thing together.
Originality is dead.

This topic is closed to new replies.

Advertisement