Advertisement
Zeda

Yahtzee_Acag_Modified

Jan 6th, 2016
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with the exception of rendering a few text strings and looping that dice->LCD code, game is done. Things of note...
  2.  
  3. 1. In the game, if you CANNOT score against a combo, you are forced to enter a score of 0 on an open combo. In this code, this is written as a value of -1. Every score is therefore upped by 1, so I can just dec a before saving it. The code to see if a combo is available (under EnterScore) should exit if a non-zero value exists for that combo.
  4.  
  5. 2. entryMode now controls what actions you can perform. Bit0 is throwing the dice, Bit1 is scoring against a combo. In this code, if both bits are set, you should be able to do everything. If only bit 0 is set, you should be able to roll the dice, and hold a dice, but do nothing else. If only bit 1 is set, you should be able to toggle combos and select one, but do nothing else. You should always be able to bring up the instructions and quit the game.
  6.  
  7.  
  8. ; Yatzee for TI-83+/84+
  9. ; Version 1.0
  10. ; by ACagliano
  11.  
  12. .nolist
  13. #include "ti83plus.inc"
  14. #include "dcs7.inc"
  15. .list
  16. ; defines/equates
  17. #define diceOne 0
  18. #define diceTwo 1
  19. #define diceThree 2
  20. #define diceFour 3
  21. #define diceFive 4
  22. #define diceSix 5
  23. #define comboScores saferam1
  24.  #define threeOnes 0
  25.  #define threeTwos 1
  26.  #define threeThrees 2
  27.  #define threeFours 3
  28.  #define threeFives 4
  29.  #define threeSixes 5
  30.  #define threeOfaKind 6
  31.  #define fourOfaKind 7
  32.  #define fullHouse 8
  33.  #define smStraight 9
  34.  #define lgStraight 10
  35.  #define chance 11
  36.  #define yatzee 12
  37.  #define threesBonus 13
  38. #define gameScore comboScores+threesBonus
  39. #define entryMode gameScore+2
  40.  #define spin 0
  41.  #define score 1
  42. #define highScore entryMode+1
  43. #define curRolls highScore+2
  44. #define dieValues curRolls+1
  45. #define dieHoldFlags dieValues+5
  46. #define threesPoints dieHoldFlags+1
  47. #define saveSize threesPoints-comboFlags
  48.  
  49. #define selectedCombo dieHoldFlags+1
  50. #define ptsforSelected selectedCombo+1
  51. #define rollBonuses ptsforSelected+2
  52. #define ctOnes rollBonuses+1
  53. #define ctTwos ctOnes+1
  54. #define ctThrees ctTwos+1
  55. #define ctFours ctThrees+1
  56. #define ctFives ctFours+1
  57. #define ctSixes ctFives+1
  58.  
  59.  
  60. .db $BB,$6D
  61. .org progstart
  62.  
  63. Start:
  64.     bcall(_ClrLCDFull)
  65.     ld a,r          ;\
  66.     ld h,a          ; |
  67.         xor 77          ; |Seed the RNG
  68.         ld l,a          ; |
  69.         ld (seed1),hl   ; |
  70.         ld (seed2),hl   ;/
  71.  
  72.     push iy \ ld iy,comboScores     ; push system flags location, set IY to comboScores address
  73.     ld ix,dieValues             ; set IX to 5-byte die roll values
  74.            
  75.     ld hl,gameScore \ ld b,ctSixes-comboScores \ call zeroData  ; zero the RAM savestate
  76.  
  77.     ld hl,GameSave \ rst 20h \ bcall(_ChkFindSym)   ; \
  78.     jr c,StartGame                  ;  | check whether save file exists
  79.     ld b,a \ or a \ jr z,{1@}           ;  | if it doesn't skip to game start
  80.     bcall(_Arc_Unarc) \ bcall(_ChkFindSym)      ;  | if it does and is unarchived, skip to loading
  81. @:  ex de,hl \ ld de,comboFlags         ;  | if it does and is archived, unarchive it
  82.     inc hl \ inc hl                 ;  | load the savestate into RAM
  83.     ld bc,saveSize                  ;  |
  84.     ldir                        ; /
  85. StartGame:
  86.     call renderDie
  87.     call renderStats
  88.     ld hl,entryMode \ set spin,(hl)     ; enable spinning, but not scoring yet
  89. KeywaitLoop:
  90.     ld a,(kbdScanCode) \ or a \ jr z,KeywaitLoop
  91.     ld hl,entryMode
  92.     bit score,(hl) \ jr z,{1@}      ; if the score bit is reset, you are not allowed to score now
  93.     cp skEnter \ jp z,EnterScore
  94.     cp skLeft \ jr nz,$+11 \ ld hl,selectedCombo \ dec (hl) \ call calcScore \ jr KeywaitLoop
  95.     cp skRight \ jr nz,$+11 \ ld hl,selectedCombo \ inc (hl) \ call calcScore \ jr KeywaitLoop
  96.     bit spin,(hl) \ jr z,KeywaitLoop    ; if the spin bit is reset, you are not allowed to spin now
  97.  
  98. @:  sub 54 \ add a,5 \ jp c,holdDice
  99.     add a,49
  100.     cp sk2nd \ jp throwDie
  101.     cp skClear \ jp QuitGame
  102.     jr KeywaitLoop
  103.  
  104. holdDice:
  105.     ld hl,dieValues
  106.     add a,l \ ld l,a \ jr nc,$+3
  107.     inc h \ ld a,(hl)
  108.     xor 80h \ ld (hl),a
  109.     jp KeywaitLoop
  110.  
  111.  
  112. ; ### Select Combo and Compute Score ###
  113.  
  114. calcScore:
  115. #define checkQuantity inc hl \ sub (hl)
  116.     ld de,dieValues-1
  117.     ld bc,$0507
  118.     inc de
  119.     ld a,(de) \ and c   ; mask out the flag bits of the die
  120.     ld hl,ctOnes
  121.     add a,l
  122.     ld l,a
  123.     jr nc,$+3
  124.     inc h
  125.     inc (hl)
  126.     djnz $-11
  127.     ; now we test the combo that is active. Score is either returned, or 0.
  128.     ld hl,calcBonuses \ push hl ; we do this so we can use ret at the end of the routines ahead and it acts like a jump
  129.     ld a,(selectedCombo)
  130.     ld hl,jmptbl
  131.     add a,a
  132.     add a,l
  133.     ld l,a
  134.     jr nc,$+3
  135.     inc h
  136.     ld a,(hl)
  137.     inc hl
  138.     ld h,(hl)
  139.     jp (hl)
  140. jmptbl:
  141. .dw isThreeOnes
  142. .dw isThreeTwos
  143. .dw isThreeThrees
  144. .dw isThreeFours
  145. .dw isThreeFives
  146. .dw isThreeSixes
  147. .dw isThreeOfaKind
  148. .dw isFourOfaKind
  149. .dw isfullHouse
  150. .dw isSmStraight
  151. .dw isLgStraight
  152. .dw sumtheDie
  153. .dw isYatzee
  154.  
  155. calcBonuses:
  156.     dec a \ ld (ptsforSelected),a
  157.     call isYatzee \ or a \ jr z,{1@}
  158.     dec a \ ld (rollBonuses),a
  159. @:  ld a,(threesBonus) \ or a \ call z,isFullTopSix
  160.     ld hl,rollBonuses \ add a,(hl)
  161.     call rendertPoints
  162.     ret
  163.  
  164. isThreeOnes:
  165.     ld a,(ctOnes) \ inc a \ ret
  166. isThreeTwos:
  167.     ld a,(ctTwos)
  168.     add a,a \ inc a \ ret
  169. isThreeThrees:
  170.     ld a,(ctThrees)
  171.     ld b,a \ add a,a \ add a,b
  172.     inc a \ ret
  173. isThreeFours:
  174.     ld a,(ctFours)
  175.     add a,a \ add a,a
  176.     inc a \ ret
  177. isThreeFives:
  178.     ld a,(ctFives)
  179.     ld b,a \ add a,a \ add a,a \ add a,b
  180.     inc a \ ret
  181. isThreeSixes:
  182.     ld a,(ctSixes)
  183.     ld b,a
  184.     add a,a
  185.     add a,b
  186.     add a,a
  187.     inc a \ ret
  188. isThreeOfaKind:
  189.     ld a,2 \ jr {1@}
  190. isFourOfaKind:
  191.     ld a,3
  192. @:  ld hl,ctOnes-1
  193.     checkQuantity \ jr c,sumtheDie
  194.     checkQuantity \ jr c,sumtheDie
  195.     checkQuantity \ jr c,sumtheDie
  196.     checkQuantity \ jr c,sumtheDie
  197.     checkQuantity \ jr c,sumtheDie
  198.     xor a
  199.     ret
  200.  
  201. isFullHouse:
  202.     ld a,2 \ ld hl,ctOnes-1
  203.     checkQuantity \ jr c,{1@}
  204.     checkQuantity \ jr c,{1@}
  205.     checkQuantity \ jr c,{1@}
  206.     checkQuantity \ jr c,{1@}
  207.     checkQuantity \ jr c,{1@}
  208.     jr {3@}
  209. @:  ld a,1 \ ld hl,ctOnes-1
  210.     checkQuantity \ jr c,{1@}
  211.     checkQuantity \ jr c,{1@}
  212.     checkQuantity \ jr c,{1@}
  213.     checkQuantity \ jr c,{1@}
  214.     checkQuantity \ jr c,{1@}
  215.     xor a\ ret
  216. @:  ld a,26 \ ret
  217.  
  218. isSmStraight:
  219.     ld hl,ctOnes \ ld a,(hl)
  220.     or a \ jr z,strt_start_2
  221.     ld b,3
  222.     inc hl \ and (hl)
  223.     djnz $-2
  224.     jr nz,{1@}
  225. strt_start_2:
  226.     inc hl \ ld a,(hl)
  227.     or a \ jr z,strt_start_3
  228.     ld b,3
  229.     inc hl \ and (hl)
  230.     djnz $-2
  231.     jr nz,{}
  232. strt_start_3:
  233.     inc hl \ ld a,(hl)
  234.     or a \ ret z
  235.     ld b,3
  236.     inc hl \ and (hl)
  237.     djnz $-2
  238.     ret z
  239. @:  ld a,31 \ ret
  240.  
  241. isLgStraight
  242.     ld hl,ctOnes \ ld a,(hl)
  243.     or a \ jr z,strt_1_to_5
  244. ;strt_2_to_6:
  245.     inc hl
  246. strt_1_to5:
  247.     ld a,(hl) \ ld b,3
  248.     inc hl \ and (hl)
  249.     djnz $-2
  250.     ret z
  251.     ld a,41 \ ret
  252.  
  253. isYatzee:
  254.     ld hl,ctOnes-1 \ ld a,4
  255.     checkQuantity \ jr c,{1@}
  256.     checkQuantity \ jr c,{1@}
  257.     checkQuantity \ jr c,{1@}
  258.     checkQuantity \ jr c,{1@}
  259.     checkQuantity \ jr c,{1@}
  260.     xor a \ ret
  261. @:  ld a,51 \ ret
  262.  
  263. isFullTopSix:
  264.     ld hl,comboScores \ xor a
  265.     ld b,6
  266. @:  add a,(hl) \ inc hl
  267.     djnz {-1@}
  268.     cp 62 \ jr c,{1@}
  269.     xor a \ ret
  270. @:  ld a,1 \ ld (threesBonus),a \ ld a,35 \ ret
  271.  
  272.  
  273.  
  274.  
  275. sumtheDie:
  276.     ld a,1 \ ld hl,dieValues
  277.     ld b,5
  278. @:  add a,(hl) \ inc hl
  279.     djnz {-1@}
  280.     ret
  281.  
  282.    
  283.  
  284.  
  285.  
  286. throwDie:
  287. ; randomize the value of each die, if the hold flag for that die is not set
  288.     ld hl,dieValues-1
  289.     call roll
  290.     call roll
  291.     call roll
  292.     call roll
  293.     call roll
  294.     call renderDie
  295.     ld hl,entryMode \ set score,(hl)    ; allow scoring once die rolled
  296.     ld hl,curRolls \ inc (hl)       ; increment number of rolls by one
  297.     ld a,3 \ sub (hl) \ jr nz,KeywaitLoop   ; if rolls != 3, leave routine
  298.     ld hl,entryMode \ res spin,(hl)     ; if rolls = 3, disable rolling
  299.     jr KeywaitLoop
  300.  
  301.  
  302. EnterScore:
  303.     ld hl,comboScores
  304.     ld a,(selectedCombo) \ ld c,a \ ld b,0
  305.     add hl,bc
  306.     ld a,(hl) \ or a \ jr nz,{1@}
  307.     ld a,(ptsforSelected) \ ld (hl),a
  308.     inc a \ jr z,{1@}
  309.     ld hl,(gameScore)
  310.     ld bc,(ptsforSelected)
  311.     add hl,bc       ; add score to game score
  312.     xor a
  313.     ld b,a \ ld c,(rollBonuses)
  314.     add hl,bc       ; add bonuses to game score
  315.     ld (gameScore),hl   ; write new score back
  316.     ld hl,entryMode \ res score,(hl) \ set spin,(hl)    ; disable scoring, enable spinning
  317.     ld (curRolls),a         ; set back to 0 rolls
  318. @:  jp KeywaitLoop
  319.  
  320. QuitGame:
  321.     ld hl,GameSave \ rst 20h \ bcall(_ChkFindSym)
  322.     jr nc,{1@}                  ; if save exists already, skip to saving
  323.     ld hl,saveSize \ bcall(_CreateAppVar)       ; if it doesn't, create it
  324. @:  ld hl,comboFlags \ inc de \ inc de      ; set read address to RAM state, set write address to after size word
  325.     ld bc,saveSize \ ldir               ; set size, load
  326.     bcall(_ClrLCDFull)              ; clear screen
  327.     pop iy                      ; restore system flags
  328.     ret                     ; exit game
  329.  
  330. renderDie:
  331.     ld hl,Sprites
  332.  
  333.     ret
  334.  
  335. renderStats:
  336.  
  337.     ret
  338.  
  339.  
  340.  
  341. rendertPoints:
  342.     ld a,(selectedCombo)
  343.     ld hl,ComboNameText \ ld b,a
  344.     or a \ jr z,{2@}
  345. @:  add hl,7
  346.     djnz {-1@} 
  347. @:  ; set penCol and penRow
  348.     bcall(_vPutS)
  349.     ld a,(ptsforSelected)
  350.     ; convert byte value into text string
  351.     ret
  352.  
  353. die_setZero:
  354.     ld hl,dieValues     ; set read address to dieValues
  355.     xor a \ ld (hl),a   ; set dieValues to 0
  356.     ld de,dieValues+1   ; set write address to dieValues+1
  357.     ld bc,4 \ ldir      ; copy 4 bytes from read address to write address
  358.     ret
  359.  
  360. zeroData:
  361.     xor a
  362.     ld (hl),a \ inc hl
  363.     djnz zeroData
  364.     ret
  365.  
  366. ; ########################
  367. ; Random Number Generator
  368. ; ########################
  369.  
  370. prng16:
  371. ;collab with Runer112
  372. ;;Output:
  373. ;;    HL is a pseudo-random int
  374. ;;    A and BC are also, but much weaker and smaller cycles
  375. ;;    Preserves DE
  376. ;;148cc, super fast
  377. ;;26 bytes
  378. ;;period length: 4,294,901,760
  379. seed1=$+1
  380.     ld hl,9999
  381.     ld b,h
  382.     ld c,l
  383.     add hl,hl
  384.     add hl,hl
  385.     inc l
  386.     add hl,bc
  387.     ld (seed1),hl
  388. seed2=$+1
  389.     ld hl,987
  390.     add hl,hl
  391.     sbc a,a
  392.     and %00101101
  393.     xor l
  394.     ld l,a
  395.     ld (seed2),hl
  396.     add hl,bc
  397.     ret
  398. roll:
  399. ;;Input: A is the range.
  400. ;;Output: Returns in A a random number from 0 to B-1.
  401. ;;  B=0
  402. ;;  DE is not changed
  403. ;;Destroys:
  404. ;;  HL
  405. ;;Speed:
  406. ;;  322cc to 373cc, 347.5cc average
  407.      inc hl
  408.      ld a,(hl) \ add a,a
  409.      ret nc
  410.      push hl
  411.      call prng16
  412.      ld l,h \ ld h,0
  413.      ld b,h \ ld c,l
  414.      add hl,hl \ add hl,bc \ add hl,hl
  415.      inc h \ ld a,h
  416.      pop hl
  417.      ld (hl),a
  418.      ret
  419.  
  420. ; ########################
  421. ; ## Rendering Routines ##
  422. ; ########################
  423.  
  424. drawSprite_6_Bit:
  425.     ld a,d \ add a,$79  ; Y coord into A
  426.     ld b,12         ; Loop this 12 times
  427. drawSprite_outerloop:
  428.     inc a \ push af
  429.     out ($10),a \ call lcdwait
  430.     ld a,e \ add a,$20
  431.     out ($10),a \ call lcdwait  ; use outi instead
  432.     ld c,$11
  433.     outi \ call lcdwait
  434.     outi \ call lcdwait
  435.     pop bc \ pop af
  436.     djnz drawSprite_outerloop
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445. lcdwait:
  446.     in a,($10) ;bit 7 set if LCD is busy
  447.     rla \ jr c,lcdwait
  448.     ret
  449.  
  450.  
  451.  
  452. GameSave:
  453. .db AppVarObj,"YatSave",0
  454.  
  455. ComboNameText:
  456. .db "Ones  ",0
  457. .db "Twos  ",0
  458. .db "Threes",0
  459. .db "Fours ",0
  460. .db "Fives ",0
  461. .db "Sixes ",0
  462. .db "3.of.a",0
  463. .db "4.of.a.",0
  464. .db "F.Hse.",0
  465. .db "Sm.St.",0
  466. .db "Lg.St.",0
  467. .db "Chance",0
  468. .db "Yatzee",0
  469.  
  470.  
  471. Sprites:
  472. ; empty die
  473. .db %11111111,%11110000
  474. .db %10000000,%00010000
  475. .db %10000000,%00010000
  476. .db %10000000,%00010000
  477. .db %10000000,%00010000
  478. .db %10000000,%00010000
  479. .db %10000000,%00010000
  480. .db %10000000,%00010000
  481. .db %10000000,%00010000
  482. .db %10000000,%00010000
  483. .db %10000000,%00010000
  484. .db %11111111,%11110000
  485. ; one die
  486. .db %11111111,%11110000
  487. .db %10000000,%00010000
  488. .db %10000000,%00010000
  489. .db %10000000,%00010000
  490. .db %10000000,%00010000
  491. .db %10000110,%00010000
  492. .db %10000110,%00010000
  493. .db %10000000,%00010000
  494. .db %10000000,%00010000
  495. .db %10000000,%00010000
  496. .db %10000000,%00010000
  497. .db %11111111,%11110000
  498. ; two die
  499. .db %11111111,%11110000
  500. .db %10000000,%00010000
  501. .db %10000000,%00010000
  502. .db %10000110,%00010000
  503. .db %10000110,%00010000
  504. .db %10000000,%00010000
  505. .db %10000000,%00010000
  506. .db %10000110,%00010000
  507. .db %10000110,%00010000
  508. .db %10000000,%00010000
  509. .db %10000000,%00010000
  510. .db %11111111,%11110000
  511. ; three die
  512. .db %11111111,%11110000
  513. .db %10000000,%00010000
  514. .db %10000000,%11010000
  515. .db %10000000,%11010000
  516. .db %10000000,%00010000
  517. .db %10000110,%00010000
  518. .db %10000110,%00010000
  519. .db %10000000,%00010000
  520. .db %10110000,%00010000
  521. .db %10110000,%00010000
  522. .db %10000000,%00010000
  523. .db %11111111,%11110000
  524. ; four die
  525. .db %11111111,%11110000
  526. .db %10000000,%00010000
  527. .db %10110000,%11010000
  528. .db %10110000,%11010000
  529. .db %10000000,%00010000
  530. .db %10000000,%00010000
  531. .db %10000000,%00010000
  532. .db %10000000,%00010000
  533. .db %10110000,%11010000
  534. .db %10110000,%11010000
  535. .db %10000000,%00010000
  536. .db %11111111,%11110000
  537. ; five die
  538. .db %11111111,%11110000
  539. .db %10000000,%00010000
  540. .db %10011001,%10010000
  541. .db %10011001,%10010000
  542. .db %10000000,%00010000
  543. .db %10000110,%00010000
  544. .db %10000110,%00010000
  545. .db %10000000,%00010000
  546. .db %10011001,%10010000
  547. .db %10011001,%10010000
  548. .db %10000000,%00010000
  549. .db %11111111,%11110000
  550. ; six die
  551. .db %11111111,%11110000
  552. .db %10000000,%00010000
  553. .db %10011001,%10010000
  554. .db %10011001,%10010000
  555. .db %10000000,%00010000
  556. .db %10011001,%10010000
  557. .db %10011001,%10010000
  558. .db %10000000,%00010000
  559. .db %10011001,%10010000
  560. .db %10011001,%10010000
  561. .db %10000000,%00010000
  562. .db %11111111,%11110000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement