Sign in to follow this  

Simpler Way To Do This?

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

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 this post


Link to post
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[i][i]).

Share this post


Link to post
Share on other sites
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[i][i]).


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites

This topic is 4114 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this