Attic › Computer science › Minimax

Minimax calculates the best move by working backward from the end of a game tree

To apply minimax across many turns, a computer maps out future moves in a diagram called a game tree. The algorithm starts at the end of the game and works backward to the current turn. It alternates between choosing the highest score on your turns and the lowest score on your opponent's turns.

In combinatorial game theory, a game tree represents every possible sequence of moves. Each point in the tree is a node, which represents a specific board position:

* The root node at the top is the current state of the game.

* The lines going down from a node are branches, which represent legal moves that lead to new nodes on the next turn.

* The terminal nodes (or leaves) at the bottom are finished games where one player has won, lost, or drawn.

The minimax algorithm labels the two players Max (you) and Min (your opponent). Max wants to maximize the final score, and Min wants to minimize it. Because a computer cannot know the value of a move until it sees where that move leads, it must evaluate the terminal nodes first and pass their scores up the tree:

1. Generate the game tree down to the terminal nodes.

2. Assign a numerical score to each terminal node from Max's perspective (for example, +1 for a win, 0 for a draw, and −1 for a loss).

3. Move up one level to the turn right before the game ends. If it is Max's turn at a node, assign that node the highest score among its branches. If it is Min's turn, assign it the lowest score.

4. Repeat this process up the tree, alternating between maximum and minimum scores at each level, until the root node has a score.

The score that reaches the root node is the guaranteed outcome of the game if both players make no mistakes. To act, Max chooses the branch from the root node that brought up that winning score. Because the goals of the two players are exact opposites, programmers often use a simpler variation called negamax. Negamax uses a single rule for both players: on every turn, a player maximizes the negative of the opponent's score, based on the relation max(a, b) = −min(−a, −b).

Fact-checked against Minimax on Wikipedia

Part of the Computer science syllabus: Artificial Intelligence and Modern Computing

See it on the timeline of Computer science