Skip to content

Keep the tallies

Keep six array entries that account for every roll.

  • The Craft

The numbered rolls disappear when you run again. Keep a summary before clearing the screen: how often did each face appear?

Continue from Roll a die. This stage still rolls twelve times and then stops. It will show the numbered rolls briefly, then replace them with a table of six counts.

Replace lines 20, 30, 450. Add lines 600, 1000, 1030, 1040, 1050, 1080, 1090, 1100, 1110, 1120, 1130, 1160, 1180. There are no lines to delete.

changes.bas
  20 RANDOMIZE : DIM t(6)
  30 LET n=0: LET b=12
 450 LET d=INT (RND*6)+1: LET t(d)=t(d)+1: LET n=n+1
 600 GO SUB 1000: GO SUB 1100
1000 CLS : PRINT AT 0,9; INK 5; BRIGHT 1;"DICE ROLLER"
1030 PRINT AT 4,1; INK 5;"FACE  COUNT"
1040 FOR j=1 TO 6: LET row=5+2*(j-1)
1050 PRINT AT row,2;j
1080 NEXT j
1090 RETURN
1100 PRINT AT 1,1;"Rolls ";n;"   "
1110 FOR j=1 TO 6
1120 LET row=5+2*(j-1)
1130 PRINT AT row,7;t(j);"    "
1160 NEXT j
1180 RETURN
Complete program at this stage
dice-roller.bas
  10 BORDER 0: PAPER 0: INK 7: BRIGHT 0: CLS
  20 RANDOMIZE : DIM t(6)
  30 LET n=0: LET b=12
 440 FOR i=1 TO b
 450 LET d=INT (RND*6)+1: LET t(d)=t(d)+1: LET n=n+1
 460 PRINT i;": ";d
 490 NEXT i
 600 GO SUB 1000: GO SUB 1100
 750 STOP
1000 CLS : PRINT AT 0,9; INK 5; BRIGHT 1;"DICE ROLLER"
1030 PRINT AT 4,1; INK 5;"FACE  COUNT"
1040 FOR j=1 TO 6: LET row=5+2*(j-1)
1050 PRINT AT row,2;j
1080 NEXT j
1090 RETURN
1100 PRINT AT 1,1;"Rolls ";n;"   "
1110 FOR j=1 TO 6
1120 LET row=5+2*(j-1)
1130 PRINT AT row,7;t(j);"    "
1160 NEXT j
1180 RETURN

RUN. Add the six counts by hand: they must total twelve. Some may be zero. The counts need not match one another.

One array, six entries

DIM t(6) creates a numeric array with six entries, all initially zero. Read t(2) as “the tally for face two”. Its valid positions are 1 through 6, the same range produced by the die expression.

Line 450 now stores the face, increases that face’s tally and increases n, the total counted rolls. LET t(d)=t(d)+1 reads the old count and writes back one more. It changes exactly one entry. A second RND call here would be a mistake: the counted face could differ from the one displayed.

The loop counter i says which repetition is running. The variable n says how many rolls have actually been counted. Those values grow together here, but keeping n separate will help when we allow an early stop.

After faces 2, 2, 6 and 1, what should the six tallies contain?

Show the explanation

In face order they are 1, 2, 0, 0, 0, 1. The total is four. Each roll contributes one to exactly one entry, so the sum increases once per roll.

Give the display two jobs

A subroutine is a group of numbered lines that can be called and returned from. GO SUB 1000 remembers where to come back, runs the header routine and reaches RETURN at 1090. Execution then resumes after the call. GO SUB 1100 prints the current totals in the same way.

Line 600 calls both routines after rolling. Line 750 then stops the program. Without that STOP, execution would continue into the routines without a matching call, eventually reaching a RETURN with nowhere to return to.

The header clears the old rolls, prints a title and labels, then places face numbers. PRINT AT takes row, column, counted from zero at the top left. The expression 5+2*(j-1) places faces on rows 5, 7, 9, 11, 13 and 15. The blank rows will hold bars later. Array positions beginning at one and screen coordinates beginning at zero are separate conventions.

The routine at 1100 prints n at the top. Its loop reads each t(j) and places it beside the corresponding face. Trailing spaces after a count erase leftover digits when this routine is reused on a changing display.

RUN again. DIM resets the tallies and line 30 resets n: this is a fresh sample, not twenty-four rolls accumulated across two runs. A wrong total is an accounting problem; an uneven spread is not.

Keyboard notes

DIM uses D in keyword mode. GO SUB uses H and RETURN uses Y. AT is Symbol Shift with I. Type the parentheses around the array position: t is part of the name, while the number inside selects its entry.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 4, chapter 5, chapter 6, chapter 12, chapter 15.