Design tic-tac-toe
easy 70 minModel a game everybody knows: marks on a grid, turns, a full line wins.
Watch lesson4:32
Check yourself
One question at a time. Each answer explains why.
Question 1 of 12
A design checks for a full board before it checks for a line. Which game goes wrong?
n = 4, X plays the square (1, 2). How many counters change?
Why are all checks made before any change?
X wins. Somebody calls undo and the code sets turn = turn.other. Whose turn is it, and whose should it be?
Why does LineCounts.remove use the same _lines method as add?
Someone stores +1 for X and -1 for O in one number for each line. Which extension breaks it?
The rule becomes: five in a row on a board of fifteen. What replaces the counters?
Plain minimax visits 549,946 nodes, with a cache 5,478. What explains the difference?
A computer player receives the real Board object. What is the main problem?
A server holds 100,000 games with one move in five seconds each. How many moves a second?
Why is loading a saved game by replaying its moves through play a good idea?
Size 1: X plays (0, 0). What is the status, and how many counters are 1?
Pick an answer to see why.
Not quite. The ninth move fills the board and may complete a line. It would be called a draw.
Not quite. Row 1, column 2, and 1 + 2 = 3 = n - 1, so the up diagonal. It is not on the down diagonal.
Not quite. If the last check can fail after the first change, somebody has to undo half a move.
Not quite. After a win the turn stayed with X. Switching gives O. The move that was taken back was X's, so X moves.
Not quite. Two lists would have to agree for ever. If diagonals are dropped, one change is enough.
Not quite. There is no third sign. Size and undo work the same way.
Not quite. Counting forgets where the marks are. The walk looks at up to 32 squares and needs no memory.
Not quite. 5,478 is the number of different positions. Merging mirrored ones would give 765.
Not quite. A player should get a copy and return a wish. Otherwise rules live in its good manners.
Not quite. 100,000 / 5 = 20,000. At a few microseconds each it is a small part of a core.
Not quite. Loading and checking are the same code. A stored board could contain anything.
Not quite. The one square is row 0, column 0 and both diagonals. All four must be counted, or undo goes below zero.
Flashcards
Say your answer, then reveal it.
Card 1 of 13
Who owns which rule?
Board: inside and empty squares. Game: turn and end. LineCounts: full lines. Players: none.
Say your answer, then reveal it.
How many lines can one move change?
At most four: row, column and two diagonals.
Say your answer, then reveal it.
Why is counting right?
A square is taken once, so the count of a line is the number of squares with this mark. n means all.
Say your answer, then reveal it.
When is counting wrong?
When a win is not a full line: k in a row. Counting forgets where.
Say your answer, then reveal it.
Win or full first?
Win. The last square can do both.
Say your answer, then reveal it.
What does undo set the turn to?
The mark of the move. After a win the turn had not passed.
Say your answer, then reveal it.
Why must add not return at the first full line?
The other counters would be missing, and undo would take from them.
Say your answer, then reveal it.
Why does play take the mark?
A wrong or repeated call is noticed and refused.
Say your answer, then reveal it.
What does a computer player get and give?
A copy in, a wish out. The referee may refuse.
Say your answer, then reveal it.
Games, positions, positions up to symmetry?
255,168, 5,478, 765.
Say your answer, then reveal it.
What is the truth, what is convenience?
The moves are the truth. Board, counts, turn and status can be computed.
Say your answer, then reveal it.
Locks for many games?
One for each game, and a short one for the list. Never both at once.
Say your answer, then reveal it.
Why is [[None] * 3] * 3 a bug?
It is one row, three times. Build each row by itself.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.