|
-
February 18th, 2009, 03:48 PM
#1
Highscore
I want to create a highscore for a simple game wich is stored after the game is closed, and opened again when the game is started a next time.
Its a simple game, so basicaly, I want 10 names to be in the highscore, with a score between 0 and 9999.
Highest should be on top, lowest gets kicked out if a higher one comes in.
I'm not sure how I should do this, can anyone help me?
Thx, Mats
-
February 19th, 2009, 12:51 AM
#2
Re: Highscore
You could create a structure called "HighScore" that contains two things, the name and the score, then create a generic List<T> of those structures, load the contents of the highscores file into that list at bootup and then whenever a new high score is made, create a new HighScore structure, call List<T>.RemoveAt(0) (assuming the list has at least 1 score in it) and then add the new HighScore structure to the list using List<T>.Add(). Then when the user closes the application, save each HighScore structure to the file using a for each loop and a StreamWriter.
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
February 19th, 2009, 05:38 AM
#3
Re: Highscore
 Originally Posted by RaleTheBlade
You could create a structure called "HighScore" that contains two things, the name and the score, then create a generic List<T> of those structures, load the contents of the highscores file into that list at bootup and then whenever a new high score is made, create a new HighScore structure, call List<T>.RemoveAt(0) (assuming the list has at least 1 score in it) and then add the new HighScore structure to the list using List<T>.Add(). Then when the user closes the application, save each HighScore structure to the file using a for each loop and a StreamWriter.
Very beautifully explained. great work mate.
-
February 19th, 2009, 06:03 AM
#4
Re: Highscore
 Originally Posted by RaleTheBlade
You could create a structure called "HighScore" that contains two things, the name and the score, then create a generic List<T> of those structures, load the contents of the highscores file into that list at bootup and then whenever a new high score is made, create a new HighScore structure, call List<T>.RemoveAt(0) (assuming the list has at least 1 score in it) and then add the new HighScore structure to the list using List<T>.Add(). Then when the user closes the application, save each HighScore structure to the file using a for each loop and a StreamWriter.
I'd have just used a DataTable:
They can save themselves (myDT.WriteXml)
They can load themselves (myDT.ReadXml)
They can sort themselves via their built-in DataView (myDT.DefaultView.Sort)
and
Personally, I wouldnt create a struct that had a reference type: http://msdn.microsoft.com/en-us/libr...71(VS.71).aspx http://codeverity.com/timweaver/struct-vs-class/
Last edited by cjard; February 19th, 2009 at 06:06 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|