Skip to main content
๐ŸŽฎ Game Logic

What you're building

A pure-logic Tic-Tac-Toe engine with an AI opponent that never loses.

The board is a 9-element array of "X", "O", or null. You will implement:

  • checkWinner(board) โ€” returns "X", "O", "draw", or null.
  • minimax(board, isMaximizing) โ€” recursive best-move scorer (X maximises, O minimises).
  • bestMove(board) โ€” returns the index (0-8) of the optimal move for "X".

Requirements

  • checkWinner must detect all 8 winning lines (3 rows, 3 cols, 2 diagonals).
  • minimax must return +10 for an X win, -10 for an O win, 0 for a draw.
  • bestMove returns the index that maximises the minimax score for X.
  • All functions are pure โ€” no side-effects, no mutation of the input board.

Stretch goals

  • Add depth-discounting so the AI prefers faster wins.
  • Add alpha-beta pruning to speed up the search.

What this proves

Minimax is the foundation of chess engines, checkers solvers, and adversarial AI. Implementing it from scratch demonstrates recursion, game-tree search, and backtracking โ€” core interview topics.

โšก 50 XP
solution.jsRuns in your browser
Loading editorโ€ฆ
Tests4 to pass
checkWinner detects row win
checkWinner detects diagonal win
checkWinner detects draw
bestMove blocks opponent win

Run your code to check these.