Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Yatzee for TI-83+/84+
- ; Version 1.0
- ; by ACagliano (http://clrhome.org)
- .variablename "YATZEE"
- .nolist
- #include "ti83plus.inc"
- #include "dcs7.inc"
- .list
- ; defines/equates
- #define diceOne 0
- #define diceTwo 1
- #define diceThree 2
- #define diceFour 3
- #define diceFive 4
- #define diceSix 5
- #define comboScores saferam1
- #define threeOnes 0
- #define threeTwos 1
- #define threeThrees 2
- #define threeFours 3
- #define threeFives 4
- #define threeSixes 5
- #define threeOfaKind 6
- #define fourOfaKind 7
- #define fullHouse 8
- #define smStraight 9
- #define lgStraight 10
- #define chance 11
- #define yatzee 12
- #define threesBonus 13
- #define gameScore comboScores+threesBonus
- #define entryMode gameScore+2
- #define spin 0
- #define score 1
- #define highScore entryMode+1
- #define curRolls highScore+2
- #define dieValues curRolls+1
- #define dieHoldFlags dieValues+5
- #define threesPoints dieHoldFlags+1
- #define saveSize threesPoints-comboScores
- #define selectedCombo dieHoldFlags+1
- #define ptsforSelected selectedCombo+1
- #define rollBonuses ptsforSelected+2
- #define ctOnes rollBonuses+1
- #define ctTwos ctOnes+1
- #define ctThrees ctTwos+1
- #define ctFours ctThrees+1
- #define ctFives ctFours+1
- #define ctSixes ctFives+1
- .org progstart
- .db t2ByteTok, tAsmCmp
- Start:
- bcall(_ClrLCDFull)
- ld a,r ;\
- ld h,a ; |
- xor 77 ; |Seed the RNG
- ld l,a ; |
- ld (seed1),hl ; |
- ld (seed2),hl ;/
- res textWrite,(iy+sGrFlags)
- ld a,7 \ out ($10),a
- ld hl,gameScore \ ld b,ctSixes-comboScores \ call zeroData ; zero the RAM savestate
- ld hl,GameSave \ rst 20h \ bcall(_ChkFindSym) ; \
- jr c,StartGame ; | check whether save file exists
- ld a,b \ or a \ jr z,{1@} ; | if it doesn't skip to game start
- bcall(_Arc_Unarc) \ bcall(_ChkFindSym) ; | if it does and is unarchived, skip to loading
- @: ex de,hl \ ld de,comboScores ; | if it does and is archived, unarchive it
- inc hl \ inc hl ; | load the savestate into RAM
- ld bc,saveSize ; |
- ldir ; /
- StartGame:
- call renderDie
- call renderScore
- ld hl,entryMode \ set spin,(hl) ; enable spinning, but not scoring yet
- KeywaitLoop:
- ld a,(kbdScanCode) \ or a \ jr z,KeywaitLoop
- cp skClear \ jp z,QuitGame
- ; cp skMode \ jp renderInstructions
- ld hl,entryMode
- bit score,(hl) \ jr z,{1@} ; if the score bit is reset, you are not allowed to score now
- cp skEnter \ jp z,EnterScore
- cp skLeft \ jr nz,$+11 \ ld hl,selectedCombo \ dec (hl) \ call calcScore \ jr KeywaitLoop
- cp skRight \ jr nz,$+11 \ ld hl,selectedCombo \ inc (hl) \ call calcScore \ jr KeywaitLoop
- bit spin,(hl) \ jr z,KeywaitLoop ; if the spin bit is reset, you are not allowed to spin now
- @: sub 54 \ add a,5 \ jr c,holdDice
- add a,49
- cp sk2nd \ jp z, throwDie
- jr KeywaitLoop
- holdDice:
- ld hl,dieValues
- push af
- add a,l \ ld l,a \ jr nc,$+3
- inc h \ ld a,(hl)
- xor 80h \ ld (hl),a
- pop af \ call eraseHoldText
- bit 7,a \ call nz,showHoldText
- jp KeywaitLoop
- eraseHoldText
- push af
- add a,a \ add a,a \ add a,a \ add a,a
- add a,10
- ld h,a \ ld l,52
- ld (penCol),hl
- ld hl,txterase \ bcall(_vPutS)
- pop af
- ret
- showHoldText:
- add a,a \ add a,a \ add a,a \ add a,a
- add a,10
- ld h,a \ ld l,52
- ld (penCol),hl
- ld hl,txthold \ bcall(_vPutS)
- ret
- txterase:
- .db " ",0
- txthold:
- .db "Hold",0
- ; ### Select Combo and Compute Score ###
- calcScore:
- #define checkQuantity inc hl \ sub (hl)
- ld hl,dieValues-1
- ld bc,$0507
- inc hl
- ld a,(hl) \ and c ; mask out the flag bits of the die
- ld hl,ctOnes
- add a,l
- ld l,a
- jr nc,$+3
- inc h
- inc (hl)
- djnz $-11
- ; call getDieValue
- ; call getDieValue
- ; call getDieValue
- ; call getDieValue
- ; call getDieValue
- ; now we test the combo that is active. Score is either returned, or 0.
- 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
- ld a,(selectedCombo)
- ld hl,jmptbl
- add a,a \ ld c,a \ xor b
- add hl,bc
- jp (hl)
- jmptbl:
- .dw isThreeOnes
- .dw isThreeTwos
- .dw isThreeThrees
- .dw isThreeFours
- .dw isThreeFives
- .dw isThreeSixes
- .dw isThreeOfaKind
- .dw isFourOfaKind
- .dw isfullHouse
- .dw isSmStraight
- .dw isLgStraight
- .dw sumtheDie
- .dw isYatzee
- calcBonuses:
- dec a \ ld (ptsforSelected),a
- call isYatzee \ or a \ jr z,{1@}
- dec a \ ld (rollBonuses),a
- @: ld a,(threesBonus) \ or a \ call z,isFullTopSix
- ld hl,rollBonuses \ add a,(hl)
- call rendertPoints
- ret
- isThreeOnes:
- ld a,(ctOnes) \ inc a \ ret
- isThreeTwos:
- ld a,(ctTwos)
- add a,a \ inc a \ ret
- isThreeThrees:
- ld a,(ctThrees)
- ld b,a \ add a,a \ add a,b
- inc a \ ret
- isThreeFours:
- ld a,(ctFours)
- add a,a \ add a,a
- inc a \ ret
- isThreeFives:
- ld a,(ctFives)
- ld b,a \ add a,a \ add a,a \ add a,b
- inc a \ ret
- isThreeSixes:
- ld a,(ctSixes)
- ld b,a
- add a,a
- add a,b
- add a,a
- inc a \ ret
- isThreeOfaKind:
- ld a,2 \ jr {1@}
- isFourOfaKind:
- ld a,3
- @: ld hl,ctOnes-1
- checkQuantity \ jp c,sumtheDie
- checkQuantity \ jp c,sumtheDie
- checkQuantity \ jp c,sumtheDie
- checkQuantity \ jp c,sumtheDie
- checkQuantity \ jp c,sumtheDie
- ld a,0
- ret
- isFullHouse:
- ld a,2 \ ld hl,ctOnes-1
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- jr {3@}
- @: ld a,1 \ ld hl,ctOnes-1
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- jr {2@}
- @: ld a,26 \ ret
- @: ld a,0 \ ret
- isSmStraight:
- ld hl,ctOnes \ ld a,(hl)
- or a \ jr z,strt_start_2
- ld b,3
- inc hl \ and (hl)
- djnz $-2
- jr nz,{1@}
- strt_start_2:
- inc hl \ ld a,(hl)
- or a \ jr z,strt_start_3
- ld b,3
- inc hl \ and (hl)
- djnz $-2
- jr nz,{1@}
- strt_start_3:
- inc hl \ ld a,(hl)
- or a \ ret z
- ld b,3
- inc hl \ and (hl)
- djnz $-2
- ret z
- @: ld a,31 \ ret
- isLgStraight
- ld hl,ctOnes \ ld a,(hl)
- or a \ jr z,strt_1_to_5
- ;strt_2_to_6:
- inc hl
- strt_1_to_5:
- ld a,(hl) \ ld b,3
- inc hl \ and (hl)
- djnz $-2
- ret z
- ld a,41 \ ret
- @: ld a,0 \ ret
- isYatzee:
- ld hl,ctOnes-1 \ ld a,4
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- checkQuantity \ jr c,{1@}
- ld a,0 \ ret
- @: ld a,51 \ ret
- isFullTopSix:
- ld hl,comboScores \ xor a
- ld b,6
- @: inc a \ jr z,$+3 \ add a,(hl) \ inc hl
- djnz {-1@}
- cp 62 \ jr c,{1@}
- ld a,0 \ ret
- @: ld a,1 \ ld (threesBonus),a \ ld a,35 \ ret
- sumtheDie:
- ld a,0 \ ld hl,dieValues
- ld b,5
- @: add a,(hl) \ inc hl
- djnz {-1@}
- inc a
- ret
- throwDie:
- ; randomize the value of each die, if the hold flag for that die is not set
- ld hl,dieValues-1
- call roll
- call roll
- call roll
- call roll
- call roll
- call renderDie
- ld hl,entryMode \ set score,(hl) ; allow scoring once die rolled
- ld hl,curRolls \ inc (hl) ; increment number of rolls by one
- ld a,3 \ sub (hl) \ jp nz,KeywaitLoop ; if rolls != 3, leave routine
- ld hl,entryMode \ res spin,(hl) ; if rolls = 3, disable rolling
- jp KeywaitLoop
- EnterScore:
- ld hl,comboScores
- ld a,(selectedCombo) \ ld c,a \ ld b,0
- add hl,bc
- ld a,(hl) \ or a \ jr nz,{1@}
- ld a,(ptsforSelected) \ ld (hl),a
- inc a \ jr z,{1@}
- ld hl,(gameScore)
- ld bc,(ptsforSelected)
- add hl,bc ; add score to game score
- xor a \ ld b,a \ ld a,(rollBonuses) \ ld c,a
- add hl,bc ; add bonuses to game score
- ld (gameScore),hl ; write new score back
- ld hl,entryMode \ res score,(hl) \ set spin,(hl) ; disable scoring, enable spinning
- ld a,0 \ ld (curRolls),a ; set back to 0 rolls
- call renderScore
- @: jp KeywaitLoop
- QuitGame:
- ld hl,GameSave \ rst 20h \ bcall(_ChkFindSym)
- jr nc,{1@} ; if save exists already, skip to saving
- ld hl,saveSize \ bcall(_CreateAppVar) ; if it doesn't, create it
- @: ld hl,comboScores \ inc de \ inc de ; set read address to RAM state, set write address to after size word
- ld bc,saveSize \ ldir ; set size, load
- set textWrite,(iy+sGrFlags)
- bcall(_ClrLCDFull) ; clear screen
- ret ; exit game
- renderDie:
- ld hl,dieValues-1 \ ld b,5
- ld e,$01
- @: inc hl \ ld a,(hl)
- ld c,a
- add a,a
- add a,c
- add a,a
- add a,a
- add a,a
- ld hl,Sprites
- add a,l
- ld l,a
- jr nc,$+3
- inc h
- ld d,$40
- push bc
- call drawSprite_6_Bit
- pop bc
- inc e \ inc e \ djnz {-1@}
- ret
- renderScore:
- res fracDrawLFont, (iy + fontFlags) ; set small font
- xor a \ ld (penCol),a \ ld (penRow),a
- ld hl,txt1 \ bcall(_vPutS)
- ld hl,(highScore) \ call HL2str
- bcall(_vPutS)
- set fracDrawLFont, (iy + fontFlags) ; set large font
- ld hl,(gameScore)
- call HL2str
- ld a,10 \ ld (penRow),a \ xor a \ ld (penCol),a
- bcall(_vPutS)
- ret
- txt1:
- .db "HS: ",0
- rendertPoints:
- ld a,(selectedCombo)
- ld b,a
- add a,a \ add a,a \ add a,a \ sub b
- ld c,a \ xor b
- @: ld hl,ComboNameText \ add hl,bc
- djnz {-1@}
- @: res fracDrawLFont, (iy + fontFlags) ; set small font
- ld a,0 \ ld (penRow),a
- ld a,48 \ ld (penCol),a
- bcall(_vPutS)
- ld hl,penCol \ inc (hl) \ inc (hl) \ inc (hl) \ inc (hl)
- ld a,(ptsforSelected) \ ld l,a \ ld h,0 \ call HL2str
- bcall(_vPutS)
- ld a,(rollBonuses) \ or a \ ret z
- ld l,a \ ld h,0 \ call HL2str
- ld a,7 \ ld (penRow),a
- ld a,48 \ ld (penCol),a
- bcall(_vPutS)
- ret
- die_setZero:
- ld hl,dieValues ; set read address to dieValues
- ld a,0 \ ld (hl),a ; set dieValues to 0
- ld de,dieValues+1 ; set write address to dieValues+1
- ld bc,4 \ ldir ; copy 4 bytes from read address to write address
- ret
- zeroData:
- ld a,0
- ld (hl),a \ inc hl
- djnz zeroData
- ret
- ; ########################
- ; Random Number Generator
- ; ########################
- prng16:
- ;collab with Runer112
- ;;Output:
- ;; HL is a pseudo-random int
- ;; A and BC are also, but much weaker and smaller cycles
- ;; Preserves DE
- ;;148cc, super fast
- ;;26 bytes
- ;;period length: 4,294,901,760
- seed1=$+1
- ld hl,9999
- ld b,h
- ld c,l
- add hl,hl
- add hl,hl
- inc l
- add hl,bc
- ld (seed1),hl
- seed2=$+1
- ld hl,987
- add hl,hl
- sbc a,a
- and %00101101
- xor l
- ld l,a
- ld (seed2),hl
- add hl,bc
- ret
- roll:
- ;;Input: A is the range.
- ;;Output: Returns in A a random number from 0 to B-1.
- ;; B=0
- ;; DE is not changed
- ;;Destroys:
- ;; HL
- ;;Speed:
- ;; 322cc to 373cc, 347.5cc average
- inc hl
- ld a,(hl) \ add a,a
- ret nc
- push hl
- call prng16
- ld l,h \ ld h,0
- ld b,h \ ld c,l
- add hl,hl \ add hl,bc \ add hl,hl
- inc h \ ld a,h
- pop hl
- ld (hl),a
- ret
- ; ########################
- ; ## Rendering Routines ##
- ; ########################
- ;
- ; input: d = row, e = column
- drawSprite_6_Bit:
- ld a,d \ add a,$79 ; Y coord into A
- ld b,12 ; Loop this 12 times
- drawSprite_outerloop:
- inc a \ push af
- out ($10),a \ call lcdwait
- ld a,e \ add a,$20
- out ($10),a \ call lcdwait ; use outi instead
- ld c,$11
- outi \ call lcdwait
- outi \ call lcdwait
- pop af
- djnz drawSprite_outerloop
- ret
- lcdwait:
- in a,($10) ;bit 7 set if LCD is busy
- rla \ jr c,lcdwait
- ret
- HL2str:
- ld de,OP1+6
- ld c,10
- xor a
- ld (de),a
- @:
- dec de
- ld b,13
- xor a
- add hl,hl \ rla
- add hl,hl \ rla
- add hl,hl \ rla
- add hl,hl \ rla \ cp c \ jr c,$+4 \ inc l \ sub c
- djnz $-7
- add a,$30
- ld (de),a
- ld a,h
- or l
- jr nz,{-1@}
- ex de,hl
- ret
- GameSave:
- .db AppVarObj,"YatSave",0
- ComboNameText:
- .db "Ones ",0
- .db "Twos ",0
- .db "Threes",0
- .db "Fours ",0
- .db "Fives ",0
- .db "Sixes ",0
- .db "3.of.a",0
- .db "4.of.a.",0
- .db "F.Hse.",0
- .db "Sm.St.",0
- .db "Lg.St.",0
- .db "Chance",0
- .db "Yatzee",0
- Sprites:
- .db %11111111,%11110000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %11111111,%11110000
- ;sprite_OneDie:
- .db %11111111,%11110000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000110,%00010000
- .db %10000110,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %11111111,%11110000
- ;sprite_TwoDie:
- .db %11111111,%11110000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000110,%00010000
- .db %10000110,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000110,%00010000
- .db %10000110,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %11111111,%11110000
- ; three die
- .db %11111111,%11110000
- .db %10000000,%00010000
- .db %10000000,%11010000
- .db %10000000,%11010000
- .db %10000000,%00010000
- .db %10000110,%00010000
- .db %10000110,%00010000
- .db %10000000,%00010000
- .db %10110000,%00010000
- .db %10110000,%00010000
- .db %10000000,%00010000
- .db %11111111,%11110000
- ; four die
- .db %11111111,%11110000
- .db %10000000,%00010000
- .db %10110000,%11010000
- .db %10110000,%11010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10000000,%00010000
- .db %10110000,%11010000
- .db %10110000,%11010000
- .db %10000000,%00010000
- .db %11111111,%11110000
- ; five die
- .db %11111111,%11110000
- .db %10000000,%00010000
- .db %10011001,%10010000
- .db %10011001,%10010000
- .db %10000000,%00010000
- .db %10000110,%00010000
- .db %10000110,%00010000
- .db %10000000,%00010000
- .db %10011001,%10010000
- .db %10011001,%10010000
- .db %10000000,%00010000
- .db %11111111,%11110000
- ; six die
- .db %11111111,%11110000
- .db %10000000,%00010000
- .db %10011001,%10010000
- .db %10011001,%10010000
- .db %10000000,%00010000
- .db %10011001,%10010000
- .db %10011001,%10010000
- .db %10000000,%00010000
- .db %10011001,%10010000
- .db %10011001,%10010000
- .db %10000000,%00010000
- .db %11111111,%11110000
- .end
- .END
Add Comment
Please, Sign In to add comment