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", ornull.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
checkWinnermust detect all 8 winning lines (3 rows, 3 cols, 2 diagonals).minimaxmust return+10for an X win,-10for an O win,0for a draw.bestMovereturns 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.