Lab 05
Games
Overview of the lab
Games
Perfect information knowledge of moves
Every player knows every move that has ever been made by all players up to the current point in the game. Think of it as no hidden history.
Complete information knowledge of rules / payoffs
Every player knows the rules, possible strategies, and payoff structure for every player. Think of it as no hidden types / incentives; everyone knows what everyone else stands to gain or lose.
| Deterministic | Chance | |
|---|---|---|
| Perfect information | ♟️ chess, go | 🎲 monopoly |
| Complete information | 🚢 battleships | 🃏 poker, scrabble |
Is it true that if a game has perfect information, it also has complete information?
- Perfect information — all past moves. Every player knows the entire history of actions taken in the game up to the current moment (no hidden action). E.g., chess: both players see every piece and every move made on the board.
- Complete information — all game rules / payoffs / types. Every player knows the incentives (utility functions / payoffs) and strategies available to all other players (no hidden rules / goals).
The game: imagine a sequential bargaining game where Player 1 makes a proposal, and Player 2 accepts or rejects it.
- Perfect information? Yes. Player 2 sees Player 1's proposal (the action). If Player 2 rejects, Player 1 sees the rejection. All moves are transparent.
- Complete information? Maybe not. Player 1 might not know the exact minimum amount Player 2 is willing to accept (Player 2's payoff / type). Player 2's threshold is private, hidden information.
Minimax
Its core principle is to minimize the maximum possible loss for the current player, assuming the opponent will always play optimally to maximize their own gain (and thus minimize your score). The name "Minimax" comes from the recursive alternation between two roles:
tries to maximize the score
tries to minimize the score
Game tree
A game tree is a directed graph where:
- Nodes represent a possible state of the game (e.g., a board configuration).
- Edges represent a move from one game state to the next.
1. Minimax is a decision-making tool for two-player turn-taking games.
2. Minimax explores a game tree by anticipating future moves.
3. Minimax uses an evaluation function to assign scores to board states.
4. Minimax strategically chooses the optimal path through recursive exploration and backtracking.
Evaluation function — tic-tac-toe
O (MIN) is to move at the root. Values are backed up from the finished games: MAX levels take the maximum of their children, MIN levels the minimum — so the root gets v = −1 and O should play the centre (node 2).
Backtracking through the game tree
MAX nodeMIN node
minimax.py Python
import numpy as np
def is_terminal(node):
# implement logic to check if the game is over
return isinstance(node, int)
def evaluate(node):
# if we are evaluating a board for example, this function should calculate a score
return node
def get_children(node):
# computing next options from current node
return node
def minimax(node, maximizingPlayer):
if is_terminal(node):
return evaluate(node)
if maximizingPlayer:
maxEval = -np.inf
for child in get_children(node):
eval = minimax(child, False)
maxEval = max(maxEval, eval)
return maxEval
else:
minEval = np.inf
for child in get_children(node):
eval = minimax(child, True)
minEval = min(minEval, eval)
return minEval
game_tree = [
[
[-1, 3],
[5, 1]
],
[
[-6, -4],
[0, 9]
]
]
optimal_score = minimax(game_tree, True)
print(f"The Optimal Score for the starting MAX player is: {optimal_score}") # 3
Properties of minimax
Conditions for minimax optimality
The minimax algorithm guarantees the optimal move for the worst-case scenario against a rational opponent, provided the following conditions hold true:
👥 Two-player
Only two players are involved. The core max / min structure breaks down with more players.
⚖️ Zero-sum
The gain of one player is exactly the loss of the other. The max player's goal (maximize my score) is exactly the inverse of the min player's goal (minimize my opponent's score).
👁️ Perfect information
Both players know the entire state of the game (e.g., chess, checkers, tic-tac-toe). If the game involves hidden information (e.g., poker), minimax is no longer guaranteed to be optimal; other methods like expected minimax are needed.
🎯 Deterministic
There is no element of chance (no dice, shuffled cards, etc.). If chance is involved, a variant called Expectimax is used, where the minimax layers are interspersed with "chance nodes" that calculate expected values.
🌳 Complete search
The algorithm must be able to search all the way to a terminal (game-ending) state. In real-world games like chess this is impossible: the algorithm stops at a fixed depth (the search horizon) and uses a heuristic evaluation function to estimate non-terminal nodes, making the result only heuristically optimal (an approximation).
Minimax is typically used in classical games like chess, checkers, and tic-tac-toe.
The moment any of the above conditions are violated, or a practical limitation is introduced, minimax ceases to be the "optimal" strategy:
- Against a suboptimal opponent: minimax will still win, but it might miss an opportunity to win faster or more decisively. If you know your opponent is weak, you could choose a "trap" move that minimax rejects (because a perfect opponent would avoid the trap), but that an imperfect opponent is likely to fall for.
- Limited search depth: if the search is cut short by an arbitrary depth limit (which is necessary for complex games), the result is only as good as the heuristic evaluation function used at the leaves.
Alpha-beta pruning
Do we need to evaluate the whole game tree?
The MIN node already has a child worth 3. The second MAX child is ≥ 5 after its first leaf — MIN will never choose it, so its last leaf does not matter.
On the right, MIN can already force ≤ −4, which is worse for MAX than the 3 it has on the left — the whole second subtree is pruned. Pruning depends on what order the moves are in.
α (alpha)
The best value that MAX can guarantee at the current level and the levels above.
β (beta)
The best value that MIN can guarantee at the current level and the levels above.
β ≤ α — MIN had a better option available earlier in the tree.
Step through minimax and alpha-beta
The same tree as above. Each node shows its current α / β (green box for MAX, red for MIN); pruned branches get a red cut. Switch to plain minimax to compare how many leaves are evaluated, or change the leaves to see how move ordering changes the pruning.
Search log
alpha_beta.py Python
import numpy as np
def is_terminal(node):
# implement logic to check if the game is over
return isinstance(node, int)
def evaluate(node):
# if we are evaluating a board for example, this function should calculate a score
return node
def get_children(node):
# computing next options from current node
return node
def minimax(node, alpha, beta, maximizingPlayer):
if is_terminal(node):
return evaluate(node)
if maximizingPlayer:
maxEval = -np.inf
for child in get_children(node):
eval = minimax(child, alpha, beta, False)
maxEval = max(maxEval, eval)
alpha = max(alpha, eval)
print(f"eval: {eval}, maxEval: {maxEval}, alpha: {alpha}, beta: {beta}")
if beta <= alpha:
break
return maxEval
else:
minEval = np.inf
for child in get_children(node):
eval = minimax(child, alpha, beta, True)
minEval = min(minEval, eval)
beta = min(beta, eval)
print(f"eval: {eval}, minEval: {minEval}, alpha: {alpha}, beta: {beta}")
if beta <= alpha:
break
return minEval
game_tree = [
[
[-1, 3],
[5, 1]
],
[
[-6, -4],
[0, 9]
]
]
optimal_score = minimax(game_tree, -np.inf, np.inf, True) # the root is MAX
print(f"The Optimal Score for the starting MAX player is: {optimal_score}") # 3
Properties of alpha-beta pruning
- Pruning does not affect the final result.
- Good move ordering improves effectiveness of pruning.
For real games that cannot be searched to the end, the search is limited by:
- Cutoff test — e.g., a depth limit (perhaps add quiescence search).
- Evaluation function — the estimated desirability of a position.
Play tic-tac-toe against minimax
You are X (MAX), the computer is O (MIN) and searches the whole game tree before every move, with the evaluation function above. It never loses — the best you can do is a draw. Turn on the hints to see the minimax value of each of your moves.
Both searches pick an equally good move — alpha-beta just looks at far fewer positions to find it.
Additional materials
- Tic-tac-toe and minimax — here
10 auto-graded tasks, 100 points: MAX and MIN nodes, minimax backup and properties, alpha–beta pruning, move ordering and depth cutoffs.