Dice And Double PEEKs
Thomas W. Wfellis If you've discovered a clever timesaving technique or a brief but effective programming shortcut,
send it to "Hints & Tips," c/o
FNR, the X is just a marker in the DEF statement that defines the function. Line 20 rolls the dice twice, generating two numbers in the range 1-6, and then adds them together. Line 30 then waits for a
COMPUTEl's GAZETTE. If we use it, we'll pay you $35. Due to the vol
keypress, after which the program
ume of Hems submitted, we regret
again.
that we cannot always reply indi
vidually to submissions.
Compare the relatively short formula D - FNR(6) + FNR(6) to the
When you're writing a game in
longer alternative D = INT(RND U)+6 + l) + INT(RND(l)*6-rl). It's
BASIC and need a random number between 1 and 10, you would use a line that looks something like this: N = INT(RND(l)"10-t-l). The ran
loops back to line 20 to roll the dice
not only easier to read the FNR(6) version, it also uses up less memory and takes less typing. This random integer function
Here, we've defined a double PEEK function called FND. Line 20 uses FND to examine the pointers
that indicate where BASIC programs and variables are stored. In certain
programs it's important to know the values held by these pointers and FND simplifies the calculation. You can also invent a function to break a number into its low byte
and high byte. At the beginning of the program, include DEF FNH <X) = INT(X/256) and DEF FNL (X) = X AND 255. If you need to change a pointer, you can use FNL
and FNH to determine the low byte and high byte.
dom number function RND gener
can be used in a variety of ways. It
Anytime you find yourself using
ates a fractional number between 0
could be helpful in making up math
a certain mathematical routine over
and over, you may discover it's a good idea to rewrite it as a function. It's possible to nest them, to have one function call another, so you're not
and 1. Multiplying by ten yields a
problems for a children's educational
number in the range 0 to 9.9999999.
program. It could be part of an ON-
Adding 1 and performing an INT
GOTO branch (ON FNR(3) GOTO
makes it into an integer between 1
100,210,300) to make random
and 10. It's like rolling ten-sided
choices in an adventure game. It's
dice.
very useful when you're simulating percentage calculations in a strategy
16), 88 (on a VIC), or 160 (on a 128).
A General Function For Rolling Dice
game; perhaps a baseball player has
Defined functions are something like portable subroutines which are quite handy and flexible in a wide variety
Variations of the formula above might be found in many places within a long program. But its 18
characters take some time to type, especially if you're a hunt-andpeck typist. There's an easier way
to get random numbers: just define a function at the beginning of your program and then use the function in place of the formula. The follow
ing program simulates the rolling of two six-sided dice: 10 DKFFNR(X)=INT(RND(1)*X+1) 20 D=FNR(6) + fNR(6) :PRItJTD 30
GKTA5:IFA?=""THEN30
40
GOTO20
The function FNR defined in line 10 picks at random a whole number between 1 and X. Once that's been done, you can substitute FNR(X) for INT(RND(1)*X + 1). Note that it's not necessary to use the variable X when you later call 92
COMPUTE'S Gaiella
April 986
a 31 percent chance of getting a hit, so if FNR{100) is less than 32, the
player would be credited with a hit. And if you're creating a word puzzle, you can pick random letters with CHR$(FNR(26) + 64).
Double PEEKs Defined functions can contain any mathematical or logical operation,
but they can also contain any of the various BASIC functions which re turn a value. PEEK, for example, tells you what number a certain memory location contains. Many locations
use two-byte pointers in low-byte/ high-byte format. To convert to a
decimal number, you have to multi ply the high byte by 256 and add the low byte, just the sort of thing a de
fined function can do well. 10
DEFFND(X)=PEEK(X)+256*FEEK( X+l)
20
FORJ=4 3T05 5STI-:P2 iPRINTJ.FND
(J)iNEXT
limited by the maximum line length
of 80 characters (on a 64, Plus/4, or
of programming situations.
Qt
COMPUTED Gazette is looking for utilities, games, applications educational programs, and
tutorial articles. If you've created a program that you think other
readers might enjoy or find use
ful, send it, on tape or disk to: Submissions Reviewer COMPUTE! Publications P.O. Box 5406
Greensboro, NC 27403
Please enclose an SASE if you
wish to have the materials returned.' Articles are reviewed within four weeks of submission.