Public Group

# TicTacToe AI question

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

## Recommended Posts

I'm making a tictactoe game and am having a little trouble with my AI. I have 3 levels of AI, EASY, MEDIUM and HARD. Their algorithms are as follows. EASY - random move. MEDIUM 1.If can win, take that move. 2.If opponent can win, block. 3.Random move. HARD 1. If can win, take that move. 2. If opponent can win, block. 3. If it is your first move If you are X - pick a corner. If you are O If X took a corner - take the middle. else take a corner. 4. Random move. I made a program to run 1000 games of AI vs AI to see the results. EASY and MEDIUM seem to work correctly with MEDIUM beating EASY about 90% of the time. The problem is that MEDIUM beats HARD consistantly also. IF MEDIUM is X it wins about 45% and ties around 45%. If MEDIUM is O it wins about 25% and ties around 67%. I don't know if I implemented something incorrectly, or is my algorithm for HARD incorrect. Any thoughts on this.

##### Share on other sites
My tic-tac-toe AI takes the center first and will always play to at least stalemate. Maybe try this.

##### Share on other sites
I used the win_if_you_can and block_if_you_have_to idea too.
But I found to make my AI unbeatable I had to map out the moves
for the first 4 turns(assuming the AI could go first or second).
After that win_if_you_can and block_if_you_have_to seems to make
all possible moves automatic.

Turn 1: Random Empty Corner
Turn 2: If turn 1 was to the center then pick a random empty corner
Else go to the center

Turns 3 & 4 are pretty complicated, several if-elses required.

I'm not suprised that your AI wins more often as X than as O.
If moves are random then I would assume that the player to go first
would win more often for no other reason than because it has the
advantage of going first. I bet if you pitted your MEDIUM AI versus
itself you would find a similar result.

Your AI for HARD isn't incorrect its just incomplete. You need to map out
turns 3 & 4 before win_if_you_can and block_if_you_have_to can take care
of all possible situations.

##### Share on other sites
Well my goal is not to make HARD unbeatable, just play a pretty good game. The thing that really puzzles me is that MEDIUM beats HARD, when their AI is almost the same. HARD just plays its first turn much better. It seems to me that HARD is "smarter", but maybe that is not the case.

##### Share on other sites
Quote:
 HARD 1. If can win, take that move.2. If opponent can win, block.3. If it is your first moveIf you are X - pick a corner.If you are OIf X took a corner - take the middle.else take a corner.4. Random move.

Quote:
 Well my goal is not to make HARD unbeatable, just play a pretty good game. The thing that really puzzles me is that MEDIUM beats HARD, when their AI is almost the same. HARD just plays its first turn much better. It seems to me that HARD is "smarter", but maybe that is not the case.

This is how I made my game unbeatable:

this is O playing...

1. if you can win WIN!
2. if X can win BLOCK!
3. take a corner if X took a corner though take the middle
4. if X took a corner and you took the middle take a side of the corner X took
5. take the middle
6. the side moves that are left...
no random needed...and it's unbeatable!

If you wanted to make it beatable just take a step out...like step 3 or 4. :)

##### Share on other sites
The last is correct because the first player to be unbeatable should always grab the center, but the second, in order to not fall into the two way trap, needs to grab certain squares accordingly. Just think about it. Tic-Tac-Toe is pretty simple for THREE levels of difficulty, simple because it is only 9 squares and 4-5 turns each. Maybe the 1st should always be random, and the second should be unbeatable. Or maybe be random of just how unbeatable it sould be each game. I can't think of a way to make a hard AI that IS beatable, because of the simplicity of the game.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015231
×