Tic Tac Toe Perfect Play Against the Minimax AI
The tic tac toe engine runs minimax to the end of the game on hard, with no randomness, so hard never loses. The menu labels it Unbeatable and the code backs the label. What perfect play buys you is a guaranteed draw there and forced wins on the two lower settings, and both outcomes follow from the
By WebRecast Published
The tic tac toe engine runs minimax to the end of the game on hard, with no randomness, so hard never loses. The menu labels it Unbeatable and the code backs the label. What perfect play buys you is a guaranteed draw there and forced wins on the two lower settings, and both outcomes follow from the eight win patterns the engine checks.
Count the lines through each square
The engine checks three rows, three columns, and two diagonals. The center cell belongs to four of those eight lines, the middle row, the middle column, and both diagonals. Each corner belongs to three. Each edge belongs to two.
That count sets the value of every square. The center is the strongest cell on the board, corners beat edges, and the same ranking sits inside the AI's own search, because minimax resolves every line through the center before it resolves lines through an edge.
Your move order, every game
Apply this priority list on each turn. The order is unconditional and covers every position you can reach with sensible play.
1. Complete any line where you already hold two squares.
2. Block any line where the AI holds two squares and the third is empty.
3. Take the center if it is free.
4. Take a corner that puts your marks on two separate lines at once.
5. Take any remaining corner.
6. Take an edge.
Open every game with a corner, cell 0 in engine numbering. The corner opening wins more often against imperfect opponents because it creates three potential lines instead of the two an edge offers.
The forced win against a weak reply
One sequence wins outright when the AI answers your corner with an edge. Numpad users can follow along with keys, where 7 8 9 form the top row and 1 2 3 the bottom.
1. Play a corner. The AI replies somewhere.
2. If the reply was an edge rather than the center, take the center. You now threaten the long diagonal.
3. The AI must block the far corner of that diagonal.
4. Play the corner adjacent to your opening corner, on the side away from the AI's edge piece. You now hold two lines with two marks each, the row through your opening corner and the diagonal through the center.
5. The AI blocks one line. You complete the other.
If the AI instead answers your opening corner with the center, the forced win is gone. Take the opposite corner, keep applying the priority list, and wait for a medium or easy error. Against hard, this line is a draw and nothing you play changes that.
Where the difficulty settings actually differ
The only difference between levels is the chance the AI ignores its search and plays a random empty cell. Easy throws 40 percent of moves, medium throws 15 percent, hard never throws. The AI moves at most four times per game, because you take five of the nine cells.
Run the probability. On medium, the chance of at least one random AI move in a full game is 1 minus 0.85 to the fourth power, which is 48 percent. On easy it is 1 minus 0.6 to the fourth power, which is 87 percent. Nearly half of your medium games contain an AI mistake, and the forced win sequence above converts almost every mistake that occurs in the first two replies.
The AI also waits 0.3 seconds before moving and starts the next round 2.0 seconds after a result. Use that fixed rhythm. Plan your reply during the pause instead of after it.
Track your results by level
The engine stores wins, losses, and draws per difficulty in local storage and shows them on the menu. Your hard record is the honest one. If it shows no losses, your defense is already perfect. If it shows losses, the cause is a missed block at priority two, because hard never fails to punish exactly that error.
Tic tac toe checklist
- [ ] Opening move is a corner, every game.
- [ ] Priority list applied top to bottom on every turn.
- [ ] After corner and center, checking for the double-threat corner.
- [ ] Block checked before building when the AI holds two in a line.
- [ ] Against hard, accepting the draw as the ceiling.
- [ ] Against medium, expecting a mistake in roughly half of full games.
- [ ] Next move planned during the 0.3 second AI delay.
The honest downside. Perfect play on both sides makes tic tac toe a draw, so hard mode offers no winning strategy and grinding it only proves your defense. The 2.0 second auto restart also lets a loss roll into a new round before you review it, so pause on the result if you want the lesson. Open [tic tac toe](/en/tic-tac-toe), play ten games on medium with the priority list, and note your win count. Ten games at the expected 48 percent error rate should give you around five wins, and a much lower number means the list is being skipped at the blocking step. Send your tallies if yours disagree.
Try It Yourself
Related guides
- Block Stacker Strategy from the Source Code: Scoring, SRS, and the Speed Curve
- Sudoku Solving Techniques: The Logic Ladder Our Generator Has to Guarantee
- Word Guess Strategy From a 200-Word Bank: Open With CEDAR
More articles in Games