Tic-Tac-Toe Game
A React exercise in expressing the rules of a two-player game with a small, predictable state model.

I used tic-tac-toe to practice keeping game rules and the interface in sync. I wanted a blocked click to leave the turn unchanged, a completed line to stop play, and reset to put the board and the turn back in the same starting state.
Deriving the outcome from the board
React stores the nine cells and whose turn it is. The winner is computed from eight lines, with a guard so empty squares cannot count. A draw is a full board without a winner. The display can derive its status from that same information.
const calculateWinner = (board) => {
const lines = [
[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 (const [a,b,c] of lines) {
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return board[a];
}
}
return null;
};
const winner = calculateWinner(squares);
const isDraw = !winner && squares.every(v => v !== "");
const handleSquareClick = (index) => {
if (winner || isDraw) return; // stop after game ends
if (squares[index]) return; // stop overwriting
const next = [...squares];
next[index] = player;
setSquares(next);
setPlayer(prev => (prev === "X" ? "O" : "X"));
};
const handleReset = () => {
setSquares(Array(9).fill(""));
setPlayer("X");
};Guarding state changes at the action
The handler checks whether the game has ended or the square is taken before creating a new board array. Only a valid move writes the new marker and changes the turn. The button grid renders the array, so the visual board uses the same state as the rules.
What I learned
Representing the board once made the interaction easier to reason about. Winner, draw, and next turn could be checked against the same cells rather than stored as separate flags.