Tic-Tac-Toe Minimax Exercise

Applying Minimax and Alpha-Beta to a Real Game

From game state to optimal strategy

Problem Statement

Step 1: Current Game State

Consider the following Tic-Tac-Toe board in the middle of a game:

1
2
3
4
5 X
6 O
7
8
9 X
X

MAX Player
Wants to WIN (+1)
O

MIN Player
Wants to prevent X's win (-1)
=

Draw
Nobody wins (0)
Step 2: Game Rules & Constraints
Current Situation:
  • X occupies positions 5 and 9
  • O occupies position 6
  • It's O's turn (MIN moves next)
  • Available moves: {1, 2, 3, 7}
Game Assumptions:
  • Game ends after O's move + X's response
  • Utility values: +1 (X wins), -1 (O wins), 0 (draw)
  • Both players play optimally
  • Positions 4 and 8 are blocked/unavailable
Step 3: O's Strategic Challenge

O's Dilemma: Where should O play to get the best possible outcome?

🤔 O's Reasoning:
  • "If I play at position 1, what will X do?"
  • "Can I force a win, or at least prevent X from winning?"
  • "I need to consider X's best response to each of my moves"
  • "What's the safest choice assuming X plays perfectly?"
⚡ X's Threats:
  • Diagonal threat: X has 5 and 9, could win with 1
  • Column threat: X has 5 and 9, could win with middle column
  • Multiple threats: X might create winning opportunities
  • Counter-attack: X will try to win if given the chance
Your Mission

Solve O's strategic problem using systematic game tree analysis:

  • Part A: Build the complete game tree (show all possible moves and outcomes)
  • Part B: Apply minimax evaluation (find optimal strategies for both players)
  • Part C: Use alpha-beta pruning (optimize the search with same results)

Goal: Determine O's best move and understand why it's optimal!

  • 1. For each of O's possible moves {1, 2, 3, 7}:
    • Show the resulting board state after O's move
    • List X's possible responses (remaining empty positions)
    • Show the final board state after X's response
    • Determine the game outcome: +1 (X wins), -1 (O wins), or 0 (draw)
