CARDIAC › Tips and tricks › Room
Owe the bonus; don't look ahead
Say you want to score a game of bowling. Each card is one roll, the number of pins it knocked down, and the program outputs the running total after each frame, the way a scoresheet shows it.
A strike scores 10 plus the next two rolls, and a spare scores 10 plus the next one, so a frame's score depends on rolls that haven't happened yet. The usual answer is to keep every roll in an array, up to 21 of them, and look ahead. If roll i is a strike, add 10 and rolls i + 1 and i + 2, and move on one roll; otherwise move on two. On CARDIAC the array costs 21 cells, and each look into it is a CLA built by adding i to CLA 78. There's a catch, too. No card says where the game ends, so reading the rolls in means working out the frames once already, just to know when to stop.
cell contents notes
10 860 JMP 60 cells 10-18: read frames 1-9, one roll for a strike and two otherwise
11 776 SUB 76
12 314 TAC 14
13 815 JMP 15
14 860 JMP 60
15 170 CLA 70
16 200 ADD 00
17 670 STO 70
18 310 TAC 10
19 860 JMP 60 cells 19-25: the tenth frame: two rolls, and a third after a strike or a spare
20 673 STO 73
21 860 JMP 60
22 273 ADD 73
23 776 SUB 76
24 326 TAC 26
25 860 JMP 60
26 178 CLA 78 cells 26-37: copy rolls i, i+1 and i+2 into cells 73-75; cell 26 is CLA 78 + i
27 673 STO 73
28 126 CLA 26
29 200 ADD 00
30 626 STO 26
31 634 STO 34
32 200 ADD 00
33 636 STO 36
34 100 CLA 00
35 674 STO 74
36 100 CLA 00
37 675 STO 75
38 173 CLA 73 cells 38-43: a strike scores 10 + the next two rolls, and the frame is one roll
39 776 SUB 76
40 344 TAC 44
41 174 CLA 74
42 275 ADD 75
43 851 JMP 51
44 136 CLA 36 cells 44-50: otherwise the frame is two rolls; a spare scores 10 + the next roll, an open frame its two rolls
45 626 STO 26
46 173 CLA 73
47 274 ADD 74
48 776 SUB 76
49 351 TAC 51
50 275 ADD 75
51 276 ADD 76 cells 51-59: add the 10, add the frame to the score and output it, ten times
52 272 ADD 72
53 672 STO 72
54 572 OUT 72
55 171 CLA 71
56 200 ADD 00
57 671 STO 71
58 326 TAC 26
59 900 HRS 00
60 199 CLA 99 cells 60-68: read a roll into the array: copy the return address, step the STO in cell 67, and leave the roll in the accumulator
61 668 STO 68
62 069 INP 69
63 167 CLA 67
64 200 ADD 00
65 667 STO 67
66 169 CLA 69
67 677 STO 77
68 800 JMP 00
69 000 the roll
70 -009 frames 1-9 still to read
71 -010 frames still to score
72 000 the score
73 000 rolls i, i+1 and i+2
74 000
75 000
76 010
78 000 cells 78-98: the rolls, up to 21
79 000
80 000
81 000
82 000
83 000
84 000
85 000
86 000
87 000
88 000
89 000
90 000
91 000
92 000
93 000
94 000
95 000
96 000
97 000
98 000
Step through the natural way in Cardigan
But a frame never waits on more than the next two rolls, so what it needs isn't the rolls. It needs to know what it's still owed. Read the rolls one at a time, and let a strike or a spare wait until its bonus rolls have come in. The moment its last roll arrives, the frame is done and its score goes on the sheet.
Two cells carry everything from one roll to the next. Cell 46 holds the frame that's waiting on this very roll, as its score so far less 10, and it's negative when no frame is waiting. A roll that finds it not negative adds itself and closes that frame. Cell 47 holds the frame's first ball less 10, and that one number does three jobs. After a strike it's 0, which means the strike is still owed two rolls, so the next roll moves it into cell 46 as 10 plus that roll. Added to a second ball, it gives the two balls less 10, which is 0 for a spare, left in cell 46 to wait, and negative for an open frame, which closes on the spot. And storing the roll less 10 is the strike test, because TAC sends anything negative on to the second ball.
The tenth frame needs no code of its own. Its bonus rolls go round the same loop, as if they were starting an eleventh frame, but the routine that closes frames counts them, and after the tenth it halts before it reads another card. It returns with TAC 99 while the count is still negative, as in "Return with TAC 99 when the accumulator is negative".
cell contents notes
10 045 INP 45 cells 10-14: a first ball; a frame waiting on this roll (cell 46 not negative) closes with cell 46 + 10 + the roll
11 146 CLA 46
12 315 TAC 15
13 245 ADD 45
14 836 JMP 36
15 147 CLA 47 cells 15-18: after a strike (cell 47 is 0) the strike waits, with 10 + this roll, kept less 10; else nothing waits
16 318 TAC 18
17 245 ADD 45
18 646 STO 46
19 145 CLA 45 cells 19-23: cell 47 = the roll - 10, which is 0 for a strike, so a strike's next roll is a first ball too
20 750 SUB 50
21 647 STO 47
22 324 TAC 24
23 810 JMP 10
24 045 INP 45 cells 24-28: a second ball, closing the same way
25 146 CLA 46
26 329 TAC 29
27 245 ADD 45
28 836 JMP 36
29 147 CLA 47 cells 29-35: the two balls - 10: 0 for a spare, which waits on the next roll; negative for an open frame, which closes now
30 245 ADD 45
31 646 STO 46
32 334 TAC 34
33 810 JMP 10
34 836 JMP 36
35 810 JMP 10
36 250 ADD 50 cells 36-44: close a frame: add the accumulator + 10 to the score and output it; return with TAC 99 until the tenth
37 248 ADD 48
38 648 STO 48
39 548 OUT 48
40 149 CLA 49
41 200 ADD 00
42 649 STO 49
43 399 TAC 99
44 900 HRS 00
45 000 the roll
46 -001 the waiting frame's score - 10, or negative when no frame waits
47 -001 the frame's first ball - 10; 0 after a strike
48 000 the score
49 -010 frames to go, counting up from -10
50 010
Step through the CARDIAC way in Cardigan
Every roll kept
CodeThe rolls, up to 21Variables and constantsCell 00, always 001Free
88 cells, 21 of them the rolls. One cell between 10 and 98 is left free.What's owed
CodeWhat's owed: the waiting frame and the first ballVariables and constantsCell 00, always 001Free
41 cells, and none of them a roll from the past. The two in orange carry what's owed from one roll to the next.
Both output 20, 39, 48, 66, 74, 84, 90, 120, 148 and 167. The array way takes 88 cells and runs 510 instructions; the streaming way takes 41 cells and runs 270. A perfect game is 448 instructions against 244, and twenty gutter balls 549 against 279. The array way leaves just one cell free between 10 and 98, so there's no room for anything else.
This is the problem in Robert C. Martin's Bowling Game kata, a classic exercise in test-driven development. Its Game class keeps the rolls in an array of 21 and scores them with the same lookahead as the first program, which is the clear way to write it when memory is plentiful. The second program is an online algorithm. It handles each input as it arrives, keeps a small fixed state, and never needs an input again. Welford's 1962 method for the mean and variance of a stream works the same way. It keeps a count, the running mean and one running sum, however much data goes past, which is why it suits data too big to keep.
From the tips in
cardiac-code on GitHub. All the tips.