Advertisement
AnthonyCagliano

Yatzee complete pre-release

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