Skip to content
Preptima
mediumCodingDesignEntryMidSenior

Write a game where two players take turns and either of them can win. What do you model, and where does the win check belong?

Model a Board that only stores marks and rejects illegal squares, and a Game that owns the turn and a status of in progress, won or drawn. The win check belongs inside the Game's move method, testing only lines through the square just played and running before the draw check, so a finished game refuses further moves.

6 min readUpdated 2026-10-09

Answer

Short answer

The win check belongs inside the move. In a machine coding round for tic-tac-toe or any two-player turn game, model a Board that stores marks and rejects illegal squares, and a Game that owns whose turn it is and whether play has ended. Game.play(...) places the mark, checks only the lines through that square for a win, then checks for a draw, then hands over the turn. Once the status is won or drawn, every further move is rejected.

Was this answer helpful?

Likely follow-ups

  • Make the board fifteen by fifteen with five in a row to win. Which parts of your code change, and which do not?
  • Add undo. What do you store, and how does undoing a winning move restore the status?
  • Plug in a computer opponent. Where does it live, and how does it read the board without being able to cheat?
  • Two clients on a network send moves for the same game at once. Where do you serialise them?

Related questions