Facebook Pixel

How to Build a Tic-Tac-Toe Game in an Interview

Tic-tac-toe with win detection and score tracking. Here is how to build it in 90 minutes.

How to Build a Tic-Tac-Toe Game in an Interview

Tic-tac-toe tests game logic and state management. Here is how to build it.

Requirements

  • 3x3 grid.
  • Two players (X and O), alternating turns.
  • Win detection (3 in a row, column, or diagonal).
  • Draw detection.
  • Reset button.
  • Score tracking.

Plan (5 minutes)

  1. HTML: 3x3 grid of buttons.
  2. State: board array (9 elements), current player, scores.
  3. Events: click a cell to place.
  4. Logic: check win after each move.

Build Core (40 minutes)

HTML

<div class="board" id="board"></div> <p id="status">Player X's turn</p> <button onclick="reset()">Reset</button> <p>X: <span id="scoreX">0</span> | O: <span id="scoreO">0</span> | Draws: <span id="scoreDraw">0</span></p>

JavaScript

let board = Array(9).fill(null); let currentPlayer = "X"; let gameOver = false; const winPatterns = [ [0,1,2], [3,4,5], [6,7,8], // rows [0,3,6], [1,4,7], [2,5,8], // columns [0,4,8], [2,4,6], // diagonals ]; function checkWin() { for (const [a, b, c] of winPatterns) { if (board[a] && board[a] === board[b] && board[a] === board[c]) { return board[a]; } } if (board.every((cell) => cell)) return "draw"; return null; } function handleClick(index) { if (board[index] || gameOver) return; board[index] = currentPlayer; render(); const result = checkWin(); if (result === "draw") { gameOver = true; updateScore("draw"); } else if (result) { gameOver = true; updateScore(result); } else { currentPlayer = currentPlayer === "X" ? "O" : "X"; } updateStatus(result); }

CSS

.board { display: grid; grid-template-columns: repeat(3, 80px); gap: 4px; } .cell { width: 80px; height: 80px; font-size: 32px; border: 1px solid #ccc; cursor: pointer; } .cell:disabled { cursor: default; }

Edge Cases (20 minutes)

  • Clicking an already-filled cell (ignore).
  • Clicking after game over (ignore).
  • Highlight winning cells.
  • Score tracking.
  • Undo last move (optional).

The Takeaway

Build tic-tac-toe: HTML (3x3 grid), JavaScript (board array, alternating players, win patterns, check win/draw), CSS (grid layout). Handle edge cases (filled cells, game over, winning highlight). This tests game logic and state management.

Create a 3x3 grid. Use an array of 9 elements for the board. Alternate players (X/O). After each move, check 8 win patterns (3 rows, 3 columns, 2 diagonals). Detect draw when all cells are filled. Add reset and score tracking.

Check 8 win patterns: [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]. For each pattern, if board[a] === board[b] === board[c] and board[a] is not null, that player wins.

Check if all cells are filled (board.every(cell => cell !== null)) and no winner was detected. If so, it is a draw. Check for a win first, then check for a draw.

Clicking an already-filled cell (ignore), clicking after game over (ignore), highlighting the winning cells, score tracking (X wins, O wins, draws), and reset button to start a new game.

When a win is detected, store the winning pattern (the three indices). Add a CSS class (e.g., 'winning') to those cells in the render function. Style .winning with a different background color.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.