Public Group

Maybe a simple question, maybe not...

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

Recommended Posts

Ok, I am working on a TicTacToe game and being a perfectionist I am running into a wall in one certain spot… That is how do I determine if an open square on the board is a winning square in an elegant manner? For example:
____________
| X | X |  |
------------
In that case the far right square would be a possible winner. Now, I know I could set up some stupid long if/then statement, but I would prefer not to if there is a more elegant way of doing it. Anyone have any ideas? EDIT: I won't be able to reply untill I get some sleep. I have done over 1000 lines of tested and debugged code in 3 hours, and I have been up for almost 20 hours. Take care everyone.

Share on other sites

If you internally have nine boards, arranged in a grid like:

B1 B2 B3
B4 B5 B6
B7 B8 B9

Play on the middle board, but any piece placed is replicated onto the other eight boards as well so at any point, all nine boards are identical.

Now, if you examine any cell on the middle board, you can check around it as if it is the middle piece by examining the cells in the other boards to find out how many Os and Xs are on a given line.

So now examining the cells around any given cell becomes as simple as examining the cells around the middle cell.

[EDIT] Thinking about it, there is no need to replicate the board. If you had say:

char GetCell(int X,int Y)
{
if(X<0) X+=3;
if(X>2) X-=3;
if(Y<0) Y+=3;
if(Y>2) Y-=3;

return Board[X][Y];
}

Then you'd get the same effect.

Share on other sites
I'm currently working on a TicTacToe game:

Although this is in 3D, you could use the same strategie.
If this would be your grid:

1 2 3
4 5 6
7 8 9

// Where the numbers are the PlaceIndexes

Then there are 8 different rows:
PlaceIndex rowPlaces [8][3] ={
{1 2 3} //Horizontal rows
{4 5 6}
{7 8 9}
{1 4 7} //Vertical rows
{2 5 8}
{3 6 9}
{1 5 9} //Diagonal rows
{3 5 7}}

You save pions in an array field, and EMPTY = 0 MAN = 1 PC = 4

Player field[9] = {
EMPTY, EMPTY,... ,EMPTY} //initially the field is empty

If I want to check what's in a row, I use the sum off all the pions in that row.
sum(RowIndex) = field[rowPlaces[rowIndex][0]+field[rowPlaces[rowIndex][1]+field[rowPlaces[rowIndex][2];

so if here is a pion of MAN at 1
there is a pion of PC at 2
3 was EMPTY
then sum(Row1) = 1+4+0 = 5
So sum(RowIndex) tells us what's in the row

// can PC win?for (every row)    if (sum(Row)=2*PC)    {        PC wins if he puts his pion at the empty place of row        return;    }

[Edited by - Kwak on August 12, 2006 3:35:13 PM]

Share on other sites
I would fill the square and then test if the result is a win.
    typedef int Board[9];        bool IsWinningMove( Board const & board, int location, int player )    {        Board t;        memcpy( &t, board, sizeof( Board ) );        t[location] = player;        if ( t[0] == t[1] && t[1] == t[2] ||             t[3] == t[4] && t[4] == t[5] ||             t[6] == t[7] && t[7] == t[8] ||             t[0] == t[3] && t[3] == t[6] ||             t[1] == t[4] && t[4] == t[7] ||             t[2] == t[5] && t[5] == t[8] ||             t[0] == t[4] && t[4] == t[8] ||             t[2] == t[4] && t[4] == t[6] )        {            return true;        }        else        {            return false;        }    }
Not high on the elegance scale, but it pretty simple and straightforward.

Share on other sites
I'm going to have to agree with John on this one. I think in this situation it's best to KISS (Keep It Simple Stupid ... not saying you're stupid, that's just the acronym our machine architecture prof used to give us). If you do find that this is a bottleneck in your code, _then_ worry about going back and optimizing/making a more elegant solution.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• Forum Statistics

• Total Topics
633686
• Total Posts
3013323
×