# tic tac toe game

## Recommended Posts

The game works fine and everything, just trying to make it that the player can never win. Anyways, I am trying to make the code make the computer go to either position 1, 3, 5 or 7 if the human has opposite corners. This is the setup of the board: 0 1 2 3 4 5 6 7 8 The code works fine and will take whichever spot is available in that order. The only thing is the next time it is the computers turn it will take the next available spot out of the 1,3,5 or 7. How do I change the code so this will only come into play once and not over and over again on every turn the computer gets?
	const int BEST_BLOCKMOVES[] = {1, 3, 5, 7};
if (human == board[0] && human == board[8] || human == board[2] && human == board[6])
{
for (int i = 0; i < board.size(); i++)
{
int move = BEST_BLOCKMOVES[i];
if (isLegal(move, board))
{
cout << move << endl;
return move;
}
}
}


##### Share on other sites
Add a boolien flag so that it happends only once. Flip the flag the first time the if statement runs, so so it woun't run again until you restart (and reset the flag)

##### Share on other sites
Uh, you're choosing that move at the wrong time: the 'if' checks if the human has two diagonally opposite corners. You wouldn't want to play on the side in that case, but in the middle (unless it's already occupied).

I think you're making the overall strategy too complex. What do you have so far, in English?

##### Share on other sites
The way I have it set up is the number one move for the AI is too take the middle square provided the human doesn't take it. Then the outside corners are the next options for the AI and then finally the outside middle squares. The move order would look like this:

232
313
232

I set up the if statement like that because if the player has opposite corners and the AI plays on another corner the human can win every time.
Ex:
X O(this would be AI's 2nd move)
O
X
If the human places and X in the bottom corner, they could win everytime. This is why I have it set up if the human has a piece of opposite corners to play in one of the outside middles rather than a corner. I hope this makes sense.

##### Share on other sites
Not really. Show the whole algorithm, in English. E.g.

if board is empty:  return: play middle.for each pair of adjacent corners:  if both corners are marked by opponent:    # etc.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978709

• 21
• 14
• 12
• 10
• 12