Case study · Intro

Design Tic-Tac-Toe

A clean OO model of a turn-based board game — board, players, moves, and win detection — designed so it generalises to N×N or other grid games.

Asked atAmazonMicrosoftAdobe
step 1 / 7
1/2Model the board
boardplayers 2cells n×n
Game
- players
- turn
+ move(r,c)
Board
- cells
- n
+ place(r,c,s)
+ winner()
Player
- symbol: Symbol
Cell
- symbol: Symbol
«enum»
Symbol
X
O
EMPTY
Model the board
1enum Symbol { X, O, EMPTY }
2 
3class Board {
4 Cell[][] cells;
5 int n;
6 void place(int r, int c, Symbol s) { ... }
7 Symbol winner() { ... } // scan rows/cols/diagonals
8}
State
Gameplayers + turn

Game orchestrates the match — it holds the two players, tracks whose turn it is, and exposes move(). It owns the rules and turn order, nothing else.