How Game AI Works: Minimax, Alpha-Beta Pruning, and Negamax Explained
Discuss this article with your favorite AI assistant
Direct Answer: The AI in the games on this site works by searching the game tree with minimax and alpha-beta pruning — the AI imagines the moves both players could make, evaluates the resulting positions, and picks the move that leads to the best outcome assuming the opponent plays their best. The difficulty setting controls search depth — how many moves ahead the AI looks. On this site the real depths are: Checkers at 1, 3, and 6; Mancala at 4 and 6 (with a heuristic Easy); Chess at 1, 2, and 3. Depth 1 looks one move ahead; depth 6 looks six moves ahead — and every extra ply of depth makes the AI dramatically harder to beat.
The Core Idea: Search the Game Tree
Every turn-based game is a tree of possibilities. From the current position, you have a set of legal moves; from each of those, your opponent has a set of replies; from each reply, you have another set; and so on. A game AI's job is to search this tree — to look ahead through the branches and choose the move that leads to the best outcome.
The catch is scale. Chess has an average branching factor around 30, so looking 4 moves ahead means evaluating roughly 30^8 positions — billions of branches. That is why search depth is the fundamental resource: every extra ply multiplies the work by the branching factor. It is also why the difficulty settings on the site differ by depth — the AI's strength is, quite literally, how far it looks.
The foundational algorithms — minimax, alpha-beta pruning, and negamax — are the same ones that power the site's AI and most game AIs in the world. The Hardest Unbeatable AI Games guide ranks the results; this post explains the machinery underneath.
Minimax: Playing for the Best Worst Case
Minimax is the decision rule at the heart of turn-based game AI. The idea is simple and named precisely: the AI wants to maximize its own score, while assuming the opponent will minimize it.
Here is how it works. The AI evaluates the board with a scoring function — in chess terms, material counts (a pawn is 1, a knight is 3, a rook is 5, a queen is 9) plus positional bonuses. At the deepest level of the search, it scores every reachable position. Then it propagates those scores upward:
- At the AI's turn (the maximizing player), it picks the child position with the highest score — the best move available.
- At the opponent's turn (the minimizing player), it assumes the opponent picks the child position with the lowest score — the worst outcome for the AI.
The result is the best worst case: the AI chooses the move whose guaranteed outcome is as good as possible, assuming the opponent plays perfectly. This is exactly the logic used by the chess AI training plan when it describes the AI "finding the best move" — the search, not intuition, is what finds it.
Alpha-Beta Pruning: Searching Smarter
Minimax alone is too slow for any real game. Alpha-beta pruning is the optimization that makes minimax practical — it prunes branches that cannot possibly matter.
The algorithm tracks two bounds as it searches:
- Alpha: the best score the maximizing player can already guarantee.
- Beta: the best score the minimizing player can already guarantee.
If a branch's evaluation falls outside these bounds — if the maximizing player already has a better option elsewhere — the search stops exploring it. The move is provably worse than something already found, so exploring further is wasted work. This pruning does not change the result; it only skips branches that could never be chosen. On a good day, alpha-beta reduces the search tree to roughly the square root of its full size — turning billions of positions into millions.
Every game AI on the site uses this optimization. The checkers logic (in the site's checkers AI guide) passes alpha and beta through its minimax exactly this way, and the mancala engine does the same in its evaluation of captures and seed counts.
Negamax: One Function Instead of Two
Negamax is a mathematical simplification of minimax for zero-sum games — games where one player's gain is exactly the other player's loss (which is true of every game on this site). Instead of writing separate code for the maximizing and minimizing turns, negamax notes that the value of a position for the current player is the negation of its value for the opponent.
Concretely: the AI evaluates a position from its own perspective, then recursively negates the score at every level. A great move for the opponent is a terrible score for the AI, and negating it at each step keeps everything in one consistent frame. The result is cleaner code that is mathematically identical to minimax — which is why many professional engines implement the negamax form with alpha-beta pruning built in. (The site's own engines use the equivalent classic minimax form — same results, two ways of writing the math.)
The distinction between minimax and negamax is purely implementation — the search results are identical. But it is worth knowing the term because it appears throughout game-AI literature, and the same family of algorithms powers everything from the site's Connect 4 AI to professional chess engines.
The Site's Real Difficulty Depths
Difficulty settings on the site are search depth — nothing more. Here are the actual values in the game code, which you can verify by playing and feeling the difference:
- Checkers: Easy = depth 1 (one move ahead), Medium = depth 3, Impossible = depth 6. The jump from 3 to 6 is enormous — depth 6 searches the checkers tree deep enough that only a strong player survives. The difficulty guide in Beat the Checkers AI explains how to train against each level.
- Mancala: Easy = heuristic moves with randomness (no deep search at all — it picks a reasonable-looking move with a random tiebreak), Medium = depth 4, Hard = depth 6. The heuristic Easy is why it occasionally makes odd moves; the depth-6 Hard rarely misses a capture. The evaluation function rewards store gains, extra turns, and captures — the same rules of thumb in Mancala Rules of Thumb.
- Chess: Easy = depth 1, Medium = depth 2, Hard = depth 3. Chess's branching factor (about 30) makes deeper search exponentially expensive, which is why the site's chess AI trades depth for speed — and why the chess AI training plan recommends the medium setting for practice: it plays solid but humanly beatable chess.
The pattern across all three games is the same: deeper search = stronger play. A depth-1 AI is beatable by anyone who thinks one move ahead; a depth-6 AI requires genuine strategy. That gradient is exactly what makes the difficulty settings useful for learning — see the ranked AI guide for which games are truly unbeatable at their hardest setting.
Evaluation Functions: The Brain Behind the Search
Search depth is only half the story. The other half is the evaluation function — the scoring formula the AI uses to judge the positions it reaches at the bottom of its search. A deep search with a bad evaluator plays badly; a shallow search with a great evaluator can play surprisingly well. The site's games use a mix of both:
- Material and structure in chess. The chess AI scores material first — pawn 1, knight 3, bishop 3, rook 5, queen 9 — and adds positional bonuses for things like king safety and piece activity. That is why the chess motifs guide and the training plan both emphasize hanging-piece awareness: the AI will happily take free material, and it punishes blunders because its evaluator is watching for exactly that.
- Captures, stores, and extra turns in mancala. The mancala evaluator rewards store gains, extra turns (a capture that lands in your store lets you go again), and captured stones — the same heuristics explained in Mancala Rules of Thumb. The Easy mode skips deep search entirely and picks from these heuristics with a random tiebreak, which is why Easy mancala can look oddly human.
- Advantage and threats in checkers. The checkers evaluator counts material and positional advantage, with extra weight on the forced captures that define the game. Combined with depths 1, 3, and 6, that is enough to make the checkers difficulty guide honest about the Impossible setting: strong, but beatable by a player who sees one more ply.
The evaluation function is also where AIs get their "personality." A purely material-based evaluator plays greedy chess; one that values piece activity plays aggressive chess. When you feel like an AI "has a style," you are feeling its evaluation weights — not its search.
Why Depth and Evaluation Work Together
The practical lesson for players is that the difficulty settings are a package deal: depth decides how far the AI looks, and the evaluator decides how well it judges what it sees. Three consequences follow:
- A deeper search punishes slow plans. The depth-6 checkers AI will see a trap three moves before it lands, because it is searching through the whole sequence. Players who beat it do so by creating threats faster than the search can resolve them — the approach detailed in the Hardest Unbeatable AI Games guide.
- A shallow search is exploitable. Depth-1 chess is beatable by anyone who thinks two moves ahead — it literally cannot see your second move. That is why Easy settings exist: they are training wheels that show the cost of not looking ahead. The ranked AI guide's advice to start at Easy and climb is really advice to grow your own search depth.
- Solved games are the exception. Tic-Tac-Toe is solved, so its AI can play perfectly regardless of search depth — the full game tree is tiny. When you play the hardest Tic-Tac-Toe AI, you are not beating a deeper search; you are playing against a perfect player, which is a different problem entirely. Open Tic Tac Toe to feel that difference in a few games.
Understanding the pair — search depth plus evaluation — is the single fastest way to improve against the site's AIs. It tells you what each difficulty level is actually doing, which tells you exactly what to exploit: either out-search it, or find the blind spots in its evaluator.
Frequently Asked Questions
What is minimax in game AI?
Minimax is the decision rule where the AI maximizes its own score while assuming the opponent minimizes it. It searches the game tree and picks the move with the best guaranteed outcome — the best worst case.
What is alpha-beta pruning?
Alpha-beta pruning skips branches that cannot improve the result, using two bounds (alpha and beta) to prove a branch is irrelevant. It returns the exact same move as plain minimax but searches far fewer positions.
What is the difference between minimax and negamax?
Negamax is a simplification of minimax for zero-sum games: the value of a position for one player is the negation of its value for the opponent. It produces identical results with simpler code.
How do difficulty settings change the AI?
They change search depth — how many moves ahead the AI looks. Checkers uses depths 1, 3, and 6; Mancala uses 4 and 6; Chess uses 1, 2, and 3. Deeper search means stronger play.
Can I beat the hardest AI on the site?
In some games, yes — a depth-6 checkers or mancala AI is strong but beatable with real strategy. In solved games like Tic-Tac-Toe, the hardest AI plays perfectly and cannot be beaten. The ranking is in Hardest Unbeatable AI Games, Ranked.
Summary and Key Takeaways
- Game AI on this site works by searching the game tree with minimax and alpha-beta pruning.
- Minimax picks the best worst case; alpha-beta prunes irrelevant branches; negamax is the simplified zero-sum form.
- Difficulty = search depth: Checkers 1/3/6, Mancala 4/6 (heuristic Easy), Chess 1/2/3.
- Deeper search is exponentially more expensive but dramatically stronger.
- Beating the AI is about thinking one ply deeper than it does — or exploiting the evaluation's blind spots.
Test the Search Depth Yourself
Play chess at Easy, then Medium, then Hard, and feel the difference one extra ply of search makes. Free, no-login, instant.
Play ChessWant to train against each level? See Beat the Chess AI: Training Plan.