Problem #1 .. Random Numbers.
In my game I require two tables of random 1's and 0's. Table A (roaddata) will be 2 columns x 4 rows, table B (trafficdata) will be 3 columns x 5 rows. There is a couple of rules : No table row should contain all zero's, Table B must not have a row containing all 1's. The tables should feel random.
Each game cycle, the two tables are moved forward, row 2 being copied to row 1 etc. and the last row is filled with a new line of random 1's and 0's
I tried a few things to simulate randomness including using the R register to point to a ROM location. All attempts looked awful, the tables produced did not feel random at all. I guess the ROM contains many patterns which are far from random.
Here is my code as it stands now :
Code: Select all
advanceroad:
ld de,roaddata_row1 ;load DE with address of 1st row of roaddata
ld hl,roaddata_row2 ;load HL with address of 2nd row
ld bc,6 ;copy 6 bytes
ldir ;move road data forward one row
ld de,trafficdata_row1 ;load DE with address of 1st row of roaddata
ld hl,trafficdata_row2 ;load HL with address of 2nd row
ld bc,12 ;copy 12 bytes
ldir ;move traffic data forward one row
ld a,(rndpos) ;load C with the random number pointer
ld c,a
ld b,0
ld hl,randomness ;load HL with adress of the start of random numbers
add hl,bc ;add BC to HL to find next random number address
inc a ;increment the random number pointer
ld (rndpos),a
ld a,(hl) ;load A with the random number
ld de,trafficdata_row5 ;load DE with address of 5th row
ld hl,trafficstyle ;load HL with start address of the traffic styles
ld c,a
add hl,bc ;add the random number to the traffic style address
ld bc,3 ;copy 3 bytes
ldir
and %00000011 ;strip to two bytes (random 0 - 3)
ld de,roaddata_row4 ;load DE with address of 4th row
ld hl,roadstyle ;load HL with start address of the road styles
ld c,a ;load BC with the random number in A
ld b,0
add hl,bc ;add the random number to the road style address
ld bc,2 ;copy 2 bytes
ldir
ret
roadstyle:
defb 1,1,0,1,1
trafficstyle:
defb 1,1,0,1,0,0,1,1,0,1
roaddata_row1:
defb 0,0
roaddata_row2:
defb 0,0,0,0
roaddata_row4:
defb 0,0,23
trafficdata_row1:
defb 0,0,0
trafficdata_row2:
defb 0,0,0,0,0,0,0,0,0
trafficdata_row5:
defb 0,0,0
rndpos:
defb 0
randomness:
defb 4, 2, 7, 3, 0, 6, 1, 5
defb 2, 6, 0, 7, 5, 1, 4, 3
defb 4, 1, 3, 6, 2, 0, 5, 7
defb 2, 4, 0, 5, 6, 1, 3, 7
defb 0, 6, 7, 2, 3, 1, 4, 5
defb 1, 7, 5, 6, 2, 3, 0, 4
defb 6, 3, 5, 7, 4, 0, 2, 1
defb 6, 2, 3, 7, 5, 4, 1, 0
defb 3, 4, 1, 0, 7, 2, 6, 5
defb 1, 5, 0, 4, 3, 2, 6, 7
defb 1, 7, 4, 6, 0, 2, 5, 3
defb 4, 5, 1, 7, 0, 3, 2, 6
defb 4, 6, 2, 0, 5, 3, 7, 1
defb 6, 4, 3, 2, 5, 1, 7, 0
defb 7, 5, 2, 3, 0, 1, 6, 4
defb 1, 6, 4, 7, 5, 3, 0, 2
defb 1, 6, 7, 5, 2, 0, 4, 3
defb 6, 0, 1, 7, 5, 3, 2, 4
defb 7, 1, 2, 5, 6, 4, 3, 0
defb 2, 3, 1, 0, 6, 7, 5, 4
defb 3, 1, 2, 5, 4, 0, 7, 6
defb 1, 5, 3, 7, 6, 2, 0, 4
defb 7, 5, 3, 4, 6, 0, 1, 2
defb 1, 7, 6, 5, 4, 0, 3, 2
defb 2, 3, 0, 1, 6, 4, 5, 7
defb 5, 4, 0, 3, 1, 2, 7, 6
defb 3, 2, 7, 1, 5, 6, 0, 4
defb 3, 1, 0, 5, 6, 4, 2, 7
defb 4, 5, 6, 1, 3, 7, 0, 2
defb 7, 2, 5, 6, 0, 3, 4, 1
defb 2, 0, 1, 6, 5, 7, 3, 4
defb 3, 2, 5, 0, 6, 7, 1, 4
My question is this..Is this how it is done? Is there a ROM call that produces good random numbers? Is there a way to avoid the 256B look up table?