Advertisement
Zeda

Yahtzee

Jan 2nd, 2016
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dice = scrap    ;5 bytes, current dice state
  2. round=scrap+5   ;counter for which round we are on
  3.  
  4. Yahtzee:
  5.     ld a,r          ;\
  6.     ld h,a          ; |
  7.     xor 77          ; |initialize PRNG seeds
  8.     ld l,a          ; |
  9.     ld (seed1),hl   ; |
  10.     ld (seed2),hl   ;/
  11. main:
  12.     ld hl,$8080     ;\
  13.     ld (dice),hl    ; |Reset the dice to the "needs rerolling state"
  14.     ld (dice+2),hl  ; |
  15.     ld (dice+3),hl  ;/
  16.     call roll       ;Roll the dice for the first step
  17.     ld bc,200h      ;number of rerolls is B, C is the current selected die
  18.     ld hl,dice      ;HL points to the current selected die
  19. waitloop:
  20.     ld a,(kbdScanCode) \ or a \ jr z,waitloop
  21.     cp 15 \ jp z,exit
  22.     cp 55 \ call z,mode \ jr z,waitloop
  23.     cp 10 \ jr nc,$+10 \ call selection \ call render_dice \ jr waitloop
  24.     cp 54 \ jr nz,waitloop
  25.     push hl
  26.     call roll
  27.     pop hl
  28.     djnz waitloop
  29. scorecheck:
  30. ;;Check if it's a winning combination
  31. ;;Also need some quitting condition. I don't know when the game is over.
  32.     jp main
  33. exit:
  34. ;;not sure if you want to do something fancy before exiting
  35.     ret
  36. mode:
  37. ;;Bring up options and stuff
  38.     push af
  39.     push hl
  40.     push bc
  41.  
  42.     pop bc
  43.     pop hl
  44.     pop af
  45.     ret
  46. selection:
  47. ;;DOWN needs to deselect the current die from being rerolled.
  48. ;;LEFT needs to move the selector to the left, wrapping around.
  49. ;;RIGHT needs to move the selector to the right, wrapping around.
  50. ;;UP selects the current die for rerolling.
  51. ;;ENTER toggles whether o not it gets rerolled
  52.     dec a \ jr nz,$+5 \ res 7,(hl)\ ret
  53.     dec a \ jr nz,$+11 \ dec hl \ dec c \ ret p  \ ld c,4 \ ld hl,dice+4 \ ret
  54.     dec a \ jr nz,$+13 \ inc hl \ inc c \ ld a,c \ sub 5 \ ret nz \ ld c,a \ ld hl,dice \ ret
  55.     dec a \ jr nz,$+5 \ set 7,(hl) \ ret
  56.     ld a,(hl) \ xor 80h \ ld (hl),a
  57.     ret
  58. roll:
  59.     push af
  60.     push hl
  61.     push de
  62.     push bc
  63.     ld hl,dice-1
  64.     call rollSub
  65.     call rollSub
  66.     call rollSub
  67.     call rollSub
  68.     call rollSub
  69.     pop bc
  70.     pop de
  71.     pop hl
  72.     pop af
  73. render_dice:
  74. ;;render the scene including dice values
  75. ;;Things to consider:
  76. ;;      B is the number of rerolls left.
  77. ;;      C is the current selected die.
  78. ;;      Any dice with bit 7 set means it is up for reroll. If A is the die value:
  79. ;;          add a,a \ jr nc,$+ \ <<adjust coord>>
  80. ;;          rlca
  81. ;;          call drawdie
  82.     push af
  83.     push hl
  84.     push de
  85.     push bc
  86.  
  87.     pop bc
  88.     pop de
  89.     pop hl
  90.     pop af
  91.     ret
  92. rollsub:
  93.     inc hl
  94.     ld a,(hl) \ add a,a
  95.     ret nc
  96.     push hl
  97.     push bc
  98. seed1=$+1
  99.     ld hl,9999
  100.     ld b,h
  101.     ld c,l
  102.     add hl,hl
  103.     add hl,hl
  104.     inc l
  105.     add hl,bc
  106.     ld (seed1),hl
  107. seed2=$+1
  108.     ld hl,987
  109.     add hl,hl
  110.     sbc a,a
  111.     and %00101101
  112.     xor l
  113.     ld l,a
  114.     ld (seed2),hl
  115.     add hl,bc
  116.     ld l,h \ ld h,0
  117.     ld b,h \ ld c,l
  118.     add hl,hl \ add hl,bc \ add hl,hl
  119.     inc h \ ld a,h
  120.     pop bc
  121.     pop hl
  122.     ld (hl),a
  123.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement