I'll help you,
start by defining the entire state of the game.
We'll call thisint board[9] = { 0 };
A 3x3 space in two dimensions.
0 - Unknown state
1 - Player 1 claims
2 - Player 2 claims
Next, you want to allow input.
The simplest inputs are [position, player]
There are two players.
void set(int pos, int player) { board[pos] = player; }
Where player is [0-2] and pos [0-8]
Check for a cat's game!
bool catsGame( )
{ // return false if any tile is unset
for(int i=0;i<9;++i) if( ! board[i] ) return false;
return true; // To be fair, you could detect a cat's game many moves ahead
} // I didn't take that higher complexity approach here.
Finally, you want to detect victory conditions.
int victory( )
{
// check rows [0, 3, 6] are left sides
for( int i=0;i<7;i+=3; )
{
if( !board[i] ) continue; // Unset.
if( board[i] == board[i+1] && board[i] == board[i+2] ) return board[i];
}
// next, check columns [0, 1, 2]
for( int i=0;i<2;++i )
{
if( !board[i] ) continue;
if( board[i] == board[i+3] && board[i] == board[i+6] ) return board[i];
}
// finally, evaluate diagonals [4] is center
if( board[4] )
{
if( board[0] == board[4] && board[0] == board[8] ) return board[4];
if( board[2] == board[4] && board[2] == board[6] ) return board[4];
}
// No winner detected.
return 0;
}
You can …