Hey everyone!
I'm creating a game, with a highscore function. There's one problem: there is a bug in it, and it seems like I can't figure out where. So I turn to you, asking for help.
The game has 6 modes, so 6 arrays of highscores. Every highscore is 2 bytes in length, so there are 6 arrays of 6 highscores (12 bytes per array).
The whole program works, except for the time that your score is too low to be in the highscore table.
C = Number of bytes to skip to get to the correct array of highscores (it DOES work).
HL = Score.
HighScoreTable = Address of highscoretable.
So here's the code! Enjoy!
Code:
I'm creating a game, with a highscore function. There's one problem: there is a bug in it, and it seems like I can't figure out where. So I turn to you, asking for help.
The game has 6 modes, so 6 arrays of highscores. Every highscore is 2 bytes in length, so there are 6 arrays of 6 highscores (12 bytes per array).
The whole program works, except for the time that your score is too low to be in the highscore table.
C = Number of bytes to skip to get to the correct array of highscores (it DOES work).
HL = Score.
HighScoreTable = Address of highscoretable.
So here's the code! Enjoy!
Code:
GameEnd:
PUSH HL ;Save the score.
LD D, H
LD E, L
LD HL, $0303
LD (CurRow), HL
LD HL, TimeIsUp
b_call(_PutS) ;HL
LD HL, $0104
LD (CurRow), HL
LD HL, TimeIsUp2
b_call(_PutS) ;HL
GameEndGetKey:
PUSH DE
b_call(_GetKey) ;DE/HL
POP DE
CP kEnter
JR NZ, GameEndGetKey
PUSH DE
PUSH BC
b_call(_ClrLCDFull) ;AF/BC/DE/HL
POP BC
POP DE
LD HL, HighScoreTable ;DE = Score, HL = HighScoreAddress.
LD B, 0
ADD HL, BC ;C holds the number of bytes to be skipped.
PUSH HL ;We need it again when we display the highscores.
LD BC, 12 ;BC = Number of bytes left to check.
CheckHighScore:
LD A, (HL)
CP D
JR C, HighScoreHigher2
JR NZ, IncrementHLTwice
INC HL
LD A, (HL)
CP E
JR C, HighScoreHigher
JR IncrementHLOnce
IncrementHLTwice: INC HL
INcrementHLOnce: INC HL
DEC BC
DEC BC
JR Z, DisplayHighScoreTable
JR CheckHighScore
HighScoreHigher: DEC HL
HighScoreHigher2:
DEC BC ;BC now holds the number of bytes to be moved.
DEC BC ;But the last highscore needs to be overwritten in stead of removed, so BC is declined.
JR Z, SkipMove
PUSH DE
PUSH HL
ADD HL, BC
LD D, H
LD E, L
INC DE
INC DE
LDDR
POP HL
POP DE
SkipMove:
LD (HL), D
INC HL
LD (HL), E
DisplayHighScoreTable:
LD HL, $0200
LD (CurRow), HL
LD HL, HighScoreTableText
b_call(_PutS) ;HL
LD IX, CurRow
LD HL, $0B01
LD (IX), L
LD (IX + 1), H
POP HL ;Restore baseaddress for highscores to display.
LD B, 6 ;6 highscores to display
DisplayHighScore:
LD D, (HL)
INC HL
LD E, (HL)
INC HL
EX DE, HL ;DE = Address of highscore to display, HL = HighScore
PUSH DE
b_call(_DispHL) ;AF/DE/HL
POP DE
INC (IX)
LD A, $0B
LD (IX + 1), A
EX DE, HL ;HL = Address of highscore to display, DE = Crap
DJNZ DisplayHighScore
POP HL ;Restore your score
b_call(_DispHL) ;AF/DE/HL
LD HL, $0007
LD (CurRow), HL
LD HL, YourScore
b_call(_PutS) ;HL
DisplayHighScoreGetkey:
b_call(_getKey) ;DE/HL
CP kEnter
JR NZ, DisplayHighScoreGetKey
JP StartProgram
HighScoreTable: .DW $0000, $0000, $0000, $0000, $0000, $0000
.DW $0000, $0000, $0000, $0000, $0000, $0000
.DW $0000, $0000, $0000, $0000, $0000, $0000
.DW $0000, $0000, $0000, $0000, $0000, $0000
.DW $0000, $0000, $0000, $0000, $0000, $0000
.DW $0000, $0000, $0000, $0000, $0000, $0000