|   |
![]() |
||||||
|
This is another dice rolling program.
It rolls up to 6 dice at the same time, after that you can lock one or more dice and roll the rest again.
This program demonstrates another way of generating pseudo random numbers.
It also shows you how you can have some limited animation on the display because it will "roll" the displays while you roll the dice.
After starting the program in the usual manner (ADDR 1 8 0 0 GO) you should see something like this on the display * d I C E *.
The two asterisks on this page indicate where you should see two animated rolling patterns on the display.
I think it may be useful to explain a bit further how I generated the 6 random numbers.
As we saw in the dice 2 game we can't use the Z80's R register here anymore because it has not enough resolution to generate 6 pretty unpredictable numbers ranging from 1 to 6.
So the SEED values are constantly changed. Whenever the user releases the GO key after a toss the 6 SEEDS, which are fairly random by them selves already, are used to calculate 6 new pseudo random numbers ranging from 0 to 255. I used the piece of you see below (slightly modified):
RND LD A,(SEED) Use seed to calculate
AND A,#10111000B random number
SCF
JP PO,.SF
CCF
.SF LD A,(SEED)
RLA
LD (SEED),A Save new seed value
At the end of this short routine you'll have a new pseudo random number in the accumulator, which is also used as a new SEED value for future random numbers.
There is one little problem with this code however, it doesn't like a SEED with the value of 0, because if the SEED is 0 the end result will also be 0.
Subsequent calls of this routine will always report 0 if you start off with a SEED of 0.
By the way, I initialize the 6 SEED bytes with a random number which is generated by the SB-Assembler. This is not really necessary, however it demonstrates the use of the fairly unique .RF directive. This concludes the explanation of the random number generator. The rest of the program flow should be easy enough to follow from the source code.
As usual I left some room for improvement in this program. You'll learn more if you can manage to implement the ideas below, or you may even have some more ideas for improvement yourself.
|