Python – The Minimax Algorithm: Achieving Perfect Strategy

The Minimax algorithm is a fundamental decision-making rule used in Artificial Intelligence (AI) for two-player, zero-sum games, such as Chess, Checkers, and, in our case, Tic-Tac-Toe. It works by exploring every possible future state of the game to find the move that maximizes the current player’s gain while assuming the opponent plays optimally to minimize that gain.

Football is a zero-sum game as well as tic-tac-toe. But AI cannot simulate it that well.

The Core Principle: Maximizing Gains, Minimizing Losses

The name “Minimax” perfectly describes its function:

  • MAXimizing Player (e.g., the AI): Seeks to choose a move that leads to the highest score possible.
  • MINimizing Player (e.g., the Human): Seeks to choose a move that leads to the lowest score for the Maximizing Player.

The entire process is a recursive simulation, creating a huge decision tree, where the algorithm predicts the opponent’s “best” (worst for the AI) response at every turn.

How Minimax Works in Code
The provided Python code illustrates this concept through two main functions: minimax and get_best_move.

  • The Scoring System (Evaluation)

The algorithm first assigns a score to every possible end-state of the game (the “leaves” of the decision tree):

    • AI Win: Returns +1 (The best possible outcome).
    • Human Win: Returns -1 (The worst possible outcome for the AI).
    • Draw: Returns 0 (A neutral outcome).
  • The minimax Function (The Simulation Engine)

This function is the heart of the AI. It uses recursion to simulate future gameplay by alternating between the two players:

    • Base Cases: The simulation stops when the board is full or a winner is found (returning +1, -1, or 0).
    • Maximizing Step (if is_maximizing): The AI tries every empty square, calls minimax recursively for the opponent, and chooses the move that returns the highest score (best_score = max(score, best_score)).
    • Minimizing Step (else): The algorithm assumes the human will try every move, calls minimax recursively for the AI, and chooses the move that returns the lowest score (best_score = min(score, best_score)). This simulates the optimal counter-strategy.
  • The get_best_move Function (The Decision)

This function acts as the bridge between the simulation and the real game:

    • It iterates through every available move on the actual board.
    • For each move, it calls minimax(board, False) (since after the AI moves, it becomes the human’s minimizing turn).
    • It finds the move that resulted in the highest score (+1 being the highest priority, then 0, then -1).
    • It executes that single, optimal move on the physical board.

By using this recursive lookahead and prioritizing the move that guarantees the best minimum outcome (Minimax), the AI achieves a perfect, unbeatable strategy for simple games like Tic-Tac-Toe. The GitHub code is available here: https://github.com/Vitosh/Python_personal/blob/master/YouTube/047_Tic-Tac-Toe/tic-tac-toe.py

🙂 The whole code is below, if you do not want to click on the GitHub link:

🙂

Tagged with: , ,