Re: A82: Rand Number Error


[Prev][Next][Index][Thread]

Re: A82: Rand Number Error



At 06:58 PM 9/4/97 -0700, you wrote:
>Alright, I had a random point generator working under Ash 2.0. However,
>when I converted to ash 3.0, I got a freeze. I isolated it to the 6th
>line below, which will rerandomize the number if it is over 95 (the
>screen boundary). Any help on fixing this, or somehow getting a random
>number between 0 and 95?
>
>NewTarget:
>    ld a, r
>    srl a
>    and 127
>    cp 95
>;   jr c, NewTarget   ; <-- If this line is uncommented, it cause a
>freeze (Or, is it an infinite loop?)
>    jr z, NewTarget
>    ld b, a

jr c,NewTarget says to the Z80 processor, "hmm, if a<95 then return carry
and jump", to which the processor replies, "okay, I'm going to jump if the
pixel is on the screen and NOT jump if it is OFF the screen."  So, to get
the Z80 processor to think the other way around, you need to use the
following code:

NewTarget:
    ld a, r
    srl a
    and 127
    cp 96     ; If A register>=96, then return not carry
    jr nc, NewTarget  ; Shorter, faster choice to jump
    ld b, a

This was a very simple bug to work out (but even I miss these little bugs
once in a while).  BTW, nice usage of the R register.  I must remind you
though, that there is a serious pattern to using it.  I recommend using
this register with a random list of pre-made numbers.  This will allow you
to get the most possible random combinations.  ZTetris has one block that
appears more often than the rest because it uses just the r register (like
you are) and doesn't do anything else with it.

Hope I could be of help

                 Thomas J. Hruska -- thruska@tir.com
Shining Light Productions -- "Meeting the needs of fellow programmers"
         http://www.geocities.com/SiliconValley/Heights/8504
                    http://shinelight.home.ml.org


References: