Building an Unbeatable AI with the Minimax Algorithm
By Niranjan Kumar Singh | Published: February 2026 | Programming
Introduction to Game Theory AI
Have you ever wondered how computers manage to play chess, checkers, or Tic Tac Toe flawlessly? The secret to building an AI that can never be defeated in a zero-sum game lies in a decision rule named the Minimax Algorithm. In this article, we dive deep into the theory and practical implementation of Minimax in JavaScript, exploring how it maps out the entire game tree to guarantee a win or a draw.
The concept of minimax extends far beyond board games. Its mathematical principles underpin everything from stock market trading algorithms to robot motion planning and automated negotiation systems. Understanding it at a fundamental level unlocks a powerful mental model for any kind of adversarial decision-making.
What is a Zero-Sum Game?
Before diving into the algorithm, it is crucial to understand the environment our AI operates in. Tic Tac Toe is a two-player, zero-sum game with perfect information. "Zero-sum" means that one player's advantage is mathematically identical to the other player's disadvantage. If Player X wins (+10 points), Player O inherently loses (-10 points). "Perfect information" means that both players have complete visibility of the board at all times — there is no hidden state, unlike in games like Poker.
Because the number of board configurations (the state space) in Tic Tac Toe is relatively small — limited to a maximum of 9 factorial (362,880) possible game sequences, but realistically much less when accounting for early wins and symmetrical boards — a computer can easily calculate every single possible outcome from any given board state before making a move.
This is fundamentally different from games like Chess, where the state space is staggeringly large (estimated at 10^120 possible games, known as the Shannon Number). Chess engines cannot evaluate every possible game. They use a fixed search depth and apply a heuristic evaluation function to estimate the value of a position. Tic Tac Toe, by contrast, is so small that Minimax can always find the provably perfect move.
The Core Concept of Minimax
The Minimax algorithm simulates all possible futures. It assumes that there are two players: the Maximizer and the Minimizer. The Maximizer wants to push the score as high as possible (striving for a Win), while the Minimizer wants to push the score as low as possible (striving for a Loss for the Maximizer).
- If the AI wins, the score is +10.
- If the Human wins, the score is -10.
- If the game ends in a draw, the score is 0.
When it is the AI's turn to move, it tests placing its symbol in the first empty spot. Then, it calls itself recursively, switching roles, pretending to be the human player making the best possible counter-move. It continues this alternating back-and-forth simulation until the board reaches a terminal state (Win, Loss, or Draw).
Once a terminal state is reached, the score is returned up the chain. The AI then selects the move that resulted in the highest possible guaranteed score, knowing that the human will always try to pick the move that yields the lowest score.
The Recursive Structure: Walking the Game Tree
The game tree is a hierarchical representation of every possible future state of the game. At the root of the tree is the current board state. From that root, branches extend to every possible move the current player can make. Each of those nodes then branches further, representing the opponent's possible responses. This continues until all branches terminate in a leaf node — a game-ending state of Win, Loss, or Draw.
The minimax function visits every node in this tree using depth-first recursion. At "maximizer" nodes (the AI's turn), it chooses the child with the maximum score. At "minimizer" nodes (the opponent's turn), it chooses the child with the minimum score. The intelligence of the AI emerges not from any stored knowledge, but purely from the structure of the tree traversal itself.
Here is the core pseudocode for the algorithm's structure:
function minimax(board, depth, isMaximizing):
if board is in terminal state:
return score (considering depth)
if isMaximizing:
bestScore = -Infinity
for each empty cell:
play AI's move in cell
score = minimax(board, depth + 1, false)
undo move
bestScore = max(score, bestScore)
return bestScore
else:
bestScore = +Infinity
for each empty cell:
play Human's move in cell
score = minimax(board, depth + 1, true)
undo move
bestScore = min(score, bestScore)
return bestScore
Handling Depth and Efficiency
While standard Minimax is powerful, a raw implementation treats a win in 1 move the exact same as a win in 5 moves. A sophisticated AI should prefer to win as quickly as possible, and lose as slowly as possible (if losing is inevitable). We achieve this by adding or subtracting the depth (number of moves made so far) from the final score.
If the AI wins at depth 2, the score is 10 - 2 = 8. If the AI wins at depth 6, the score is 10 - 6 = 4. The Maximizer will naturally select the move that yields the score of 8, thereby winning faster. Similarly, if the AI is losing, a loss at depth 8 (score = -10 + 8 = -2) is preferred over a loss at depth 2 (score = -10 + 2 = -8), because it delays defeat and gives the human player more opportunities to make a mistake.
Alpha-Beta Pruning
To further optimize the performance of our Tic Tac Toe engine, we use Alpha-Beta Pruning. This is an optimization technique that stops evaluating a move when at least one possibility has been found that proves the move to be worse than a previously examined move. It drastically reduces the number of nodes the algorithm evaluates in the game tree, ensuring our AI responds in milliseconds even on low-end mobile devices without causing the browser to freeze.
The algorithm maintains two values: alpha (the best score the Maximizer is guaranteed so far) and beta (the best score the Minimizer is guaranteed so far). When the algorithm discovers that a branch cannot possibly improve upon the current alpha or beta, it "prunes" that branch — stops evaluating it entirely — and moves on. In practice, Alpha-Beta pruning can reduce the search space from O(b^d) to approximately O(b^(d/2)), allowing the algorithm to search roughly twice as deep in the same amount of time.
Limitations and Practical Considerations
While Minimax is perfect for Tic Tac Toe, there are important practical considerations when applying it to the real world. The technique scales poorly to larger games — even with Alpha-Beta pruning, a game like Go (with a 19×19 board) has too many states to enumerate fully. Modern AI systems for such games (like AlphaGo) use Monte Carlo Tree Search combined with deep neural networks to evaluate positions rather than brute-force tree traversal.
Another consideration is the evaluation function in non-terminal midgame states. In Tic Tac Toe, we only need to score terminal states. But in Chess, we need a function that estimates the value of a position while the game is still ongoing. The design of these heuristic evaluation functions is one of the primary challenges in classical game AI research.
Conclusion
By relying on the Minimax algorithm and Alpha-Beta pruning, the Tic Tac Toe Master game guarantees an offline, client-side opponent that plays perfectly. It cannot be beaten — the best a human can hope for is a draw. Building this was an excellent exercise in recursion, depth-first search, and algorithmic thinking, forming a foundation that can be expanded into much more complex systems like Checkers or Chess. The Minimax algorithm, first formalized by John von Neumann in 1928, remains one of the most elegant and foundational concepts in all of computer science.