Classic Computer Magazine Archive COMPUTE! ISSUE 25 / JUNE 1982 / PAGE 86

Atari Dice Simulation

W. C. McLachlan
Dallas

So you wanted to program a board game utilizing dice? Well, you've come to the right place: Atari's graphics and string handling capabilities make it easy.

It is possible to simulate dice in graphics modes 3 through 7 (even 8), but it is cumbersome. It is much easier and faster in mode 0. The following program uses one large string (DICE$) to store the die faces one through six. Each die is divided into nine parts. By effectively utilizing Atari's cursor controls, we can build the parts into a realistic looking die.

100 GR.0
110 DIM DICE$(102) : POKE 752, 1 : REM TURN OFF CURSOR
120 DICE$ = "AAACDDDABACDDDAAABAACDDDAAACD DDAABBAACDDDABACDDDAABBAACDDDAAACDDDBABB
         AACDDDABACDDDBABBBBCDDDAAACDDDBBB"
130 X = 20 : Y = 10 : REM X AND Y ARE INITIAL DIE POSITION
140 DICE = INT(6*RND(0)+1) : REM RANDOM NUMBER
150 A = DICE*17-16 : REM A = SUBSTRING START POSITION
160 COUNT = COUNT + 1
170 IF X>25 THEN X = 20
180 POSITION X, Y
190 PRINT DICE$(A, A + 16) : REM PRINT THE SUBSTRING
200 X = X + 5 : REM MOVE OVER 5 SPACES TO PRINT NEXT DIE
210 SO. 0, 10, 2, 15 : S0. 0, 0, 0, 0 : REM SOME DICE ROLLING NOISE
220 IF COUNT <> 20 THEN 140

To enter line 120, the following code prevails:

A = (REVERSE VIDEO) SPACE
B = (REVERSE VIDEO) CTRL-. (PERIOD)
C = ESC-CTRL-DOWN ARROWM
D = ESC-CTRL-LEFT ARROW

You'll note that the first 17 characters constitute die face #1, the second 17–die face #2, etc. All six faces will fit on one logical line, although it's a tight fit. Now to the program:

Line 140 selects a number from one to six. After the number is selected, it will be necessary to tell the computer what part of the string contains the die face corresponding to it. This is the function of line 150. Next, the substring is printed (line 190) at the position determined in line 180. Since two dice are displayed, lines 170 and 210 are required to select the die position. Lines 160 and 220 combine to roll each die ten times to simulate a dice "throw."

This routine has been expanded just to make it clearer. You can compact it to four logical lines and store it away as a subroutine in your programs. [The word logical means "as the computer would see it." In other words, four "logical lines" might break into seven or eight lines on your TV because it can only show 40 characters per line. There would still only be four "logical lines," however, since there would only be four line numbers (to the computer). Sometimes, for example, sector 15 on a disk might be located physically before sector 14. They would still be logical 14,15.]

Other additions, such as adding more dice, or using joysticks to initiate a dice roll, are made easily.

Atari BASIC allows for extremely fast string handling. This is just one example of how to use a string to its fullest advantage. Remember, you can use any of the edit commands (tab, cursor up, cursor right, backspace, line delete, etc.) in a string by first pressing the ESC key. Usually, you can save a considerable amount of time and unnecessary program steps by employing string edit commands.