--- title: "Dice" id: 57354 type: "computer_media" slug: "dice-2" url: "http://localhost/computer_media/dice-2/" markdown_url: "http://localhost/computer_media/dice-2.md" published_at: "2024-10-04T08:48:10+00:00" modified_at: "2026-04-03T07:58:34+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/09/202_Dic1.png" excerpt: "A dice-rolling program uses a clever inverse-video character array and string-slicing trick to render up to five fully graphical die faces at once." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" indiv: - name: "Tony Willing" slug: "tony-willing" taxonomy: "indiv" url: "http://localhost/indiv/tony-willing/" genre: - name: "Dice" slug: "dice" taxonomy: "genre" url: "http://localhost/type/dice/" - name: "Game" slug: "game" taxonomy: "genre" url: "http://localhost/type/game/" media_contents: - id: 56735 title: "Timex Sinclair Public Domain Library Tape 1004" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1004/" media_type: "Program" programmers: - name: "Tony Willing" slug: "tony-willing" taxonomy: "indiv" url: "http://localhost/indiv/tony-willing/" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/09/202_Dic1.png" - url: "http://localhost/wp-content/uploads/2024/09/202_Dice.png" media_type_tags: "Dice, Game" --- This program simulates rolling one to five dice, displaying each die face using inverse-video characters stored in a two-dimensional string array. The array D$(6,9) holds six 9-character strings, each representing a 3×3 dot pattern for die faces one through six; the program slices each string into three 3-character rows using a FOR loop with STEP 3 to print the face grid. Random die values are generated with INT(1+6*RND). Input validation caps the number of dice at five, citing display width limitations, and entering zero exits the program gracefully. *** ## Program Analysis ### Program Structure The program divides into four logical phases: 1. **Initialisation (lines 1–60):** Clears the screen, dimensions the die-face array, and populates all six entries. 2. **Introduction (lines 62–70):** Prints instructions and prompts for the number of dice. 3. **Rolling loop (lines 100–700):** Accepts input, validates it, rolls the requested dice, and loops back to the prompt. 4. **Exit / error handlers (lines 1000–2050):** Handles the >5 error message, the farewell screen, and housekeeping. ### Die-Face Representation Each die face is stored as a 9-character string in the array `D$(6,9)`. The characters are arranged as three rows of three, where `%O` represents an inverse “O” (a pip) and `%` represents an inverse space (blank). This gives a visually clean 3×3 grid when printed. | D$ index | Face value | Pattern (3×3) | | --- | --- | --- | | 1 | 1 | · · · / · ● · / · · · | | 2 | 2 | ● · · / · · · / · · ● | | 3 | 3 | ● · · / · ● · / · · ● | | 4 | 4 | ● · ● / · · · / ● · ● | | 5 | 5 | ● · ● / · ● · / ● · ● | | 6 | 6 | ● · ● / ● · ● / ● · ● | ### String-Slicing Technique The rendering loop at lines 510–530 is the key idiom. Rather than storing three separate strings per face, the entire 9-character face is stored in one string and sliced into rows using `FOR Y=1 TO 9 STEP 3`, printing `D$(B,Y TO Y+2)` each iteration. This is an efficient use of the ZX81 substring notation and keeps array dimensioning simple. ### Random Number Generation Line 500 uses `INT(1+6*RND)` to produce an integer in the range 1–6 inclusive. This is the standard ZX81 BASIC idiom for a fair die roll; `RND` returns a value in [0,1) so the expression maps correctly onto {1,2,3,4,5,6}. ### Input Validation Validation is minimal but functional. Line 300 checks for zero to exit, and line 310 catches values greater than five and redirects to an explanatory message at line 1000 before looping back. There is no guard against negative numbers or non-integer input; entering a negative value would pass both checks and cause `FOR X=1 TO A` to execute zero iterations (the ZX81 executes a FOR…NEXT loop at least once only when the limit is already exceeded before entry — actually on the ZX81 a `FOR` loop with limit less than start does not execute the body), silently producing no output before returning to the prompt. ### Notable Anomalies - Line 310 uses `IF A>5` but the instructions say “less than 5”; entering exactly 5 is valid and works, but the error message at line 1010 says “choose numbers less than 5” — this is contradictory and should read “5 or less”. - Line 750 (`STOP`) is unreachable; the flow from line 700 (`GOTO 70`) never falls through to it. - Lines 2030–2050 (`CLEAR`, `SAVE`, `RUN`) follow a `STOP` at line 2020 and are therefore only reachable by direct `GO TO` or by the user continuing after the STOP — they appear to be developer/distribution utilities left in the listing. - The farewell signature at line 2010 prints `1/83/%A%W`, encoding a date (January 1983) and the author’s initials in inverse video. ## Source Code ``` 1 REM "DICE PROGRAM" BY ANTHONY WILLING 3 CLS 5 DIM D$(6,9) 10 LET D$(1)="% % % % %O% % % % " 20 LET D$(2)="%O% % % % % % % %O" 30 LET D$(3)="%O% % % %O% % % %O" 40 LET D$(4)="%O% %O% % % %O% %O" 50 LET D$(5)="%O% %O% %O% %O% %O" 60 LET D$(6)="%O% %O%O% %O%O% %O" 62 REM "DICE PROGRAM" 64 PRINT "I AM A DICE ROLLING PROGRAM" 65 PRINT 66 PRINT "I WILL ROLL FROM 1-5 DICE FOR YOU, AT RANDOM" 67 PRINT 68 PRINT "IF YOU ENTER A <0> AS THE CHOICEI WILL STOP ROLLING DICE" 69 PRINT 70 PRINT "HOW MANY DICE DO YOU WISH TO ROLL?" 100 INPUT A 200 CLS 300 IF A=0 THEN GOTO 2000 310 IF A>5 THEN GOTO 1000 400 FOR X=1 TO A 500 LET B=INT (1+6*RND) 510 FOR Y=1 TO 9 STEP 3 520 PRINT D$(B,Y TO Y+2) 530 NEXT Y 540 PRINT 600 NEXT X 700 GOTO 70 750 STOP 1000 PRINT "SORRY, MY DISPLAY CAN ONLY SHOW 5 DICE AT A TIME" 1005 PRINT 1010 PRINT "PLEASE CHOOSE NUMBERS LESS THAN 5, DOING SO UNTIL YOUR DESIRED TOTAL IS REACHED" 1015 PRINT 1020 GOTO 70 2000 PRINT AT 11,9;"" 2010 PRINT AT 20,18;"1/83/%A%W" 2020 STOP 2030 CLEAR 2040 SAVE "1020%2" 2050 RUN ```