AlgoViz

Design tic-tac-toe

easy 70 min

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

Watch lesson4:32

How to approach it

What this question tests, where it gets hard, and how to spend the time.

Read the question

What this problem tests

Everybody can play it, so nobody is impressed by a program that works. It tests whether rules have owners, whether a wrong call can do harm, and how calmly you take the next three wishes.

Four moves

  1. 1

    Ask about size and winning

    These two decide the win check.

  2. 2

    Give each rule an owner

    Taken: board. Turn and end: game. Line: counts.

  3. 3

    Write play

    Questions, then changes, then the result: win before full.

  4. 4

    Trace the bad cases

    Wrong turn, taken, after the end, the winning ninth move.

Time

Spend the 45 minutes

MinutesDoHave at the end
0 to 4QuestionsAny size, full line, undo
4 to 9Owners of rules, rejected nounsFour types
9 to 15Class diagramThe pairs put and clear, add and remove
15 to 28CodeBoard, counts, game, about 70 lines
28 to 33TraceWin, draw, refusal, undo after a win
33 to 45WishesComputer, k in a row, two phones
You will finish early. That is normal for this problem, and the wishes are where levels are separated.

Memory

SAND

  • S: Squares store

    The board refuses a taken one and knows no rules.

  • A: Ask before act

    All checks before all changes.

  • N: N on a line

    Count, four lines at most, win before full.

  • D: Do has an undo

    Each change has an opposite with the same owner.