What algorithm for a tic-tac-toe game can I use to determine the best move for the AI?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Tic-tac-toe is small enough that an AI can reason about the full game tree instead of relying on guesses. That makes it a classic example for learning adversarial search, because the "best move" is not just a locally good move, but the move that leads to the best outcome after the opponent responds optimally.
Why Minimax Fits Tic-Tac-Toe
The standard algorithm for tic-tac-toe is minimax. It works well because tic-tac-toe is:
- turn-based
- deterministic
- zero-sum
- fully observable
Those properties let the AI evaluate each legal move, assume the opponent will also play well, and choose the action with the best worst-case result.
In a minimax search:
- the AI tries to maximize the score
- the opponent tries to minimize the score
- terminal boards get a fixed score
A common scoring system is:
- '
+1for an AI win' - '
-1for an opponent win' - '
0for a draw'
Because tic-tac-toe has a tiny search space, you can often search to the end of the game without shortcuts.
A Simple Minimax Implementation
The board below uses a list of nine cells, where "X" is the AI, "O" is the human, and " " is empty.
This is enough to make an unbeatable tic-tac-toe AI. If both sides use minimax correctly, the game ends in a draw unless one player makes a mistake.
Improving Move Quality with Depth
Pure minimax often treats all wins equally. In practice, you usually want the AI to:
- win as quickly as possible
- delay losing as long as possible
You can do that by including search depth in the score:
- '
10 - depthfor an AI win' - '
depth - 10for an opponent win' - '
0for a draw'
That way, the AI prefers a faster checkmate-like finish and avoids a line that loses immediately when a slower loss is still possible.
Alpha-Beta Pruning
For tic-tac-toe, alpha-beta pruning is not required, but it is worth learning because it is the standard optimization for minimax. It avoids exploring branches that cannot affect the final decision.
The idea is:
- '
alphatracks the best score the maximizing player can guarantee' - '
betatracks the best score the minimizing player can guarantee'
When beta <= alpha, the remaining branch can be skipped.
That matters much more in larger games such as chess or connect four, but tic-tac-toe is a good place to understand the concept.
Common Pitfalls
One common mistake is evaluating only the immediate board and ignoring the opponent's next move. That creates a greedy AI that misses forks, blocks too late, and loses to simple traps.
Another mistake is forgetting to undo moves during recursion. If you place "X" on a board cell and do not reset it after the recursive call, later branches will evaluate a corrupted board state.
Scoring can also cause subtle bugs. If wins, losses, and draws are not handled consistently, the AI may prefer a draw over a forced win or may fail to block a loss.
Finally, do not overcomplicate tic-tac-toe with machine learning. Reinforcement learning can be a fun experiment, but minimax is the correct first tool here because the full game can be searched exactly.
Summary
- Minimax is the standard algorithm for choosing the best tic-tac-toe move.
- It works by assuming both players make optimal decisions.
- Tic-tac-toe is small enough that you can search the complete game tree.
- Depth-aware scoring helps the AI win faster and lose later.
- Alpha-beta pruning is a useful optimization, even if tic-tac-toe does not require it.

