DEV Community

Lucian (LKB)
Lucian (LKB)

Posted on

I tried to beat my own Tic-Tac-Toe AI 1,270 ways. It never lost.

Everyone says a minimax Tic-Tac-Toe bot is "unbeatable." I wanted a number instead of a vibe — so I benchmarked the exact engine that ships in my game. The code and the harness are open source; every figure below reproduces with node benchmark.js.

Three things I measured: whether it can actually lose, how much alpha-beta pruning really saves, and whether bigger boards crack it.

1. The proof isn't a sample — it's the whole game tree

On a 3×3 board, Hard mode runs a full depth-9 minimax search, so its reply to any position is deterministic. That means I don't have to sample games — I can enumerate every reachable one: every move a human could make, answered by the AI.

Playing second, exactly as it does in the game:

  • 569 reachable game lines → 386 AI wins, 183 draws, 0 losses.

Playing first:

  • 73 lines → 71 wins, 2 draws, 0 losses.

Across all 642 possible 3×3 games, it never loses. On 3×3, "unbeatable" isn't a claim — it's the entire game tree.

2. Alpha-beta earns its keep

Minimax alone is wasteful: it scores branches that can't possibly change the decision. Alpha-beta pruning cuts them the moment that's provable. Choosing the opening move at full depth:

Search Node visits
Plain minimax 549,945
With alpha-beta 36,528

That's a 93% reduction — identical answer, ~1/15th the work. It's why Hard mode replies instantly.

3. Does it break on bigger boards? I expected it to. It didn't.

The engine caps its search depth as the board grows (9 → 7 → 5 → 3) and only needs 4-in-a-row past 3×3. A capped horizon should leave a crack. So I ran the Hard AI against random and greedy opponents on 4×4, 5×5, and 6×6:

Board vs random vs greedy
3×3 91.8% W · 8.2% D · 0 L draw · 0 L
4×4 72.5% W · 27.5% D · 0 L draw · 0 L
5×5 96.7% W · 3.3% D · 0 L draw · 0 L
6×6 100% W · 0 L win · 0 L

0 losses across 628 simulated games, on top of the exhaustive 3×3 proof. What actually changes with board size isn't losing — it's that forcing a win gets harder, so results drift toward draws.

Why it holds up: the engine always blocks an immediate threat before it searches, and 4-in-a-row stays defensible within the depth cap. The genuine fragility is theoretical — a player who can set up a fork beyond the AI's horizon — which is exactly why the full game offers boards up to 10×10.

Takeaways

  • If a game is small and its policy is deterministic, enumerate — don't sample. A proof beats a big sample size.
  • Alpha-beta's payoff on real trees is bigger than the textbook "it helps" — here, 93%.
  • Benchmark your own claims. I set out to find where "unbeatable" breaks and instead measured how it holds.

Engine + benchmark (run it yourself): github.com/lucian-devops/tictactoe-ai
Play it: lkforge.com/games/tictactoe

Top comments (0)