Page 1 of 1

Highscore tool

Posted: Sat Jun 09, 2007 4:18 pm
by michel51
I've built an highscore tool for an project of my own.

It works, and contained all what is needed for an highscore.

Code: Select all

; HighScore
;
; Original from STARGATE (from german forum)
; modified and extended by michel51 in Juni 2007
;
; built with Mac PB V.4.00 Beta 1
;
; tested on Mac OS X and Windwow XP
; Linux not tested, but it should run because only PB-comands are used.
;
; 

Procedure ShowHighScore(File.s)
   If OpenWindow(10, 0, 0, 260, 300, "Highscore", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
      If CreateGadgetList(WindowID(10))
         ListIconGadget(0, 10, 10, 240, 240, "Pos", 50, #PB_ListIcon_GridLines)
         AddGadgetColumn(0, 1, "Name", 100)
         AddGadgetColumn(0, 2, "Points", 60)
         If ReadFile(1, File)
; ------- activate here the wished loop -----------------         
;             While Eof(1) = 0      ; all will be shown
            For I = 1 To 10         ; only the best 10 will be shown
               String.s = ReadString(1)
               Name.s = Left(String, 10)
               Points.s = Mid(String, 11, 5)
               pos+1
               AddGadgetItem(0, -1, Str(pos)+Chr(10)+Name + Chr(10) + Points)
            Next                    ; only the best 10 will be shown
;             Wend                  ; all will be shown
; -------------------------------------------------------
         EndIf
         CloseFile(1)
      EndIf
   EndIf
EndProcedure

Procedure HighScore(File.s, Name.s, Points.l)
   If OpenFile(1, File)             ; File will be opened or created.
      If CreateFile(2, "_"+File)    ; helpfile will be created
; ------ activate here the wished loop -----------------         
;       Repeat             ; all will be saved
      For i = 1 To 10      ; only the best 10 will be saved
         String.s = ReadString(1)
         HS_Points = Val(Trim(Mid(String, 11, 10)))
         
         If HS_Points < Points And entered = 0:    ; compare points
            WriteStringN(2, LSet(name, 10, " ")+" "+Str(points))
            entered = 1
         EndIf
         WriteStringN(2, String)
      Next                 ; only the best 10 will be saved
;       Until String = ""  ; all will be saved
; -----------------------------------------------------
      CloseFile(2)
      EndIf
      CloseFile(1)
   EndIf
   CopyFile("_"+file, file)
   DeleteFile("_"+file)      ; ready, Highscore is saved to file
   
EndProcedure
; 
HSFile.s = "HScore_e.score"
; the file name should be adapted after own taste

; Example calls

HighScore(HSFile, "Test 1", 23)
HighScore(HSFile, "martin", 4)
HighScore(HSFile, "Stargate", 124)
HighScore(HSFile, "LOL", 1)
HighScore(HSFile, "baum", 2341) 
HighScore(HSFile, "Namesehrlang", 55)   ; very lon name for testing
HighScore(HSFile, "Klaus", 198)

ShowHighScore(HSFile)     ; ... and show the highscore

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
End
It's not perfect, surely, but it does which it is.

Happy "Highscoring" :D

Posted: Sat Jun 09, 2007 9:32 pm
by Joakim Christiansen
One thing to also do is to add a "cheat protection", I did that in my highscore list by writing a CRC32 fingerprint a the end of the file. And then check it every time I load the list to see if anyone has changed it without updating the CRC32. Would stop the basic-computer-user from cheating...

Posted: Sat Jun 09, 2007 10:06 pm
by michel51
Joakim Christiansen wrote:One thing to also do is to add a "cheat protection", I did that in my highscore list by writing a CRC32 fingerprint a the end of the file. And then check it every time I load the list to see if anyone has changed it without updating the CRC32. Would stop the basic-computer-user from cheating...
Good idea, but I don't know, how to do. Can you help with a little bit code?

Another thing to insert is an option to switch the sortdirection. Sometimes the best is one with the fewest points.

But the code is the first try and should be a simple possibility for an highscore