Tic-Tac-Toe
Play tic-tac-toe against an AI opponent that uses minimax for an unbeatable hard mode.
Written by Golam Rabbani, Founder & Lead Engineer
How to use this tic-tac-toe
- Pick whether you play as X (move first) or O (CPU moves first).
- Choose a difficulty: Easy plays random moves, Medium mixes random and optimal, Hard runs full minimax and never loses.
- Click or tap any empty cell on the 3x3 board to place your mark.
- The CPU plays automatically after a short delay; the result panel shows the winner or "Draw".
- Press New Round to clear the board, Reset to wipe the running score, or Copy Score to put the W-L-D summary on the clipboard.
About this tic-tac-toe
Tic-Tac-Toe is the classic 3-by-3 game where you and an opponent take turns placing X or O, trying to line up three of your marks in a row, column, or diagonal. This tool plays you against a CPU that uses minimax — a recursive search that looks at every legal continuation and assumes each side plays optimally — so on Hard the CPU can be drawn but never beaten.
Difficulty changes how often the CPU plays optimally. Easy picks uniformly at random from the empty cells, Medium plays the minimax-optimal move 60% of the time and a random move otherwise, and Hard always plays the minimax move. Score tallies for wins, losses, and draws persist across rounds within a single session and reset only when you press Reset.
A worked example: you play X on Hard and open in the centre (cell 5). The CPU as O plays minimax and answers in a corner — say cell 1. You play another corner (cell 9). With perfect play from here the game is a forced draw: O blocks, X blocks, and neither side can build an unstoppable fork. Try the same opening on Easy and you can usually win by setting up a double threat the random CPU fails to block. The score row updates after each finished round so you can track how often you draw the unbeatable opponent versus how often you win against Easy.
FAQ
- Can I beat the Hard CPU?
- No — Hard uses minimax with perfect lookahead, so the best outcome you can force is a draw. Easy and Medium are beatable because they play sub-optimal moves part or all of the time.
- Why does the CPU pause before moving?
- A short 250 ms timeout makes the move readable instead of appearing instantly. The delay is cosmetic; the underlying minimax search returns in well under a millisecond on a 9-cell board.
- What does the running score track?
- Three counters: your wins, CPU wins, and draws, accumulated across rounds in the current session. Pressing Reset zeroes all three. Refreshing the page also clears the score because nothing is persisted.
- Does playing as O change anything?
- Yes — when you pick O the CPU plays X and moves first, which typically gives the CPU the small first-move advantage at Hard. Centre and corner openings from X are the strongest, which is reflected in the resulting minimax search.
- Is the board keyboard accessible?
- Yes. Each cell is a focusable button so you can Tab between cells and press Enter or Space to place your mark.
- Does the tool send my moves anywhere?
- No. The whole game runs in your browser; nothing is uploaded and the CPU does not call any external API.