[A83] shuffle code help please


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

[A83] shuffle code help please




this isnt the worlds most elegant code but it works, somewhat. I am trying to 
shuffle 52 cards ranging from 0..51. the method i am trying is takeing 2 
random slots and swapping them, then repeating the process 255 times. after i 
run this code and check it i am getting repeates in the sequence.

shuffle:
    ;new idea take two random slots and swap them
    ld b,255
again:
    push bc                   ;save the main loop var
    ld b,52
    call irandom             ;get random number 0-51
    ld (slot1),a
    ld b,52
    call irandom             ;another random number 0-51
    ld (slot2),a
    ld hl,carddeck      ;start to put slot1 into slot2
    ld a,(slot2)
    ld b,0
    ld c,a
    add hl,bc                  ;get address of deck+slot number
    ld a,(slot1)
    ld (hl),a
    ld hl,carddeck      ;start to put slot2 into slot1
    ld a,(slot1)
    ld b,0
    ld c,a
    add hl,bc
    ld a,(slot2)
    ld (hl),a
    pop bc
    djnz again
    ret

carddeck:
    .db 0,1,2,3,4,5,6,7,8,9,10
    .db 11,12,13,14,15,16,17,18,19,20
    .db 21,22,23,24,25,26,27,28,29,30
    .db 31,32,33,34,35,36,37,38,39,40
    .db 41,42,43,44,45,46,47,48,49,50
    .db 51

slot1:
       .db 0
slot2:
       .db 0



Follow-Ups: