Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411588 Posts in 69386 Topics- by 58443 Members - Latest Member: Mansreign

May 06, 2024, 05:52:53 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Stupid data structures and while statements question
Pages: [1]
Print
Author Topic: Stupid data structures and while statements question  (Read 1171 times)
pgil
Guest
« on: May 12, 2010, 12:02:06 PM »

So I have a high score list that I'm saving in a binary file. I want to compare the player's score with those on the list, and put the player's score in the proper place (in order from highest to lowest).

This script:

Code:
pos = 4
if score <= ds_list_find_value(global.hiscores,pos) break

while score > ds_list_find_value(global.hiscores,pos){
   pos-=1
   if pos <= 0 break
}

ds_list_insert(global.hiscores,pos,score)
ds_list_insert(global.hinames,pos,players_name)

...Just places the most recent score at the top. What am i doing wrong?
Logged
Falmil
Level 6
*


View Profile
« Reply #1 on: May 12, 2010, 02:35:09 PM »

I don't know GML, but the code looks alright to me. Do you have an example of the incorrect score output?
Logged
___
Vice President of Marketing, Romeo Pie Software
Level 10
*


View Profile
« Reply #2 on: May 12, 2010, 03:11:59 PM »

Why do you start with pos = 4?  Are there only 3 scores or something?
Logged
pgil
Guest
« Reply #3 on: May 12, 2010, 03:30:35 PM »

I'm only saving the top 5 scores. It should check places 4 through 0, and stop when it gets to a score higher than what the current player has. Instead, it doesn't seem to stop at all, and just places the newest score at the top (at pos=0).
Logged
___
Vice President of Marketing, Romeo Pie Software
Level 10
*


View Profile
« Reply #4 on: May 12, 2010, 03:51:20 PM »

Code:
if score <= ds_list_find_value(global.hiscores,pos) break

"break" wont stop the script.  I think what you want here is "exit"

Also, I suggest you use the popup windows in Game Maker for debugging.  Like send_message().  At least, I think that's the function.  You can then check to see what each loop is doing.
Logged
Pineapple
Level 10
*****

~♪


View Profile WWW
« Reply #5 on: May 12, 2010, 06:03:15 PM »

Here's an example of the high-scores code in my BlitzMax project. It won't be copy-pastable, but it'll give you a good idea of how it works.

scores is an array containing all the scores in the table. sname is the array containing the 3-letter names to go along with each score. scorenum is the number of scores to record in the table, basescore is the lowest score, and scoreinc is how much each consecutive score increases by. The score at array index 0 is the highest score, and the last index is the lowest in the table. highscores.dat is the file I save the scores to, the first byte tell how many scores are in the table to prevent silly conflicts. Then all the scores and names in the array are recorded, in order.

writescores creates a new highscore file. readscores reads and puts in memory the scores saved in the highscore file. savescores saves the scores currently in memory to the file. addscore is the function I call whenever the player finishes a game to check if the player's score is higher than the lowest score on the table, and if it is it provides a light interface to enter their initials or w/e, and then shifts all the scores that will be below the newly placed score down to the next position in the arrat, then adds the new score and name at the appropriate place in the array.


Code:
Global scorenum%=20,basescore%=14000,scoreinc%=2000
Global scores%[scorenum],sname$[scorenum]

readscores


Function writescores()
Local fil:TStream=WriteFile("highscores.dat")
WriteByte fil,scorenum
For Local ws%=0 To scorenum-1
scores[ws]=(scorenum-1-ws)*scoreinc+basescore
WriteInt fil,scores[ws]
sname[ws]="---"
WriteString fil,Left(sname[ws],3)
Next
CloseFile fil
End Function
Function savescores()
Local fil:TStream=WriteFile("highscores.dat")
WriteByte fil,scorenum
For Local ws%=0 To scorenum-1
WriteInt fil,scores[ws]
WriteString fil,Left(sname[ws],3)
Next
CloseFile fil
End Function
Function readscores()
If FileType("highscores.dat")<>1 Then
writescores
Else
Local fil:TStream=ReadFile("highscores.dat")
If ReadByte(fil)<>scorenum Then
writescores
CloseFile fil
fil:TStream=ReadFile("highscores.dat")
ReadByte(fil)
EndIf
For Local ws%=0 To scorenum-1
scores[ws]=ReadInt(fil)
sname[ws]=Upper(ReadString(fil,3))
Next
CloseFile fil
EndIf
End Function

Function addscore(pts%)
SetColor 255,255,255
FlushKeys
readscores
If pts<scores[scorenum-1] Then Return

Local eslot%=1,strin$="AAA",req$="QWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()-=_+[]{};':~q,.<>/?\|`~~"
Repeat
Cls

drawstring "High score!",GraphicsWidth()/2,220,1
drawstring pts,GraphicsWidth()/2,240,1

drawstring "Please enter your name.",GraphicsWidth()/2,280,1
drawstring strin,GraphicsWidth()/2,310,1
Select eslot
Case 1 drawstring "_  ",GraphicsWidth()/2,312,1
Case 2 drawstring " _ ",GraphicsWidth()/2,312,1
Case 3 drawstring "  _",GraphicsWidth()/2,312,1
End Select

Local char%=GetChar()
If char=key_enter Then
Local found%=scorenum-1
For Local insert%=0 To scorenum-1
If pts>scores[insert] And found=scorenum-1 Then
found=insert
EndIf
Next
For Local shift%=scorenum-1 To found Step -1
If shift+1<scorenum-1 Then
scores[shift+1]=scores[shift];sname[shift+1]=sname[shift]
EndIf
Next
scores[found]=pts;sname[found]=strin
savescores
FlushKeys
Return
ElseIf char<>0 Then
Local sc$=Upper(Chr(char))
If Instr(req,sc) Then
Select eslot
Case 1 strin=sc+Right(strin,2)
Case 2 strin=Left(strin,1)+sc+Right(strin,1)
Case 3 strin=Left(strin,2)+sc
End Select
eslot:+1
If eslot>3 Then eslot=1
EndIf
EndIf

Flip -1
If KeyDown(27) Or AppTerminate() Then End
Forever

End Function
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic