Any idea how to implement a highscore list?

Monkey Targets Forums/HTML5/Any idea how to implement a highscore list?

Hotshot(Posted 2011) [#1]
Yeah, basically what the title says. I have no idea how to implement a highscore list. Any hints?

Update: this should actually go into the iOS section :(


Volker(Posted 2011) [#2]
I'm going to post this in the code section if it's fully tested.

Class Highscore 
	Field scorelist:List<Score>
	Field maxhighscores:Int=10 ' how may highscores are tracked	
	
	Method New()
		self.scorelist=New List <Score>
	End Method

	Method WorthAnEntry:Bool(points:Int)
		' are this points worth an entry in highscore list?
		If points=0 Then Return False
		If Self.scorelist.Count()<Self.maxhighscores
			Return true
		EndIf
		If self.GetLowestPoints()<points
			Return true
		EndIf
		Return false
	End Method
	
	Method AddScore:void(name:String,points:Int)		
		If name="" Then Return 
		'
		Local newscore:Score= new Score
		newscore.name=name
		newscore.points=points
		self.scorelist.AddLast(newscore)
		self.Sort()
	End Method
	
	Method GetLowestPoints:Int()
			Local sc:Score=Score(Self.scorelist.Last())		
			If sc<>NULL ' Android crashes without check
				Return sc.points	
			endif
			Return 0
	End Method
		
	Method Sort:Void()
		If self.scorelist.Count()<2 Then return
		Local newlist:=new List<Score>
		Local highest:Score=Score(self.scorelist.First())
		Local slsize:Int=self.scorelist.Count()

		repeat
			Local highest:Score=Score(self.scorelist.First())
			For Local s:Score=eachin self.scorelist 
				If s.points>highest.points 
					highest=s	
				EndIf
			Next
			newlist.AddLast(highest)	
			Self.scorelist.Remove(highest)
		Until self.scorelist.Count()=0 ' 
		self.scorelist.Clear()
		
		self.scorelist=newlist'
		' limit list so maximum tracked highscores
		while scorelist.Count()>Self.maxhighscores 
			Self.scorelist.RemoveLast()
		wend
	End Method

	Method DrawTopten:Void()
		Local count:int
		For Local sc:Score=eachin self.scorelist
'			Debuglog("Score:"+sc.points+"  "+sc.name)
'			afont.DrawText("Name:"+sc.name+" Points: "+sc.points,800/2-50,100+count*30)	
			DrawText("Name:"+sc.name+" Points: "+sc.points,10,10+count*10)	
			count+=1
		Next		
	End Method
	
	Method Load:Void()
		Local s:String=LoadState()
'		Debuglog("Loading Highcore, string:"+s)		
		self.FromString(s)
	End Method
	
	Method Save:Void()
		Local s:String=self.ToString()
'		Debuglog("Saving Highcore, string:"+s)
		SaveState(s)	
	End Method
	
	Method remove:Void()
		Self.scorelist.Clear()		
	End Method

	Method ToString:string()
		Local ts:string
		For Local sc:Score=eachin self.scorelist 			
			ts=ts+sc.points+","+sc.name+","
		Next
		Return ts
	End Method
'	
	Method FromString:Void(s:string)
		Self.scorelist.Clear()		
		Local splitted:String[]=s.Split(",")
		For Local count:Int= 0 to splitted.Length-2 step 2
			Local sc:Score=Score.Create(string(splitted [count+1]),int(splitted[count]))			
			Self.scorelist.AddLast(sc)			
		next		
	End Method
End Class




Class Score
	Field name:String
	Field points:Int
	Field ignore:Int 'for sorting
		
	Function Create:Score(name:String,points:Int)
		Local s:Score=new Score
		s.name=name
		s.points=points
		Return s	
	End Function
End Class




Hotshot(Posted 2011) [#3]
Thank you, but "LoadState" and "SaveState" would have been enough hints ;)