A browser Tic-Tac-Toe game written in plain HTML, CSS and JavaScript. Play against a minimax AI at several difficulty levels or against a friend on the same screen, and pick up a saved game later where you left off.
Play it online — no install, no sign-up.
Why Tic-Tac-Toe#
Tic-Tac-Toe is small enough that a computer can search every possible game, which makes it the classic first project for game AI. The interesting question isn't "can the computer win?" — with perfect play, nobody does — but "how do you make an opponent that is fun to play against at different levels?"
What it does#
- Classic gameplay — a clean 3×3 board with clear turn and result indicators
- Single-player — play against an AI opponent
- Two-player — take turns on the same device
- Difficulty levels — from beatable to unbeatable
- Save and load — game state is stored in the browser so you can resume later
The AI: minimax#
The AI uses minimax. For every move it could make, it imagines the rest of the game: it assumes it will always pick its best move and the opponent will always pick theirs, and scores each final position — a win, a loss or a draw. It then plays the move with the best guaranteed outcome.
In simplified form:
function minimax(board, depth, isAiTurn) {
const result = winner(board);
if (result === AI) return 10 - depth; // win sooner is better
if (result === HUMAN) return depth - 10; // lose later is better
if (isFull(board) || depth === maxDepth) return 0;
const scores = emptyCells(board).map((cell) => {
const next = play(board, cell, isAiTurn ? AI : HUMAN);
return minimax(next, depth + 1, !isAiTurn);
});
return isAiTurn ? Math.max(...scores) : Math.min(...scores);
}Subtracting the depth from the score makes the AI prefer faster wins and slower losses, so it doesn't play a winning move "eventually" when it can win now. Alpha-beta pruning skips branches that can't change the decision, which keeps every move instant.
Difficulty that feels different#
A perfect minimax player is unbeatable, which is impressive for about one game. Lower difficulty levels limit how far ahead the AI searches. A shallow search still blocks obvious threats but misses forks set up two moves ahead, so it plays like a reasonable human who can be outsmarted. At the highest level it searches to the end of the game and can't be beaten.
Saving the game#
After every move, the game state — the board, whose turn it is, the mode and the difficulty — is serialized to JSON and written to localStorage. Loading reads it back and redraws the board. Everything stays in the browser, so there is no backend to run and nothing to sign up for.
Architecture#
index.html
├── style.css board layout and states
└── script.js
├── state board, current player, mode, difficulty
├── rules winner detection, available moves
├── ai minimax with alpha-beta pruning
├── render updates the DOM after each move
└── storage save / load through localStorageThe game state is a plain JavaScript object and the single source of truth: every move updates the state, and the board is redrawn from it. Hosted on GitHub Pages.
Results#
- A live demo playable directly in the browser through GitHub Pages
- A minimax AI that is unbeatable at maximum difficulty
- Save and load that works entirely client-side, with no backend
