Skip to content

FILE 002

Unbeatable Tic-Tac-Toe

DECLASSIFIED

Browser game with an unbeatable Minimax AI

Role
Developer (personal project)
Year
2022
Stack
  • JavaScript
  • HTML
  • CSS

01Overview

A tic-tac-toe game in the browser where you play O against an AI that never loses. Before every move, the AI uses the Minimax algorithm to play out every possible rest of the game and picks the move with the best guaranteed result.

You vs. AI · Minimax search over the full game tree · win, loss and draw detection with the winning line highlighted · replay · vanilla JavaScript, no dependencies.

02The problem

Hand-written rules for a tic-tac-toe bot (“block when they have two in a row”) easily miss a case. The goal: an opponent that never loses because it looks ahead at every possible game instead of following rules.

03My approach

  1. A.01

    Score each finished game: +10 if the AI (X) wins, −10 if you (O) win, 0 for a draw.

  2. A.02

    Try every free square, then every possible reply, recursively until each game ends.

  3. A.03

    Back up the scores: on its turn the AI takes the highest (MAX); on yours it assumes you take the lowest (MIN).

  4. A.04

    Play the move with the best backed-up score. The 9-square board is small enough to search the whole tree, with no depth limit.

04Key screens

› swipe for all 4 screens

05Technical highlights

Game engine
Minimax · full game tree
Front end
Vanilla JS · HTML · CSS
Platform
Vercel

06Outcome

Because it assumes you always play your best move, the AI can’t be beaten: every game ends in a win for the machine or a draw.