Advertisement
AnthonyCagliano

Untitled

Jan 10th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.90 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 ix,dieValues ; set IX to 5-byte die roll values
  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 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 \ jp c,holdDice
  95. add a,49
  96. cp sk2nd \ jp 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 de
  139. ld a,(de) \ 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
  157. add a,l
  158. ld l,a
  159. jr nc,$+3
  160. inc h
  161. ld a,(hl)
  162. inc hl
  163. ld h,(hl)
  164. jp (hl)
  165. jmptbl:
  166. .dw isThreeOnes
  167. .dw isThreeTwos
  168. .dw isThreeThrees
  169. .dw isThreeFours
  170. .dw isThreeFives
  171. .dw isThreeSixes
  172. .dw isThreeOfaKind
  173. .dw isFourOfaKind
  174. .dw isfullHouse
  175. .dw isSmStraight
  176. .dw isLgStraight
  177. .dw sumtheDie
  178. .dw isYatzee
  179. calcBonuses:
  180. dec a \ ld (ptsforSelected),a
  181. call isYatzee \ or a \ jr z,{1@}
  182. dec a \ ld (rollBonuses),a
  183. @: ld a,(threesBonus) \ or a \ call z,isFullTopSix
  184. ld hl,rollBonuses \ add a,(hl)
  185. call rendertPoints
  186. ret
  187.  
  188. isThreeOnes:
  189. ld a,(ctOnes) \ inc a \ ret
  190. isThreeTwos:
  191. ld a,(ctTwos)
  192. add a,a \ inc a \ ret
  193. isThreeThrees:
  194. ld a,(ctThrees)
  195. ld b,a \ add a,a \ add a,b
  196. inc a \ ret
  197. isThreeFours:
  198. ld a,(ctFours)
  199. add a,a \ add a,a
  200. inc a \ ret
  201. isThreeFives:
  202. ld a,(ctFives)
  203. ld b,a \ add a,a \ add a,a \ add a,b
  204. inc a \ ret
  205. isThreeSixes:
  206. ld a,(ctSixes)
  207. ld b,a
  208. add a,a
  209. add a,b
  210. add a,a
  211. inc a \ ret
  212. isThreeOfaKind:
  213. ld a,2 \ jr {1@}
  214. isFourOfaKind:
  215. ld a,3
  216. @: ld hl,ctOnes-1
  217. checkQuantity \ jp c,sumtheDie
  218. checkQuantity \ jp c,sumtheDie
  219. checkQuantity \ jp c,sumtheDie
  220. checkQuantity \ jp c,sumtheDie
  221. checkQuantity \ jp c,sumtheDie
  222. ld a,0
  223. ret
  224.  
  225. isFullHouse:
  226. ld a,2 \ ld hl,ctOnes-1
  227. checkQuantity \ jr c,{1@}
  228. checkQuantity \ jr c,{1@}
  229. checkQuantity \ jr c,{1@}
  230. checkQuantity \ jr c,{1@}
  231. checkQuantity \ jr c,{1@}
  232. jr {3@}
  233. @: ld a,1 \ ld hl,ctOnes-1
  234. checkQuantity \ jr c,{1@}
  235. checkQuantity \ jr c,{1@}
  236. checkQuantity \ jr c,{1@}
  237. checkQuantity \ jr c,{1@}
  238. checkQuantity \ jr c,{1@}
  239. jr {2@}
  240. @: ld a,26 \ ret
  241. @: ld a,0 \ ret
  242.  
  243. isSmStraight:
  244. ld hl,ctOnes \ ld a,(hl)
  245. or a \ jr z,strt_start_2
  246. ld b,3
  247. inc hl \ and (hl)
  248. djnz $-2
  249. jr nz,{1@}
  250. strt_start_2:
  251. inc hl \ ld a,(hl)
  252. or a \ jr z,strt_start_3
  253. ld b,3
  254. inc hl \ and (hl)
  255. djnz $-2
  256. jr nz,{1@}
  257. strt_start_3:
  258. inc hl \ ld a,(hl)
  259. or a \ ret z
  260. ld b,3
  261. inc hl \ and (hl)
  262. djnz $-2
  263. ret z
  264. @: ld a,31 \ ret
  265.  
  266. isLgStraight
  267. ld hl,ctOnes \ ld a,(hl)
  268. or a \ jr z,strt_1_to_5
  269. ;strt_2_to_6:
  270. inc hl
  271. strt_1_to_5:
  272. ld a,(hl) \ ld b,3
  273. inc hl \ and (hl)
  274. djnz $-2
  275. ret z
  276. ld a,41 \ ret
  277. @: ld a,0 \ ret
  278.  
  279. isYatzee:
  280. ld hl,ctOnes-1 \ ld a,4
  281. checkQuantity \ jr c,{1@}
  282. checkQuantity \ jr c,{1@}
  283. checkQuantity \ jr c,{1@}
  284. checkQuantity \ jr c,{1@}
  285. checkQuantity \ jr c,{1@}
  286. ld a,0 \ ret
  287. @: ld a,51 \ ret
  288.  
  289. isFullTopSix:
  290. ld hl,comboScores \ xor a
  291. ld b,6
  292. @: inc a \ jr z,$+3 \ add a,(hl) \ inc hl
  293. djnz {-1@}
  294. cp 62 \ jr c,{1@}
  295. ld a,0 \ ret
  296. @: ld a,1 \ ld (threesBonus),a \ ld a,35 \ ret
  297.  
  298.  
  299.  
  300.  
  301. sumtheDie:
  302. ld a,0 \ ld hl,dieValues
  303. ld b,5
  304. @: add a,(hl) \ inc hl
  305. djnz {-1@}
  306. inc a
  307. ret
  308.  
  309.  
  310.  
  311. throwDie:
  312. ; randomize the value of each die, if the hold flag for that die is not set
  313. ld hl,dieValues-1
  314. call roll
  315. call roll
  316. call roll
  317. call roll
  318. call roll
  319. call renderDie
  320. ld hl,entryMode \ set score,(hl) ; allow scoring once die rolled
  321. ld hl,curRolls \ inc (hl) ; increment number of rolls by one
  322. ld a,3 \ sub (hl) \ jp nz,KeywaitLoop ; if rolls != 3, leave routine
  323. ld hl,entryMode \ res spin,(hl) ; if rolls = 3, disable rolling
  324. jp KeywaitLoop
  325.  
  326.  
  327. EnterScore:
  328. ld hl,comboScores
  329. ld a,(selectedCombo) \ ld c,a \ ld b,0
  330. add hl,bc
  331. ld a,(hl) \ or a \ jr nz,{1@}
  332. ld a,(ptsforSelected) \ ld (hl),a
  333. inc a \ jr z,{1@}
  334. ld hl,(gameScore)
  335. ld bc,(ptsforSelected)
  336. add hl,bc ; add score to game score
  337. xor a \ ld b,a \ ld a,(rollBonuses) \ ld c,a
  338. add hl,bc ; add bonuses to game score
  339. ld (gameScore),hl ; write new score back
  340. ld hl,entryMode \ res score,(hl) \ set spin,(hl) ; disable scoring, enable spinning
  341. ld a,0 \ ld (curRolls),a ; set back to 0 rolls
  342. call renderScore
  343. @: jp KeywaitLoop
  344.  
  345. QuitGame:
  346. ld hl,GameSave \ rst 20h \ bcall(_ChkFindSym)
  347. jr nc,{1@} ; if save exists already, skip to saving
  348. ld hl,saveSize \ bcall(_CreateAppVar) ; if it doesn't, create it
  349. @: ld hl,comboScores \ inc de \ inc de ; set read address to RAM state, set write address to after size word
  350. ld bc,saveSize \ ldir ; set size, load
  351. set textWrite,(iy+sGrFlags)
  352. bcall(_ClrLCDFull) ; clear screen
  353. ret ; exit game
  354.  
  355. renderDie:
  356. ld hl,dieValues-1 \ ld b,5
  357. ld e,$01
  358. @: inc hl \ ld a,(hl)
  359. ld c,a
  360. add a,a
  361. add a,c
  362. add a,a
  363. add a,a
  364. add a,a
  365. ld hl,Sprites
  366. add a,l
  367. ld l,a
  368. jr nc,$+3
  369. inc h
  370. ld d,$40
  371. push bc
  372. call drawSprite_6_Bit
  373. pop bc
  374. inc e \ inc e \ djnz {-1@}
  375. ret
  376.  
  377.  
  378. renderScore:
  379. res fracDrawLFont, (iy + fontFlags) ; set small font
  380. xor a \ ld (penCol),a \ ld (penRow),a
  381. ld hl,txt1 \ bcall(_vPutS)
  382. ld hl,(highScore) \ call HL2str
  383. bcall(_vPutS)
  384. set fracDrawLFont, (iy + fontFlags) ; set large font
  385. ld hl,(gameScore)
  386. call HL2str
  387. ld a,10 \ ld (penRow),a \ xor a \ ld (penCol),a
  388. bcall(_vPutS)
  389. ret
  390. txt1:
  391. .db "HS: ",0
  392.  
  393.  
  394.  
  395. rendertPoints:
  396. ld a,(selectedCombo)
  397. ld b,a
  398. add a,a \ add a,a \ add a,a \ sub b
  399. ld c,a \ xor b
  400. @: ld hl,ComboNameText \ add hl,bc
  401. djnz {-1@}
  402. @: res fracDrawLFont, (iy + fontFlags) ; set small font
  403. ld a,0 \ ld (penRow),a
  404. ld a,48 \ ld (penCol),a
  405. bcall(_vPutS)
  406. ld hl,penCol \ inc (hl) \ inc (hl) \ inc (hl) \ inc (hl)
  407. ld a,(ptsforSelected) \ ld l,a \ ld h,0 \ call HL2str
  408. bcall(_vPutS)
  409. ld a,(rollBonuses) \ or a \ ret z
  410. ld l,a \ ld h,0 \ call HL2str
  411. ld a,7 \ ld (penRow),a
  412. ld a,48 \ ld (penCol),a
  413. bcall(_vPutS)
  414. ret
  415.  
  416. die_setZero:
  417. ld hl,dieValues ; set read address to dieValues
  418. ld a,0 \ ld (hl),a ; set dieValues to 0
  419. ld de,dieValues+1 ; set write address to dieValues+1
  420. ld bc,4 \ ldir ; copy 4 bytes from read address to write address
  421. ret
  422.  
  423. zeroData:
  424. ld a,0
  425. ld (hl),a \ inc hl
  426. djnz zeroData
  427. ret
  428.  
  429. ; ########################
  430. ; Random Number Generator
  431. ; ########################
  432.  
  433. prng16:
  434. ;collab with Runer112
  435. ;;Output:
  436. ;; HL is a pseudo-random int
  437. ;; A and BC are also, but much weaker and smaller cycles
  438. ;; Preserves DE
  439. ;;148cc, super fast
  440. ;;26 bytes
  441. ;;period length: 4,294,901,760
  442. seed1=$+1
  443. ld hl,9999
  444. ld b,h
  445. ld c,l
  446. add hl,hl
  447. add hl,hl
  448. inc l
  449. add hl,bc
  450. ld (seed1),hl
  451. seed2=$+1
  452. ld hl,987
  453. add hl,hl
  454. sbc a,a
  455. and %00101101
  456. xor l
  457. ld l,a
  458. ld (seed2),hl
  459. add hl,bc
  460. ret
  461. roll:
  462. ;;Input: A is the range.
  463. ;;Output: Returns in A a random number from 0 to B-1.
  464. ;; B=0
  465. ;; DE is not changed
  466. ;;Destroys:
  467. ;; HL
  468. ;;Speed:
  469. ;; 322cc to 373cc, 347.5cc average
  470. inc hl
  471. ld a,(hl) \ add a,a
  472. ret nc
  473. push hl
  474. call prng16
  475. ld l,h \ ld h,0
  476. ld b,h \ ld c,l
  477. add hl,hl \ add hl,bc \ add hl,hl
  478. inc h \ ld a,h
  479. pop hl
  480. ld (hl),a
  481. ret
  482.  
  483. ; ########################
  484. ; ## Rendering Routines ##
  485. ; ########################
  486. ;
  487. ; input: d = row, e = column
  488.  
  489. drawSprite_6_Bit:
  490. ld a,d \ add a,$79 ; Y coord into A
  491. ld b,12 ; Loop this 12 times
  492. drawSprite_outerloop:
  493. inc a \ push af
  494. out ($10),a \ call lcdwait
  495. ld a,e \ add a,$20
  496. out ($10),a \ call lcdwait ; use outi instead
  497. ld c,$11
  498. outi \ call lcdwait
  499. outi \ call lcdwait
  500. pop af
  501. djnz drawSprite_outerloop
  502. ret
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511. lcdwait:
  512. in a,($10) ;bit 7 set if LCD is busy
  513. rla \ jr c,lcdwait
  514. ret
  515.  
  516.  
  517.  
  518. HL2str:
  519. ld de,OP1+6
  520. ld c,10
  521. xor a
  522. ld (de),a
  523. @:
  524. dec de
  525. ld b,13
  526. xor a
  527. add hl,hl \ rla
  528. add hl,hl \ rla
  529. add hl,hl \ rla
  530. add hl,hl \ rla \ cp c \ jr c,$+4 \ inc l \ sub c
  531. djnz $-7
  532. add a,$30
  533. ld (de),a
  534. ld a,h
  535. or l
  536. jr nz,{-1@}
  537. ex de,hl
  538. ret
  539.  
  540.  
  541. GameSave:
  542. .db AppVarObj,"YatSave",0
  543.  
  544. ComboNameText:
  545. .db "Ones ",0
  546. .db "Twos ",0
  547. .db "Threes",0
  548. .db "Fours ",0
  549. .db "Fives ",0
  550. .db "Sixes ",0
  551. .db "3.of.a",0
  552. .db "4.of.a.",0
  553. .db "F.Hse.",0
  554. .db "Sm.St.",0
  555. .db "Lg.St.",0
  556. .db "Chance",0
  557. .db "Yatzee",0
  558.  
  559.  
  560. Sprites:
  561. .db %11111111,%11110000
  562. .db %10000000,%00010000
  563. .db %10000000,%00010000
  564. .db %10000000,%00010000
  565. .db %10000000,%00010000
  566. .db %10000000,%00010000
  567. .db %10000000,%00010000
  568. .db %10000000,%00010000
  569. .db %10000000,%00010000
  570. .db %10000000,%00010000
  571. .db %10000000,%00010000
  572. .db %11111111,%11110000
  573. ;sprite_OneDie:
  574. .db %11111111,%11110000
  575. .db %10000000,%00010000
  576. .db %10000000,%00010000
  577. .db %10000000,%00010000
  578. .db %10000000,%00010000
  579. .db %10000110,%00010000
  580. .db %10000110,%00010000
  581. .db %10000000,%00010000
  582. .db %10000000,%00010000
  583. .db %10000000,%00010000
  584. .db %10000000,%00010000
  585. .db %11111111,%11110000
  586. ;sprite_TwoDie:
  587. .db %11111111,%11110000
  588. .db %10000000,%00010000
  589. .db %10000000,%00010000
  590. .db %10000110,%00010000
  591. .db %10000110,%00010000
  592. .db %10000000,%00010000
  593. .db %10000000,%00010000
  594. .db %10000110,%00010000
  595. .db %10000110,%00010000
  596. .db %10000000,%00010000
  597. .db %10000000,%00010000
  598. .db %11111111,%11110000
  599. ; three die
  600. .db %11111111,%11110000
  601. .db %10000000,%00010000
  602. .db %10000000,%11010000
  603. .db %10000000,%11010000
  604. .db %10000000,%00010000
  605. .db %10000110,%00010000
  606. .db %10000110,%00010000
  607. .db %10000000,%00010000
  608. .db %10110000,%00010000
  609. .db %10110000,%00010000
  610. .db %10000000,%00010000
  611. .db %11111111,%11110000
  612. ; four die
  613. .db %11111111,%11110000
  614. .db %10000000,%00010000
  615. .db %10110000,%11010000
  616. .db %10110000,%11010000
  617. .db %10000000,%00010000
  618. .db %10000000,%00010000
  619. .db %10000000,%00010000
  620. .db %10000000,%00010000
  621. .db %10110000,%11010000
  622. .db %10110000,%11010000
  623. .db %10000000,%00010000
  624. .db %11111111,%11110000
  625. ; five die
  626. .db %11111111,%11110000
  627. .db %10000000,%00010000
  628. .db %10011001,%10010000
  629. .db %10011001,%10010000
  630. .db %10000000,%00010000
  631. .db %10000110,%00010000
  632. .db %10000110,%00010000
  633. .db %10000000,%00010000
  634. .db %10011001,%10010000
  635. .db %10011001,%10010000
  636. .db %10000000,%00010000
  637. .db %11111111,%11110000
  638. ; six die
  639. .db %11111111,%11110000
  640. .db %10000000,%00010000
  641. .db %10011001,%10010000
  642. .db %10011001,%10010000
  643. .db %10000000,%00010000
  644. .db %10011001,%10010000
  645. .db %10011001,%10010000
  646. .db %10000000,%00010000
  647. .db %10011001,%10010000
  648. .db %10011001,%10010000
  649. .db %10000000,%00010000
  650. .db %11111111,%11110000
  651.  
  652. .end
  653. .END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement