Sorted highscore list
Beginners question.
How do I create a highscorelist the easiest way ?
The list has to be sorted by score.
Do I have to use a STL vector to sort the list ?
The list will contain about 10 different names.
I use a "Player Class" that contains the player name and score.
Thanx
/Urax
quote: Original post by Urax
Beginners question.
How do I create a highscorelist the easiest way ?
The list has to be sorted by score.
Do I have to use a STL vector to sort the list ?
The list will contain about 10 different names.
I use a "Player Class" that contains the player name and score.
Thanx
/Urax
Player *highscore[10];for(int i=0; i<10; i++)} highscore<i> = NULL;}void add(Player *new){ for(int j=9; j>=0; j--){ if(highscore[j]){ if(highscore[j]->score < new->score){ highscore[j+1] = highscore[j]; highscore[j] = new; } else { break; } } }}
then to after every game when you have the Player object (say player1) just:
add(&player1);
- ro
Take a look at the qsort() function. If you implement the "hall of fame" as an array of it the fastest thing you can do...
Hope it helps.
Bye,
Karmalaa
---
"Lifting shadows off a dream once broken
She can turn a drop of water into an ocean"
typedef struct tag_HOFITEM { // Internal structure goes here... :-)} HOFITEM, *PHOFITEM;HOFITEM HallOfFame[10];int CompareHOFItems(const void *pItem1, const void *pItem2){ // Should return an integer less than, equal or // higher than zero whether the first argument is // to be considered lower than, equal or // higher than the second one.}void SortHallOfFame(){ qsort(HallOfFame, 10, sizeof(HOFITEM), CompareHOFItems);}
Hope it helps.
Bye,
Karmalaa
---
"Lifting shadows off a dream once broken
She can turn a drop of water into an ocean"
---[home page] [[email=karmalaa@inwind.it]e-mail[/email]]
You won''t need a qsort because the array is already sorted, that''s what a ''hall of fame'' is all about. Just shift everything down to make space, and insert your new value. If it''s really big, a linked list might be better, but then you don''t care about performance for this sort of thing.
If your using C++, you should use std::sort. It''s faster and safer than qsort.
I know qsort, but std::sort ?
I tried finding some information about it in my MSVC++ 5, but
I found nothing.
How can I use std::sort, and which header file I need to include (or library I need to link) in order to use it?
I tried finding some information about it in my MSVC++ 5, but
I found nothing.
How can I use std::sort, and which header file I need to include (or library I need to link) in order to use it?
std::sort sort is the header algorithm (no .h), you give it a start and an end to sort (end in this case is one-past-last). For more info you can have a look here. Here's an example:
Also there's some code here comparing std::sort to qsort that you might want to have a look at.
Edited by - Wilka on November 26, 2000 6:35:49 PM
bool compare(const int& lhs, const int& rhs){ return lhs > rhs;}int main()bool compare(const int& lhs, const int& rhs){ return lhs > rhs;}int main(){ int array[10] = {42, 4, 534, 92, 523, 1032, 43, 67, 9, -6}; // std::sort using < operator (default) std::sort(array, array + 10); for(int n=0; n<10; ++n) { cout << array[n] << endl; } cout << "-------------" << endl; // std::sort using comppairsion function std::sort(array, array + 10, compare); for(int j=0; i<10; ++i) { cout << array[j] << endl; } return 0;}
Also there's some code here comparing std::sort to qsort that you might want to have a look at.
Edited by - Wilka on November 26, 2000 6:35:49 PM
You could also use a Multimap. Set the score as the key and the player who got the score as the element. Just set up the sorting criterion to greater than. Then it will auto insert scores in the proper spot and all you have to do is make sure it doesn''t exceed 10 elements.
Just implement it however you want and by whatever means you are familiar with. Since you mentioned using STL structures, if you learn to use the STL, I recommend picking up Nicolai M. Josuttis'' book, The C++ Standard Library: A tutorial and Reference. It is extremely well written and easy to understand (imo). A definite must have if your interested in learning and utilizing them many features of the standard library in your coding.
Definetly worth the $35 it costs from booksamillion.
Even definetly worth the $50 at amazon.
std::multimap<int, string, std::greater<int> > highscores;for (int i = 0; i < 10; ++i){highscores.insert(std::make_pair(0, "John Doe");}std::multimap<int, string, std::greater<int> > ::iterator pos;for (pos = highscores.begin(), pos != highscores.end(), ++pos){std::cout << pos->first << '' '' << pos->second << std::endl;}
etc...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement