AlgoViz

Design tic-tac-toe

easy 70 min

Model a game everybody knows: marks on a grid, turns, a full line wins.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:32

See it run

The referee at work: questions first, then one square, four counters, and the verdict.

Operations

The winning move

Three questions, a square, a count, a verdict.

Step 1 of 5

Asha asks

ashacallergameGameturnXstatusPLAYINGmoves6boardBoardrow 0O . Xrow 1X X .row 2O . OlinesLineCountsX row 12X col 21play(X, 1, 2)

X, row 1, column 2. The caller says who they are.

Step 1: Asha asks

def play(self, mark, row, col):    if self._status is not Status.PLAYING:        raise IllegalMove("the game is over")    if mark is not self._turn:        raise IllegalMove("not your turn")    if not self._board.holds(row, col):        raise IllegalMove("outside")    move = Move(row, col, mark)    self._board.put(move)    self._moves.append(move)    if self._lines.add(move):        self._status = won_by(mark)    elif self._board.full:        self._status = Status.DRAW    else:        self._turn = mark.other    return self._status
Cost
O(1)
Guarantee
One move in the list
Whole board, n = 100
20,200 looks

Everything above the store is a question. Everything below cannot fail.

In the real world

The same tic-tac-toe doing real jobs. Pick one and step through it.

It fills the board and it wins.

Step 1 of 3

One square left

012
0XOX
1OXX
2OO

X moves. The right column already has two X, and so has the down diagonal.

Step 1: One square left

if self._lines.add(move):    self._status = won_by(mark)elif self._board.full:    self._status = Status.DRAW

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • One row, three times

    critical

    The same list is used for all rows. A mark appears in each of them.

    Wrong
    board = [[None] * 3] * 3
    Right
    board = [[None] * 3 for _ in range(3)]
  • Full before win

    critical

    A win with the last square becomes a draw.

    Wrong
    if board.full: status = DRAWelif lines.add(move): status = won
    Right
    if lines.add(move): status = wonelif board.full: status = DRAW
  • elif for the second diagonal

    critical

    The middle is on both. One of them never counts it.

    Wrong
    if r == c: ...elif r + c == n - 1: ...
    Right
    if r == c: ...if r + c == n - 1: ...
  • Switching the turn in undo

    major

    After a win the turn had not passed. Now the other one moves twice.

    Wrong
    self._turn = self._turn.other
    Right
    self._turn = move.mark
  • Half of an index check

    major

    -1 is the last row in Python, and an accident in C++.

    Wrong
    if row < size and col < size:
    Right
    if 0 <= row < size and 0 <= col < size:
  • Input inside the rules

    major

    No test without a keyboard, and no second screen without a copy of the rules.

    Wrong
    row, col = map(int, input().split())
    Right
    def play(self, mark, row, col): ...

Interview tips

  • Open with

    • Any size?
    • Full line, or k in a row?
    • Undo, computer, network?
  • Reject aloud

    • A cell is a value.
    • A player is a caller.
    • A row is a number.
  • Prove it

    • The ninth move.
    • Undo after a win.
    • All 255,168 games.
  • Never

    • Print in a rule.
    • The real board for players.
    • A look at all lines for any n.