The Minimax Algorithm: How the AI Outthinks You

user

Michal Drobny

Nov 04, 2024

4 min read

Algorithms

Share this:

blog

So, here's the deal: the Minimax algorithm is a decision-making strategy used in two-player games like Tic Tac Toe, where players take turns, and the game has a finite number of moves. The algorithm's goal is simple: maximize your score while minimizing your opponent's.

In Tic Tac Toe, the AI player (let’s call it Player X) tries to maximize its chances of winning while minimizing the chances for the human player (Player O).

How Minimax Works

The algorithm simulates all possible moves from the current state of the game and evaluates the outcome of each move. It essentially plays out the game to the end in its head and chooses the move that leads to the best possible outcome.

Here’s a basic breakdown:

  1. Recursive Exploration: The algorithm looks at all possible moves for both the AI and the human player, simulating the game until it reaches a terminal state (either a win, loss, or draw).
  2. Scoring Terminal States: At each terminal state:
    • A win for the AI gives a positive score.
    • A win for the human player gives a negative score.
    • A draw gives a score of zero.
  3. Backtracking: The algorithm backtracks through the simulated moves, choosing the move with the highest score for the AI and the lowest score for the human player.

The Minimax Algorithm in Action

Let’s take a look at how the Minimax algorithm is implemented in my AiPlayer.php class:

function findBestMove(array $board): ?array
{
	$bestScore = -INF;
	$bestMove = null;

	foreach (range(0, 2) as $row) {
		foreach (range(0, 2) as $col) {
			if ($board[$row][$col] === null) {
				$board[$row][$col] = $this->aiPlayer;
				$score = $this->minimax($board, 0, false);
				$board[$row][$col] = null;

				if ($score > $bestScore) {
					$bestScore = $score;
					$bestMove = [$row, $col];
				}
			}
		}
	}

	return $bestMove;
}
function minimax(array $board, int $depth, bool $isMaximizing): int
{
	$winner = $this->checkWinner($board);

	if ($winner === $this->aiPlayer) {
		return 10 - $depth;
	} elseif ($winner === $this->humanPlayer) {
		return $depth - 10;
	} elseif ($this->isDraw($board)) {
		return 0;
	}

	$bestScore = $isMaximizing ? -INF : INF;

	for ($row = 0; $row < 3; $row++) {
		for ($col = 0; $col < 3; $col++) {
			if ($board[$row][$col] === null) {
				$board[$row][$col] = $isMaximizing ? $this->aiPlayer : $this->humanPlayer;
				$score = $this->minimax($board, $depth + 1, !$isMaximizing);
				$board[$row][$col] = null;

				$bestScore = $isMaximizing? max($bestScore, $score) : min($bestScore, $score);
			}
		}
	}

	return $bestScore;
}

Breaking Down the Code

  1. findBestMove(): This function loops through all possible moves, simulates each move using the Minimax algorithm, and chooses the move with the best score for the AI.
  2. minimax(): This recursive function evaluates all possible outcomes of the game:
    • If the AI is playing (maximizing), it looks for the highest score.
    • If the human is playing (minimizing), it looks for the lowest score.
    • The algorithm uses depth to adjust the scores, penalizing longer paths to victory and rewarding faster wins.

Why Minimax Is Cool (and Tricky)

The beauty of Minimax is that it plays optimally. The downside? It can be computationally expensive, especially for games with a large number of possible moves. Thankfully, Tic Tac Toe is a relatively small game, so it works perfectly here.

This was my first time implementing Minimax in a game, and while it took some brainpower to get it right, the end result was super satisfying. The AI is tough to beat (or almost imposible), and if you play perfectly, it will either beat you or force a draw—no sneaky victories here!

So, there you have it: a deep dive into how the AI in my Tic Tac Toe game works. It’s simple, effective, and makes the game a lot more challenging and fun!

Let me know if you’d like to see more details or if you have any questions about the implementation. And if you ever beat the AI, feel free to brag about it!