Unbeatable Tic-Tac-Toe

A tic-tac-toe game with an unbeatable AI opponent using the minimax algorithm with alpha-beta pruning.

Python

The Problem

I wanted to implement a game-playing AI that could never lose — a good exercise for understanding adversarial search, game trees, and optimization of brute-force algorithms.

The Solution

I built an unbeatable tic-tac-toe game in Python using the minimax algorithm.

  • Minimax search — Exhaustively explores all possible game states to choose the optimal move, guaranteeing the AI never loses.
  • Alpha-beta pruning — Eliminates branches that can't affect the final decision, reducing the search space significantly.
  • Terminal UI — Clean command-line interface for human vs. AI gameplay.

What Went Wrong

The initial implementation without pruning explored all 255,168 possible game sequences on every move, causing noticeable lag on the AI's first move when the board is mostly empty.

The fix: Alpha-beta pruning reduced the explored nodes by ~60% on average. Combined with move ordering (center first, then corners, then edges), the AI responds instantly even on the first move.

Results

  • Mathematically unbeatable — AI always wins or draws
  • Alpha-beta pruning for instant response times
  • Clean exercise in adversarial search and game tree optimization

Interested in working together?

Let's Talk