← Home

❌ Tic-Tac-Toe

Play the classic three-in-a-row game against a computer opponent.

Your turn (X)

What is this tool?

Tic-tac-toe is one of the oldest and most universally recognized strategy games in the world. Also known as noughts and crosses in British English, the game is played on a simple three-by-three grid where two players take turns placing their marks, traditionally X and O, in empty cells. The first player to align three of their marks in a row horizontally, vertically, or diagonally wins the game. If all nine cells are filled without either player achieving three in a row, the game ends in a draw. Tic-tac-toe has a remarkably long history, with variations dating back to ancient Egypt around 1300 BC and similar games played in the Roman Empire. The modern pencil-and-paper version became popular in the twentieth century as a simple game that could be played anywhere with just a piece of paper and a pen. Despite its simplicity, tic-tac-toe has interesting mathematical properties. The game has been solved, meaning that with perfect play from both sides, the game always ends in a draw. There are 255,168 possible game sequences, but only 138 unique final board positions. The game is often used as a teaching tool to introduce children to strategic thinking, turn-taking, and the concept of looking ahead to anticipate an opponent moves. It is also one of the first programming projects many computer science students tackle, as implementing a tic-tac-toe AI teaches fundamental concepts like game trees, evaluation functions, and the minimax algorithm.

How it works

The game runs on a simple state machine that tracks the board as a grid of nine cells. You play as X and always move first, giving you a slight strategic advantage. After each of your moves, the computer AI evaluates all available empty cells and selects the best move. The AI uses a strategy that first checks if it can win immediately on the current turn, then checks if it needs to block your winning move, then prefers the center cell if available, then prefers corners, and finally falls back to any remaining open cell. After each move, the game checks all eight possible winning lines for three matching marks in a row. If a winning line is found, the game announces the winner. If all cells are filled without a winner, the game declares a draw. The Reset button clears the board for a new game.
Ad

How to use

  1. You play as X and always go first. Click any empty cell in the three-by-three grid to place your mark.
  2. After your move, the computer automatically places its O mark using a strategy that blocks your winning moves and tries to win.
  3. Continue taking turns. The goal is to get three of your X marks in a row horizontally, vertically, or diagonally.
  4. Watch the status text above the grid to see whose turn it is and whether the game has been won or ended in a draw.
  5. Press Reset at any time to clear the board and start a new game. Try different opening moves to find the best strategy.

Frequently Asked Questions

Frequently Asked Questions

Can you always win at tic-tac-toe?
Against perfect play, tic-tac-toe always ends in a draw. However, if your opponent makes a mistake, you can win. The strongest opening move is the center cell, which is part of four winning lines. If you take the center and your opponent does not take a corner, you can force a win. If you take a corner first and your opponent does not take the center, you can also force a win.

Is it better to start in the center or a corner?
Starting in the center is statistically the strongest opening move because the center is part of four winning lines out of eight possible. Starting in a corner is the second best option. Starting on an edge is the weakest opening. Against the computer AI, the center gives you the best chance if the AI makes a suboptimal response.

Why does the computer never lose?
A well-programmed tic-tac-toe AI using optimal strategy will never lose. At worst it draws. This is because tic-tac-toe is a solved game, meaning every possible move sequence has been analyzed. Our AI prioritizes winning moves, then blocking your threats, then taking strong positions, which means it plays near-optimally and is very difficult to beat.

What is the best strategy to beat the computer?
Take the center first. If the computer takes a corner, take the opposite corner. Create a fork situation where you have two winning threats simultaneously, which the computer cannot block both. However, against a strong AI, most games will end in a draw. Experiment with different opening moves to find gaps in the strategy.

Tips & Advice

The center cell is the most powerful position on the board because it is part of more winning lines than any other cell. Always consider creating a fork, which is a move that gives you two winning threats at once, forcing your opponent to block one while you complete the other. Beware of your opponent creating the same trap against you. If you go first, starting in the center or a corner gives you the best chance. Starting on an edge puts you at a disadvantage. Think two moves ahead and always check if your opponent is one move away from winning so you can block. The game is a great exercise in logical thinking and is suitable for all ages as an introduction to strategy games.

Related Fun & Misc Tools

Ad