AnthonyCagliano

Untitled

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