Games were one of the first big challenges for AI: clear rules, a clear goal, and an opponent trying to beat you. The ideas - looking ahead, assuming the opponent plays well, and pruning hopeless options - apply to planning and negotiation too.
Game trees#
In a two-player, turn-taking, perfect-information game (tic-tac-toe, chess, Go):
- MAX (our AI) wants the highest score; MIN (the opponent) wants the lowest.
- Each node of the game tree is a position; each edge is a move.
- Terminal positions get a utility: +1 win for MAX, -1 loss, 0 draw.
The minimax algorithm#
Work backwards from the leaves: at MAX nodes take the maximum of the children's values, at MIN nodes the minimum. The value at the root is the best MAX can guarantee against a perfect opponent.
MAX → 3 (MAX picks the left move)
/ | \
MIN MIN MIN → 3, 2, 2
/ | \ / | \ / | \
3 12 8 2 4 6 14 5 2
MIN [3, 12, 8] -> 3 MIN [2, 4, 6] -> 2 MIN [14, 5, 2] -> 2 MAX [3, 2, 2] -> 3 value of the game: 3
Minimax में AI मान लेता है कि सामने वाला खिलाड़ी हमेशा सबसे अच्छी चाल चलेगा। इसलिए अपनी बारी (MAX) में सबसे बड़ा score चुनो, और विरोधी की बारी (MIN) में सबसे छोटा मान लो। नीचे से ऊपर ऐसे हिसाब लगाने पर root पर वह score मिलता है जो AI पक्का हासिल कर सकता है।
An unbeatable tic-tac-toe AI#
Tic-tac-toe has fewer than 6,000 distinct reachable positions, so minimax can search all the way to the end. Here the AI plays O and scores a position from O's point of view:
X | | O ---+---+--- | X | ---+---+--- | | O plays square 8 (expected result +0), after searching 932 positions
X threatens to complete the diagonal 0-4-8. Minimax discovers that every other move loses (score -1), so it blocks at square 8. Change the board (use "X", "O" or " ") and run again; you will never beat it.
Searching the whole tree from the empty board visits about half a million positions in this simple program. That is fine for tic-tac-toe, impossible for chess (about 10^120 possible games).
Alpha-beta pruning#
Minimax explores every branch, but many cannot change the decision. Alpha-beta pruning keeps two numbers while searching:
- α (alpha) - the best score MAX can already guarantee,
- β (beta) - the best score MIN can already guarantee.
When α ≥ β at a node, the remaining children are pruned (skipped): the opponent would never let the game reach this position.
alpha-beta value: 3 nodes visited - minimax: 13 alpha-beta: 11
After the first branch MAX knows it can get 3. In the second branch, the first leaf is 2 - MIN can force ≤ 2, which is worse than 3 for MAX, so the rest of that branch is skipped. With good move ordering, alpha-beta can search roughly twice as deep as plain minimax in the same time.
Real games: depth limits and evaluation functions#
For chess or Go the tree is far too big to search to the end, so programs:
- search only a few moves ahead (a depth limit), and
- score the positions at that depth with an evaluation function - for chess, material (queen = 9, rook = 5...), king safety, piece mobility, pawn structure.
def evaluate(position):
return 9 * (my_queens - their_queens) + 5 * (my_rooks - their_rooks) + 3 * (my_minor - their_minor) + ...
| System | Approach |
|---|---|
| Deep Blue (1997, chess) | alpha-beta search on special hardware + hand-tuned evaluation |
| Stockfish (chess) | alpha-beta + neural-network evaluation (NNUE) |
| AlphaGo / AlphaZero (2016-17) | Monte Carlo Tree Search guided by neural networks learned through self-play (reinforcement learning) |
Summary#
- Two-player games form game trees; MAX maximises and MIN minimises the utility.
- Minimax backs values up from the leaves; it plays perfectly when it can search to the end.
- Alpha-beta pruning gives the same answer while skipping branches that cannot matter.
- Big games need depth limits, evaluation functions, and - in modern systems - learned neural evaluations.