CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Highscore

  1. #1
    Join Date
    Dec 2008
    Posts
    2

    Question 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

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    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

  3. #3
    Join Date
    Apr 2006
    Posts
    220

    Re: Highscore

    Quote Originally Posted by RaleTheBlade View Post
    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.

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Highscore

    Quote Originally Posted by RaleTheBlade View Post
    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.
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

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
  •  





Click Here to Expand Forum to Full Width

Featured