Complete Game Tree Construction
1 Branch 1: O plays at position 1
O
2
3
4
X
O
7
8
X
After O→1
X can respond at: {2, 3, 7}
X→2:
No winning line possible → 0 (Draw)
X→3:
No immediate win → 0 (Draw)
X→7:
No immediate win (O blocks diagonal) → 0 (Draw)
2 Branch 2: O plays at position 2
1
O
3
4
X
O
7
8
X
After O→2
X can respond at: {1, 3, 7}
X→1:
X gets diagonal (1,5,9) → +1
X→3:
No immediate win → 0 (Draw)
X→7:
No immediate win → 0 (Draw)
3 Branch 3: O plays at position 3
1
2
O
4
X
O
7
8
X
After O→3
X can respond at: {1, 2, 7}
X→1:
X gets diagonal (1,5,9) → +1
X→2:
No immediate win → 0 (Draw)
X→7:
No immediate win (can't complete anti-diagonal) → 0 (Draw)
4 Branch 4: O plays at position 7
1
2
3
4
X
O
O
8
X
After O→7
X can respond at: {1, 2, 3}
X→1:
X gets diagonal (1,5,9) → +1
X→2:
No immediate win → 0 (Draw)
X→3:
No immediate win → 0 (Draw)
Key Observation

In every branch, if X plays at position 1, X wins by completing the main diagonal (1-5-9). This is X's dominant strategy - O must find a way to deal with this threat!

O's Move X's Responses Outcomes Summary
1 X→2, X→3, X→7 0, 0, 0 All moves lead to draws
2 X→1, X→3, X→7 +1, 0, 0 X wins with move 1
3 X→1, X→2, X→7 +1, 0, 0 X wins with move 1
7 X→1, X→2, X→3 +1, 0, 0 X wins with move 1
  • 2. Apply the Minimax algorithm:
    • At leaf nodes, use the utility values from Part A
    • At MAX nodes (X's turn), take the maximum value
    • At MIN nodes (O's turn), take the minimum value
    • Determine which move O should choose at the root
Minimax Evaluation Solution
1 Evaluate each of O's moves (MIN nodes):
O plays at 1

X can choose: {2, 3, 7}

Outcomes: [0, 0, 0]

MAX chooses: MAX(0, 0, 0) = 0

All X moves lead to draws
O plays at 2

X can choose: {1, 3, 7}

Outcomes: [+1, 0, 0]

MAX chooses: MAX(+1, 0, 0) = +1

X will play at position 1 to win
O plays at 3

X can choose: {1, 2, 7}

Outcomes: [+1, 0, 0]

MAX chooses: MAX(+1, 0, 0) = +1

X will play at position 1 to win
O plays at 7

X can choose: {1, 2, 3}

Outcomes: [+1, 0, 0]

MAX chooses: MAX(+1, 0, 0) = +1

X will play at position 1 to win
2 Root MIN Node Evaluation:
O (MIN) faces these backed-up values from each possible move:
  • Move to position 1 → Value: 0
  • Move to position 2 → Value: +1
  • Move to position 3 → Value: +1
  • Move to position 7 → Value: +1
MIN choice: MIN(0, +1, +1, +1) = 0
📊 Minimax Tree Visualization
O's Turn (MIN Root - wants to minimize X's score) O [value: 0] / | \ \ (move 1) / | \ \ (move 7) / | \ \ X₁ [0] X₂ [+1] X₃ [+1] X₇ [+1] After After After After O→1 O→2 O→3 O→7 O→1 leads to draw, others lead to X winning Therefore: MIN(0, +1, +1, +1) = 0 🎯 Conclusion: O can force a draw! O should choose position 1 to achieve the best outcome.
Interactive Tree Diagram
flowchart TD Root["O (MIN)
Root
Value: 0"] X1["X (MAX)
After O→1
Value: 0"] X2["X (MAX)
After O→2
Value: +1"] X3["X (MAX)
After O→3
Value: +1"] X7["X (MAX)
After O→7
Value: +1"] Root --> X1 Root --> X2 Root --> X3 Root --> X7 X1 --> L1["Draw
0"] X2 --> L2["X wins
+1"] X3 --> L3["X wins
+1"] X7 --> L4["X wins
+1"] classDef minNode fill:#dc3545,stroke:#333,stroke-width:2px,color:#fff classDef maxNode fill:#28a745,stroke:#333,stroke-width:2px,color:#fff classDef leafNode fill:#6c757d,stroke:#333,stroke-width:2px,color:#fff class Root minNode class X1,X2,X3,X7 maxNode class L1,L2,L3,L4 leafNode
Strategic Discovery!

O has a way to force a draw! While most moves lead to X winning, O can play at position 1 to achieve a draw.

From the minimax perspective, O should choose position 1 - it's the only move that doesn't result in X's victory. This demonstrates the power of systematic game analysis in finding optimal strategies.

Node Type Position Possible Outcomes Operation Result
MAX After O→1 [0, 0, 0] MAX(0, 0, 0) 0
MAX After O→2 [+1, 0, 0] MAX(+1, 0, 0) +1
MAX After O→3 [+1, 0, 0] MAX(+1, 0, 0) +1
MAX After O→7 [+1, 0, 0] MAX(+1, 0, 0) +1
MIN Root [0, +1, +1, +1] MIN(0, +1, +1, +1) 0
Strategic Analysis

Why can O force a draw at position 1? When O plays at position 1, it blocks X's main diagonal threat (1-5-9). This prevents X from achieving any immediate winning combinations, forcing all subsequent moves to result in draws.

O's best move: Position 1 is clearly optimal, achieving a draw (0) while all other moves {2, 3, 7} allow X to win (+1). This demonstrates how a single strategic move can change the game's outcome.

  • 3. Apply Alpha-Beta pruning:
    • Start with α = -∞, β = +∞ at the root
    • Update α and β values as you traverse the tree
    • Identify and mark branches that can be pruned
    • Count how many leaf evaluations were skipped
Alpha-Beta Pruning Solution
1 Initialize at Root (MIN node):
Start with bounds: α = -∞, β = +∞
Explore O's moves left-to-right: {1, 2, 3, 7}
2 Explore Branch 1: O plays at position 1
X's first option: position 2

Game outcome: 0 (Draw)

Update: α = MAX(-∞, 0) = 0

α = 0, β = +∞
X's second option: position 3

Game outcome: 0 (Draw)

No change to α (still 0)

No change to bounds
X's third option: position 7

Game outcome: 0 (Draw)

No change to α (still 0)

X chooses MAX = 0

Branch 1 result: 0

Update β: β = MIN(+∞, 0) = 0

Current bounds: α = 0, β = 0

3 Explore Branch 2: O plays at position 2
X's first option: position 1

Game outcome: +1 (X wins)

Since +1 > β = 0, MIN won't choose this branch

β-cutoff: prune remaining X moves
X's remaining options: {3, 7}

🚫 PRUNED!

Since X already found +1 > β = 0

MIN won't explore this branch further

Branch 2 result: +1 (pruned after first move)

4 Pruning Analysis for Remaining Branches:
Enhanced Pruning Opportunity

Since β = 0 (MIN's current best from Branch 1), and X can immediately get +1 in branches 3 and 4 by playing position 1, both branches can be entirely pruned:

  • Branch 3 (O→3): X gets +1 > β = 0 → Pruned immediately
  • Branch 4 (O→7): X gets +1 > β = 0 → Pruned immediately
Branch 3: O→3

X's first move would be position 1 → +1

🚫 ENTIRELY PRUNED!

Since +1 > β = 0, MIN rejects this branch
Branch 4: O→7

X's first move would be position 1 → +1

🚫 ENTIRELY PRUNED!

Since +1 > β = 0, MIN rejects this branch
Step Node α β Action Pruned?
1 Root -∞ +∞ Initialize No
2 O→1, X→2 0 +∞ Evaluate leaf: 0 No
3 O→1, X→{3,7} 0 0 Complete branch: 0, β = 0 No
4 O→2, X→1 0 0 Evaluate leaf: +1 > β Yes (β-cutoff)
5 O→3 0 0 X would get +1 > β immediately Yes (entire branch)
6 O→7 0 0 X would get +1 > β immediately Yes (entire branch)
Pruning Efficiency Analysis
Standard Minimax:
  • Branch 1: 3 leaf evaluations
  • Branch 2: 3 leaf evaluations
  • Branch 3: 3 leaf evaluations
  • Branch 4: 3 leaf evaluations
  • Total: 12 evaluations
Alpha-Beta Pruning:
  • Branch 1: 3 leaf evaluations
  • Branch 2: 1 leaf evaluation (pruned)
  • Branch 3: 0 leaf evaluations (entirely pruned)
  • Branch 4: 0 leaf evaluations (entirely pruned)
  • Total: 4 evaluations

Efficiency gain: 67% reduction in leaf node evaluations while producing the identical result (0)!

✂️ Alpha-Beta Pruning Tree
Alpha-Beta Search (α = -∞, β = +∞) O [α=-∞, β=+∞ → β=0] → 0 / | \ \ / | \ \ X₁ [0] X₂ [✂️] X₃ [✂️] X₇ [✂️] ✓ Full ✗ Partial ✗ Pruned ✗ Pruned Step-by-step execution: 1. Explore O→1: All X moves give 0 → Update β = 0 2. Explore O→2: X gets +1 > β = 0 → β-cutoff! 3. Explore O→3: X would get +1 > β → Prune entire branch! 4. Explore O→7: X would get +1 > β → Prune entire branch! 🚀 Result: Optimal answer (0) with 67% fewer evaluations!
Alpha-Beta Pruning Visualization
flowchart TD Root["O (MIN)
α=-∞, β=+∞→0
Value: 0"] X1["X (MAX)
After O→1
Value: 0
✅ Fully Evaluated"] X2["X (MAX)
After O→2
Value: +1
✂️ Pruned Early"] X3["❌ PRUNED
After O→3
✂️ Skipped"] X7["❌ PRUNED
After O→7
✂️ Skipped"] Root --> X1 Root --> X2 Root -.-> X3 Root -.-> X7 X1 --> L1["All draws
0"] X2 --> L2["First eval: +1
Then pruned"] classDef minNode fill:#dc3545,stroke:#333,stroke-width:2px,color:#fff classDef maxNode fill:#28a745,stroke:#333,stroke-width:2px,color:#fff classDef prunedNode fill:#ffc107,stroke:#333,stroke-width:2px,color:#333 classDef leafNode fill:#6c757d,stroke:#333,stroke-width:2px,color:#fff class Root minNode class X1,X2 maxNode class X3,X7 prunedNode class L1,L2 leafNode
Why Pruning Works Here

In this specific game state, pruning is particularly effective because:

  • Dominant strategy: X's move to position 1 wins in almost every branch
  • Early discovery: We find X's winning move early in the search
  • Predictable outcomes: Once we know X can guarantee +1, remaining branches are less interesting
  • Bound tightening: β quickly converges to +1, enabling more aggressive pruning
Key Findings from This Exercise
🎯 Strategic Insights:
  • Position matters: X's initial setup created an unbeatable advantage
  • Forced sequences: Some game positions have predetermined outcomes
  • Threat analysis: X's diagonal threat dominated all variations
  • Optimal play: Both algorithms found the same result (X wins)
⚙️ Algorithmic Insights:
  • Same results: Minimax and Alpha-Beta gave identical outcomes
  • Efficiency matters: Alpha-Beta saved up to 58% of computations
  • Early termination: Pruning worked well due to dominant strategies
  • Practical value: Faster search enables deeper game tree analysis
From Tic-Tac-Toe to Real AI

This exercise demonstrates principles used in real game AI systems:

🏆 Chess Engines:
  • Stockfish uses alpha-beta with advanced pruning
  • Deep Blue defeated world champion using minimax variants
  • Modern engines search 20+ moves deep efficiently
🎮 Game Development:
  • Strategy games use minimax for AI opponents
  • Puzzle games apply adversarial search principles
  • Real-time games use time-bounded alpha-beta
Educational Takeaways
✅ What Students Learned:
  • How abstract algorithms apply to concrete games
  • Why mathematical optimization matters in AI
  • How to systematically analyze strategic scenarios
  • The relationship between search depth and intelligence
🎯 Skills Developed:
  • Game tree construction and visualization
  • Minimax algorithm implementation
  • Alpha-beta pruning optimization techniques
  • Strategic thinking in adversarial environments

Continue Learning