’Cause all games were better on the GBC

You are not logged in.

#1 2012-09-19 20:49:39

Registered: 2010-10-16
Post 1,111/2,348

Lottery example

When I was in high school, one of my friends programmed a lottery in our programming course and for some reason, this got me into thinking whether I could do the exact same thing now, using Gameboy's assembly language though. So yeah, I found some sort of a way to implement this and figured I might as well share with you what I thought of.

Let's assume we wanted to make a lottery in which the game draws 7 numbers out of 39. First, we'd have to create a code for creating a random number (00-FF) and if possible, put its range between 0x1 and 0x27. We could reset the two highest bits of this random byte we created, limiting it between 00 and 3F (FF - 80 - 40).

Next, there would have to be some sort of check for seeing whether our random number is between 0x28 and 0x3F though because if it was, it wouldn't belong to the ones picked up from the lottery. This could be done by adding (0x80 - 0x28) 0x58 to our random number and checking whether its eight bit would be set or not. If the bit was set, we would see that the drawn number was between 0x28 and 0x3F and we'd have to draw another random number and start from the beginning (for the lottery to be fair).

If our drawn number was between 0x0 and 0x27, we'd have to substract 0x58 from it to form the original number drawn (before adding 0x58 to it). Next, we could simply check whether our number is 0x0 or not.

Finally, we would get a number between 0x1 and 0x27 like we wanted to. In case this wasn't the first number drawn, we've had to check whether this number was drawn already. There is a code that's used in Pokemon G/S for checking whether a certain byte belongs to FF -arrayed byte table.

(Basically, when the first number was drawn, it was saved somewhere in ram, that address was loaded in hl, hl was incremented and FF was stored in [hl].)

Now in case the number we drew isn't a part of this FF arrayed byte table, FF is replaced with the drawn number, and FF saved to the address following this byte. This process is gone through until 7 numbers are stored in this FF arrayed byte table.

call @draw (creates a random number and stores it in a)
res 6, a
res 7, a
ld [hl], 58
add a, [hl]
bit 7, a
jr nz, @start
sub a, [hl]
cp a, 00
jr z @start
call @table (checks if value in a is in the FF arrayed table)
jr ("if true") @start
ld hl, $table
call @time (check how many numbers have been drawn and increment hl so)
ld [hl], a (load the "FF" with contents of a)
inc hl
ld [hl], FF
jp @start

Last edited by Miksy91 (2012-09-19 20:52:11)


#2 2012-09-21 14:47:40

Registered: 2010-10-16
Post 301/459

Re: Lottery example

There already is a routine like that. It's location escapes me right now, however, basically it draws a number between 0x00 and N (in register a). It's somewhere in the 1xxx or 2xxx range.




Board footer

Powered by FluxBB