Why this matters
Tic-tac-toe is one of the oldest and most widely recognized strategy games in the world, played on a simple three-by-three grid where two players alternate placing X and O marks. Despite its simplicity, the game is a solved problem in game theory: with perfect play from both sides, every game ends in a draw. This makes it an excellent teaching tool for concepts like minimax algorithms, game trees, and strategic thinking. The implementation here focuses on the two-player local experience, where you and a friend share the same screen.
The game engine enforces strict turn alternation, ensuring that X always moves first and that neither player can place two marks in a row. Win detection checks all eight possible winning lines after each move: three horizontal rows, three vertical columns, and two diagonals. If all nine squares are filled with no winner, the game registers a draw. Scores persist across rounds within the same session, giving you an ongoing tally of wins for X, wins for O, and draws.
While the game lacks an AI opponent, the two-player format has its own advantages. It is ideal for classrooms, waiting rooms, and casual social settings where two people want a quick, zero-setup activity. The session-based scoring adds a competitive element that encourages rematches. Clicking the new game button resets the board while keeping the cumulative scores, so you can track who is winning across an entire afternoon.
Reference table
| Game element | Detail |
|---|---|
| Grid size | 3 x 3 (9 cells) |
| First player | X always goes first |
| Win conditions | 3 horizontal + 3 vertical + 2 diagonal = 8 total |
| Draw trigger | All 9 cells filled with no winner |
| Score persistence | Wins, losses, and draws tracked within the session |
How to use it
Player X clicks any empty square to place the first mark.
Player O clicks next, and turns alternate automatically with no manual switching required.
The game detects a win or draw immediately after each move and displays the result.
Click the new game button to reset the board for another round while keeping the score tally.
Testing your result
Play a complete game where X wins by filling the top row and confirm that the win is detected immediately after the third mark. Test a draw by filling all nine squares without either player completing a line, and verify that the draw is registered correctly. Try clicking on an already-occupied square to confirm the game rejects the move. After several rounds, check that the win counters for X and O reflect the correct number of victories, and that the draw counter increments only when a game ends with a full board.
Common mistakes
Forgetting that X always goes first in every round, not just the first game.
Assuming scores persist across page refreshes, when they are stored only in the current session.
Expecting an AI opponent, which this version does not include — it is strictly two-player local.
Playing in the center on the first move when the opponent does not know the optimal counter-strategy.
Edge cases and options
The optimal tic-tac-toe strategy is well-documented: the first player should open in a corner or the center, and the second player should respond in the center if the first player opened in a corner. When both players follow optimal strategy, every game is a draw. The game does not enforce optimal play, so beginners can still win against each other by exploiting mistakes. The new game button resets the board state completely, but the cumulative score counters are preserved until the page is refreshed or closed. There is no undo function, which means once a mark is placed, it is permanent for that round.
Real-world use cases
Playing a quick game with a friend during a study break or commute on a shared device.
Teaching basic strategy and turn-taking to young children in a classroom setting.
Demonstrating game theory concepts like the minimax algorithm in a computer science lecture.
Using as a simple interactive example for learning front-end state management.
Frequently asked questions
Q: Who goes first?
A: X always goes first. After a win or draw, click new game and X starts the next round.
Q: Can I play against the computer?
A: Not in this version. This is a two-player local game designed for playing against a friend on the same device.
Q: Are scores saved between sessions?
A: Scores reset when you refresh the page or close the tab. They are stored in the current session only.
Q: Is it possible to always win?
A: With perfect play from both sides, every game ends in a draw. You can only win if your opponent makes a suboptimal move.
Start using it now
Try the Tic-Tac-Toe tool. See also Quiz Game, Guess the Number, and 2048 Game.