Dice 6

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.
As usual you can download the source, hex, listing and mp3 files by clicking on the download link on the left here. The mp3 file can be played back to the MPF-1 using file name 0004.

User Instructions

After starting the program in the usual way (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.
The program now waits for you to press GO, which will start the actual dice rolling program. While you hold the GO key the display will show 6 animated rolling patterns. As soon as you release the GO key you will see 6 random numbers appear, each in the range from 1 to 6. It will look something like this 1 2 3 4 5 6.
With the dice shown like this you can start locking the ones you don't want to roll during the next toss. Simply press the digits 1 to 6 on the keyboard to lock/unlock the corresponding dice. Die number 1 is the left most die, which makes 6 the right most die. A locked die shows a decimal point to its right, something like this 4 2 1.5 6 1..
The DEL key clears all locks at the same time so that the next time you press GO you will roll all the dice again.

Generating The Random Numbers

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 I've implemented a different approach. Well it's not all together that much different really. I use 6 SEED values, these values are used to calculate the next pseudo random number. These 6 SEED values are incremented with a different step size after each display scan cycle. Incrementing them with a different step size ensures that there will be no real relationship between the outcome of the 6 dice. The step sizes are all prime numbers which ensures that each SEED value will have a total of 256 different possible values (the SEEDS are byte values indeed).

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
                JP      PO,.SF
.SF             LD      A,(SEED)
                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.
This is not really a problem for our random number generator because the SEED is increment after every display scan anyway. But if you like to use this pseudo random number generating routine in your own programs you must be aware of the limitation of this routine.
When using this code on its own you should keep in mind that the "random" pattern repeats itself after every 255 calls, not particularly random but it will do for simple games.

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.

Room For Improvements

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.

  • You may want the program to show less than 6 dice, as it does now. For instance the game of Yahtzee only needs 5. You can change the program to accept a digit the very first time, indicating the number of dice you want to use. After that you simply leave the unused dice blank.
  • I've made no effort to avoid cheating, eh I mean pressing wrong keys. Most dice games allow you to lock some dice, but you shouldn't be able to unlock them once you've made another toss. Other games limit the number of tosses per round.
    Feel free to add these kind of rule checking functions for the dice games you like to play.
  • You could group locked dice together on the left for easier reading.


Here you can download the file dice6.zip, which contains the source listing, the actual plain hex file, a list file and an mp3 file of the program.