Results 1 to 12 of 12

Thread: HighScore, sorting and saving

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    HighScore, sorting and saving

    What is the best way of saving a highscore list and how do I sort that same list? I'll have 10 places in the list. I don't want to save it to a txt file as that is way too easy to edit from outside my game.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Have an array of structs that store name and highscore. Write this array to a file using fwrite and fread (C) or basic_ostream::write and basic_istream::read (C++). This will occlude it a little, though not much. If you want to occlude it more you'll have to write code to do that.

    You can sort the array using qsort (C) or std::sort (C++).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    How do I sort an array of structs? I can sort a simple int array using std::sort. But how do I sort an array of structs? My struct look like this:
    Code:
    struct HighScore
    {
    	char Name[50];
    	int Score;
    };
    
    HighScore HS[10];
    Last edited by McCain; Dec 29th, 2002 at 12:25 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can either write a custom predicate or overload the < operator for the struct.

    From a design POV the predicate is better, but overloading < is easier.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    What is a predicate?
    "overload the < operator"???
    The only overload I know about is overloading functions... Please explain.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    A predicate is a function or function object that is passed to an algorithm to perform actions such as comparisons. A function object is a class that overloads the () operator.
    Code:
    class highscore_compare_pred {
    public:
      bool operator()(const HighScore &hs1, const HighScore &hs2) {
        return hs1.Score < hs2.Score;
      }
    };
    Operator overloading means that you define a special function in your class (or outside in some cases) which the compiler will use when you use normal operators. Usually when you write
    CMyClass myObj;
    myObj += 3;
    the compiler will give you an error, because you can't simply add a number to myObj.
    That's where operator overloading comes in. You can define a public function in CMyClass like this:
    const CMyClass & operator += (int i);
    and write whatever you want in the implementation. This function gets called automatically when the compiler encounters an expression such as the above.

    The prototype of a < overload is
    bool YourType::operator <(const YourType& yt);

    Return true if 'this' is less than 'yt'.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    This has gone way over my head...
    I've never used classes
    I'll try to come up with something more simple...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Then you'd better start using them.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Why should I use classes? I've never needed them.
    Here is what I came up with for sorting and saving the highscore list. I'm sure the sorting is very inefficient but it gets the job done. If there is something I can change to make it faster please tell me. As for the saving; it doesn't really make it hard to edit or anything... Guess I'll have to come up with something like a very simple encryption or something...
    Code:
    void SortHS(HighScore HS[], int n)
    {
    	int iHighest = 0;
    	int iNum = 0;
    	int iCounted = 0;
    	HighScore temp;
    
    	for(int j = 0; j < n; j++)
    	{
    		for(int i = iCounted; i < n; i++)
    		{
    			if(HS[i].Score > iHighest)
    			{
    				iHighest = HS[i].Score;
    				iNum = i;
    			}
    		}
    		temp = HS[iCounted];
    		HS[iCounted] = HS[iNum];
    		HS[iNum] = temp;
    		iCounted++;
    		iHighest = 0;
    	}
    }
    
    void main()
    {
    	ifstream fin ("score.bin", ios::in | ios::binary);
    	char buffer[3];
    
    	for(int n = 0; n < 10; n++)
    	{
    		fin.read(HS[n].Name, sizeof(HS[n].Name));
    		fin.read(buffer, sizeof(buffer));
    		HS[n].Score = atoi(buffer);
    	}
    
    	SortHS(HS, 10);
    
    	for(n = 0; n < 10; n++)
    		cout << HS[n].Name << "    " << HS[n].Score << endl;
    
    	ofstream fout ("score.bin", ios::out | ios::trunc | ios::binary);
    
    	for(n = 0; n < 10; n++)
    	{
    		fout.write(HS[n].Name, sizeof(HS[n].Name));
    		itoa(HS[n].Score, buffer, 10);
    		fout.write(buffer, sizeof(buffer));
    	}
    
    	fout.close();
    
    	return;
    }
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  10. #10
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Hi,
    I just to give my opinion, and I think what can be the most effective for you is that each time a score is added to the list then you put it right at the good place. Like that you don't need to sort all the list but juste place the new entry to the list.

    You should need class just to put your code more structured cause later in your code you will have a lots of line and it can be more messy.

    Daok

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You never need classes to accomplish anything. A CPU doesn't know classes yet it can do everything you code. Assembly or C don't know classes either, yet they were used for years.

    But classes can make things simpler. If you don't intend to use classes, then why are you using C++ (you are, I can see it)?

    And in order to use std::sort you'll have to use classes (or function pointers, but those are less efficient). Of course, if you don't want that you can use qsort. But it still requires function pointers and is a lot harder to use than the nice simple predicates or operator overloading.

    I think your algorithm is even less efficient than bubble sort, or maybe it is bubble sort.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    I do intend to use classes, it's just that I want to learn the basics first. I come from VB and there I never used classes. When I think I know the language well enough I'll go find and read a tutorial on classes and see what they are good for.
    It's kind of like pointers, first I couldn't see the point in using pointers, but now I use them all the time, mostly because most funcions require them but still...

    No, it isn't bubble sort, and yes, it's probably very ineficient, but after googeling for a way to sort an array of struncts for hours I got tired of it and wrote my own, slow, function, just to get the job done.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

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