Tic-Tac-Toe
Tic-Tac-Toe is a solved game โ with perfect play, it always ends in a draw. The AI here uses the minimax algorithm to play perfectly, making it a useful demonstration of decision tree search and an impossible opponent.
Tic-Tac-Toe
Two-player or unbeatable minimax AI โ a solved game.
Game modes
vs AI (minimax)
The AI uses the minimax algorithm to evaluate all possible game states. It will never make a mistake. The best result you can achieve is a draw โ the AI wins if you make any suboptimal move.
Good for: understanding perfect play, seeing minimax in action
Two-player (local)
Pass the device between two players. X goes first. The game detects wins (three in a row in any direction) and draws (board full, no winner). Score is tracked across rounds.
Good for: quick games with a friend on the same device
How the minimax AI works
Minimax is a recursive algorithm that explores every possible move from the current board state. For each possible move, it simulates the opponent's best response, then the AI's best counter-response, and so on until the game ends. Each terminal state (win, lose, draw) gets a score (+10 for AI win, -10 for player win, 0 for draw). The AI picks the move with the highest score.
The algorithm works by alternating between two roles at each level of the tree: the maximizing player (the AI, trying to achieve the highest possible score) and the minimizing player (the human, assumed to be trying to achieve the lowest possible score from the AI's perspective). At each node, the maximizing player picks the child node with the highest value, while the minimizing player picks the child with the lowest value. This mutual assumption of optimal play is what makes the resulting strategy genuinely unbeatable.
Why it's unbeatable
Tic-Tac-Toe has at most 9! = 362,880 possible game states (much fewer with symmetry pruning). Minimax evaluates all of them in milliseconds. There is no move you can make that the AI hasn't already considered and has a prepared response for. The game tree is fully solved.
Why Tic-Tac-Toe is a solved draw
A game is considered "solved" when the outcome under perfect play from both sides is known with certainty before the first move is made. Tic-Tac-Toe has been solved completely: if both players play optimally, the game always ends in a draw. No advantage accrues to going first โ the first-mover advantage that exists in many other games is fully neutralized by correct defensive play from the second player.
This was established long before computers through careful manual analysis of the game tree, but minimax makes it immediately verifiable. Run minimax from any empty board and every branch of perfect-play resolves to a draw. The proof is embedded in the algorithm itself: the AI can always force a draw as the second player, and the best the first player can do is also a draw.
The reason Tic-Tac-Toe remains widely played despite this fact is that human players are not perfect. Most casual games end decisively because one or both players make suboptimal moves. The game's value as a teaching tool โ in mathematics, computer science, and game theory โ comes precisely from the gap between the known optimal solution and the average human performance. That gap is what makes the minimax AI feel unbeatable rather than merely occasionally difficult.
The best first move and why it matters
There are three strategically distinct positions on a standard 3x3 board: the center, the four corners, and the four edges (the non-corner middle cells of each side). These three position types have meaningfully different values as opening moves.
The center is the most valuable single square on the board. It participates in four winning lines โ both diagonals, the middle row, and the middle column. A player who holds the center can threaten more lines simultaneously than from any other position. However, the center opening is well-understood by any experienced player, and the correct response (taking a corner) is widely known. Against a player who responds correctly, a center opening does not generate immediate advantage.
Corners are generally considered the strongest opening for X. A corner participates in three winning lines: one row, one column, and one diagonal. More importantly, a corner opening creates fork threats that are harder for an inexperienced opponent to navigate. If X opens in a corner and O does not play center immediately, X can often force a win in five moves by setting up a fork. The two-corner strategy โ where X takes a corner, forces a response, then takes the opposite corner โ is one of the most reliable winning sequences against non-optimal opponents.
Edge moves (the four non-corner perimeter squares) are the weakest opening. An edge cell participates in only two winning lines, and an edge opening gives the opponent more room to maneuver without creating immediate threats. Edge openings are generally a mistake in terms of maximizing winning chances against a suboptimal opponent.
Opening summary: Play corner first if you want maximum winning potential against a casual opponent. If your opponent plays center in response, take the opposite corner. If they play anywhere other than center, you likely have a forced win available through a fork setup.
Forks: the central skill
A fork is a board position where you have two separate unblocked lines each needing only one more of your marks to complete. Since your opponent can only block one cell per turn, you win on the following move by completing whichever line was not blocked. Creating a fork is the primary winning method in Tic-Tac-Toe and the central skill that separates players who win consistently from those who draw or lose.
The classic fork setup goes like this: X plays top-left corner (cell 1). O is forced to respond somewhere. X plays bottom-right corner (cell 9, the diagonally opposite corner). Now X threatens to complete the main diagonal with cell 5 (the center). If O does not take cell 5, X plays there and has a diagonal and can expand to corners for a double threat. Even if O takes the center, X can find a fork through the remaining corners. The specific moves change depending on O's responses, but the principle is the same: place your marks such that completing any two of them would win, and the opponent cannot block both at once.
A double-corner strategy is one of the most reliable fork creators. If X plays two opposite corners (top-left and bottom-right, or top-right and bottom-left), and O plays an edge cell rather than the center, X can take the center and now threatens two separate diagonal completions. O can only block one; X wins the other. This sequence works consistently against players who take edges in response to corner openings.
Blocking forks is equally important. When you recognize that your opponent is setting up a fork โ that is, they have two marks placed such that a single cell would complete a fork for them โ you must act. You have two options: take the fork cell yourself (directly blocking it), or create an immediate two-in-a-row threat that forces the opponent to block you, and then play the fork-blocking cell on your next turn. The second option is only available if the threat you create does not itself give the opponent a fork in response.
Optimal responses to every opening
Because the game is fully solved, there is a correct response to every possible opening move. Understanding these responses is the most direct path to achieving draws (the best possible outcome) against the minimax AI, and wins against casual opponents who deviate from optimal play.
If X (or the opponent) opens in a corner: the only response that prevents an immediate fork threat is to take the center. Taking any other position allows X to take the opposite corner on the next move and set up a fork. After O takes the center, X's best continued play goes for the opposite corner, and from there the game typically draws with correct play from both sides.
If X opens in the center: the only moves that hold are corners. Edge responses allow X to set up a fork on the very next move. Specifically, if O plays an edge in response to X's center, X can take two corners on either side of that edge and create a fork where O cannot block both winning threats. Any corner response by O leads to a drawable game with correct continued play.
If X opens on an edge: this is the weakest opener and O has more flexibility in response. The center is still the strongest reply, after which O holds a positional advantage (the center controls more lines). A corner adjacent to the edge is also reasonable. Unlike the corner and center openers, an edge opener by X does not create immediate fork threats and O can draw without needing a specific response.
Optimal strategy for humans
If you want to draw against the minimax AI (the best you can do), follow these rules:
The priority list: win, then block, then fork
Experienced Tic-Tac-Toe players use a decision priority list to choose their move each turn. The list is ordered strictly: always fulfill the highest-priority condition available. This is also, essentially, what the minimax algorithm computes โ the list just makes the reasoning explicit and memorable for human players.
- 1.Win: if you can complete a line and win on this move, do it. This takes absolute priority.
- 2.Block: if your opponent can win on their next move, block that cell immediately. Missing a block is the most common way casual players lose.
- 3.Fork: if you can place a mark that creates two simultaneous winning threats, take it. Your opponent cannot block both.
- 4.Block a fork: if your opponent has a fork opportunity, block it โ either by directly taking the forking cell, or by creating a two-in-a-row threat that forces them to respond, then blocking their fork cell next turn.
- 5.Center: if none of the above apply, take the center. It is the most valuable neutral position.
- 6.Opposite corner: if your opponent has a corner, take the diagonally opposite corner. This counters the common opposite-corner fork strategy.
- 7.Empty corner: if no opposite corner is available, any empty corner is preferred over an edge.
- 8.Empty edge: take any remaining empty edge as the last resort.
This eight-step priority list, applied consistently, guarantees a draw against any opponent and a win against any opponent who makes at least one suboptimal move. It is the manual equivalent of the minimax algorithm applied to this specific game, and it produces the same results โ the list is correct by construction, since it matches what minimax would compute at each decision point.
Turning mistakes into wins
Playing two-player against another human, your goal is not merely to avoid losing โ it is to detect the moment your opponent deviates from optimal play and exploit that deviation immediately. Every suboptimal move your opponent makes opens a window, and often that window closes quickly if you do not act in the same turn.
The most common deviation is missing a block. A player who fails to block your two-in-a-row gives you a free win. The second most common deviation is taking an edge cell instead of the center in response to a corner opener โ this hands you the fork opportunity described above. A third common mistake is responding to your fork threat by blocking the wrong line. When you have created two separate threats, the only correct response is to block one of them (you pick which one gets blocked; the other wins). But some players, confused by the dual threat, respond with an offensive move or block a non-existent threat โ giving you both lines.
A useful habit when playing humans is to scan for fork opportunities after every single move โ both yours and your opponent's. Before you place your mark, check: does any single empty cell give me two simultaneous winning lines? If yes, play it. Also check: does any empty cell give my opponent two simultaneous winning lines? If yes, play there to block, unless you can win outright this turn (priority 1 takes precedence).
A concrete example of exploiting a mistake: X opens top-left, O plays top-right (an edge-adjacent corner rather than center โ a common beginner response). X plays bottom-right (opposite corner to X's first move). O, now worried about the diagonal, blocks at the center. X plays bottom-left. Now X has both lower corners and threatens to win on the left column (top-left, center-left, bottom-left) and the bottom row (bottom-left, bottom-center, bottom-right). O can only block one. X wins. The mistake was O's first move โ taking a corner instead of the center handed X the fork setup within three moves.
Practice tip: When you lose a game against the minimax AI, trace back to the exact move where you fell behind. The AI will have exploited a specific positional error โ usually a missed block, a suboptimal response to a corner opener, or a fork you failed to see. Identifying the single error is more instructive than replaying the whole game.
Tic-Tac-Toe as a learning tool
Beyond its value as a game, Tic-Tac-Toe occupies an important place in computer science education precisely because it is fully solvable and small enough to reason about completely. It was one of the first games programmed on early computers โ a 1952 program called OXO, written by Alexander Douglas as part of his doctoral thesis on human-computer interaction, is believed to be among the first implementations of a video game. It played perfect noughts and crosses against a human player using minimax logic.
The game is used today in introductory AI courses because minimax on the Tic-Tac-Toe game tree is simple enough to implement in an afternoon, yet demonstrates all the core concepts of adversarial search: the game tree, terminal states, score functions, recursive evaluation, and the alternating maximizer-minimizer structure. Understanding how minimax works on this small game provides the conceptual foundation for understanding how it works on much larger and more complex games like Chess and Go, where the tree is too large to search exhaustively and additional techniques (alpha-beta pruning, heuristic evaluation functions, Monte Carlo tree search) become necessary.
The game also illustrates the concept of game-theoretic equilibrium at a level students can verify by hand. The Nash equilibrium of Tic-Tac-Toe is a draw with perfect play, and this can be confirmed exhaustively by tracing through the game tree manually โ which is actually feasible given the small state space. This hands-on verifiability makes it an unusually good teaching example for abstract concepts that normally require faith in the mathematics.
If you want to see minimax in action directly, the AI mode in this implementation shows the algorithm's result in real time. Play a few games, make deliberate mistakes at different points in the game, and observe how quickly and precisely the AI capitalizes. Each game you lose is a demonstration of a specific branch of the game tree that the AI had pre-computed and was waiting to enter.
Frequently Asked Questions
Can I actually beat the AI?
No โ not if it plays perfectly. The minimax algorithm evaluates every possible game state and always chooses the optimal move. Against a perfect minimax player, the best you can achieve is a draw (if you also play optimally).
What is the minimax algorithm?
Minimax is a decision algorithm used in two-player games. The AI assumes the opponent always plays optimally and picks the move that minimizes the opponent's maximum possible gain. For Tic-Tac-Toe, the search space is small enough that minimax can evaluate all possible games in milliseconds.
Is Tic-Tac-Toe a solved game?
Yes. With perfect play from both sides, every game ends in a draw. The minimax AI proves this โ it will never lose, and will only draw against a perfect opponent.
Related Articles & Guides
Tic Tac Toe Strategy: How to Never Lose (and Force a Draw Every Time)
Tic Tac Toe is one of the oldest, simplest games ever devised. Three rows, three columns, nine squares, andโฆ
Read guide โArticleTic-Tac-Toe Online: Play vs AI with Minimax Algorithm
Play Tic-Tac-Toe against an unbeatable minimax AI or a friend in two-player mode.
Read guide โBlog2048: History, Addiction, Math & Tips to Win
There is a certain kind of puzzle that feels too simple to be interesting and too interesting to put down.โฆ
Read guide โ