Thread: Keeping high scores for pong

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    16

    Keeping high scores for pong

    Ok, well ive written a pong game and now i need to incorporate the scoring into it. The game starts out where you enter your initials. Then at the end of the game it shows your score. Now im stuck on how im going to do this next part. Basically I need to have a file with 10 names and scores saved. If your score is bigger than any of the 10, it needs to input it into the file and sort that file from higest score to lowest. Now my question is would the easiest way to do this is to have two arrays? One for initials and one for scores? And then just write a swap function? Any ideas are greatly appreciated!

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    This would be better off on the games programming board
    Double Helix STL

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    There are tons of ways you can save your high-score list. You can write binary files and read/sort objects or you can write text files, read the content into arrays/objects, sort them and overwrite the old file.

    One possible way is to have two arrays, sort the numerical array and apply the same changes to the array for the initials. You can also use a multi-dimensional array and sort that one or you can make an array of objects and sort the objects.

    Imagination is your only constraint.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Use a std::map? Seems rather overkill, but hassle free! Then all you would have to do is rewrite the file completely if there's a new high score (i.e. you beat the lowest score - the one at topscores.begin(), I presume).
    Last edited by whiteflags; 04-16-2007 at 02:20 AM. Reason: reworded for clarity

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Thanks all =) I think my best bet would be to do what KONI said. Read from a file into an array, sort it and then sort the initial array the same way than overwrite the previous one! Now to go try to write it all out!

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by trev456 View Post
    Thanks all =) I think my best bet would be to do what KONI said. Read from a file into an array, sort it and then sort the initial array the same way than overwrite the previous one! Now to go try to write it all out!
    Btw, you don't have to sort that array at all. Since your highscore list inside the file is already sorted, inserting a new score comes down to finding the position for that element. This is turn is the same as inserting a new element in an already ordered set, exactly like in insertion sort.

    Suppose you have a highscore of 10, ordered from highest to lowest:

    - initialize an array of 11 elements
    - put the 10 highscores in the positions [0,9]
    - put the new highscore in the position 10
    - let i be the position of the new highscore
    - compare i-1 with i, if i is greater than i-1, swap i-1 and i (move the highscore 1 element closer to 0)
    - repeat the last step as long as i > 0 or you couldn't swap the element.
    - finally, write the 10 first elements in the file

    in pseudo-code:
    Code:
     insert(array a, int length, value) {
         int i = length - 1;
         while (i >= 0 && a[i] > value) {
             a[i + 1] = a[i];
             i = i - 1;
         }
         a[i + 1] := value;
     }

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Thanks a ton KONI!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. High Scores - A Dilemma
    By CrazyNorman in forum Game Programming
    Replies: 5
    Last Post: 12-28-2006, 09:01 PM
  2. Store high scores in exe file
    By Nutshell in forum Game Programming
    Replies: 12
    Last Post: 04-27-2003, 04:09 PM
  3. Help on high scores
    By Gnoober in forum C++ Programming
    Replies: 0
    Last Post: 02-17-2003, 07:28 PM
  4. Working on High Scores...
    By Gnoober in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2003, 12:50 PM
  5. High Scores
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2003, 01:08 PM