Design tic-tac-toe 00:00:00 Make a prediction You will order validation, placement and end-state checks so an invalid or final move cannot corrupt the turn sequence. The ninth move fills the final cell and completes a diagonal. If the engine checks board fullness before checking the new line, what wrong result could it return? A draw instead of a win. A full board can contain a winning line. Validate and place the mark, test the relevant lines, then declare a draw only when no winning line exists. 00:00:33 One game, one tide Two players take turns drawing marks on the sand before the tide reaches them. Placing a shell on an already marked square does not become a valid turn merely because it happened last. Track the player, board and terminal status as one state. A rejected action changes none of them; a winning action freezes future moves. Tic-tac-toe in a low-level design round is a referee: an object that stores a grid, allows legal moves in turn, and says when the game is won or drawn. Screens, players and scores are around it. It needs no explanation, so an interviewer sees your habits at once: where input goes, where rules go, what is checked before what. 00:01:18 Validate the move before declaring a win X has marks in the first two squares of the top row. It is X's turn and the final square in that row is empty. Validate the turn, the cell and that the game is active. Place one X and check rows, columns and diagonals affected by the move. The completed row ends the game. A later O move must be rejected without altering the board. Keep the move rule and end-state rule in the same trace. Ask the next player to choose an occupied coordinate. Neither turn nor win counters should advance. Repeat with a valid square completing a row. This time the move, turn result and terminal status belong together; checking the win before validating the move can create a false winner. 00:02:04 Check a full-board win before calling a draw The final empty cell can complete a winning line. If the engine checks board fullness first, it may announce a draw incorrectly. Test a game whose winning move is the ninth move, then test a genuinely full board with no winning line. Design a board where the final empty square also completes a line. Filling it makes the board full, but the correct result is a win. Test the win before falling back to a draw. Now alter one adjacent marker and verify that a full board without a line does produce a draw. Checking counts without validating the turn can allow one player to move twice and manufacture a win. Assert the unchanged turn after an invalid attempt. For a fixed three-by-three board, scanning a few lines may be clearer than maintaining extra counters. The React tutorial uses this game to show how saving previous board states makes time travel possible. The board game framework in the chapter can report an invalid move separately from the end of the game. Neither a rejected move nor a history inspection should silently advance the active player. 00:03:14 Try an interview follow-up Where do you put the win check, and why? I put the rule where it can observe the newly placed marker and the lines through its position. A move affects one row, one column and at most two diagonals. For a fixed three-by-three exercise a board method is reasonable. A separate checker helps when the same storage supports several sets of winning rules. Play a game until the first player completes a row. Then send another legal-looking coordinate from the second player. Reject it because the game has ended. Check that the board, turn and winner all stay unchanged. This catches designs that validate the square but forget the game lifecycle. 00:03:59 What to take with you The game enforces turns and the end, the board stores squares and refuses taken ones, counters see a full line in at most four steps. All questions come before all changes, the win before the draw. A move is an object, so it can be taken back by opposites, replayed, stored and sent. Counting works because a win is a full line; for k in a row walk from the last move. A computer can know all 5,478 positions.