ok i was wondering if somebody can help???im sure its simple but after a few hours(2 days) i cant get it...
ok imagine i have:
struct hiscoretable
{
stringw name;
int score;
}
hiscoretable hstable[6]; //for a 5 score table + 1 new
ok i load the first 5 hiscoretable.score and corrensponding .names and i add a newscore to hstable[5].score and player name to hstable[5].name then i copy to a tmparray[6] and i sort it this last one.after i copy the scores back to the struct score member i want the names to match the sorted scores...
before:
marco - 45000
manuel - 35000
ana - 20000
bruno - 10000
joaquim - 5000
after:
marco - 45000
manuel - 35000
playername - 25000
ana - 20000
bruno - 10000
can somebody help??
im such a noob in c++
HiScore Table question
-
- Posts: 17
- Joined: Tue Sep 26, 2006 8:01 am
HiScore Table question
Sorry about my bad english.thanks.
How does your sort routine look like (some code snippet would help) ???
In fact you don't need a second (temp) array, also the number of elements don't need to be count + 1...
Let's say element 0 is the lowest score and you have a function for adding a new score:Well, I wrote this now on the scratch, so it probalby could be done better, but it should work...
In fact you don't need a second (temp) array, also the number of elements don't need to be count + 1...
Let's say element 0 is the lowest score and you have a function for adding a new score:
Code: Select all
void addScore(hiscoretable newScore){
for(int t = 0; t < 5; t++){
if(hstable[t].score >= newScore.score){
if(t == 0) return; // no new entry
if(t == 1){
hstable[0] = newScore; // last entry
return;
}
for(int r = 0; r < t - 2; r++){
hstable[r] = hstable[r + 1];
}
hstable[t - 1] = newScore; // new entry
return;
}
}
}
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
My code works with std and with time instead of scores (so lower is better), but i guess you will be able to change that (hint: use mScore instead of mTime and change a < to > in CheckPosition).
Code: Select all
// size of the highscoretable
#define MAX_HIGHSCORES 5
#define DEFAULT_TIME 3599999
struct HScoreEntry
{
HScoreEntry();
// Check if the score is initialized by a player or if it's the default value
bool IsUserScore() const;
std::wstring mProfileName;
unsigned int mTime;
};
class Highscores
{
public:
// return index where time would be positioned, -1 for no highscore
int CheckPosition(unsigned int time_) const;
// add the score and return the position where it was added ( or -1 for no highscore)
int AddScoreEntry(const HScoreEntry & score_);
// access score
const HScoreEntry& GetScore(int index_) const;
private:
HScoreEntry mScores[MAX_HIGHSCORES];
};
HScoreEntry::HScoreEntry()
{
mTime = DEFAULT_TIME;
mProfileName = L"----";
}
bool HScoreEntry::IsUserScore() const
{
return (s32)mTime != DEFAULT_TIME;
}
// return index where it score will be positioned, -1 for no highscore
int Highscores::CheckPosition(unsigned int time_) const
{
for ( int i=0; i < MAX_HIGHSCORES; ++i )
{
if ( time_ < GetScore(i).mTime )
return i;
}
return -1;
}
int Highscores::AddScoreEntry(const HScoreEntry & score_)
{
int pos = CheckPosition(score_.mTime);
if ( pos < 0 )
return pos;
for ( int i = MAX_HIGHSCORES-1; i > pos; --i )
{
mScores[i] = mScores[i-1];
}
mScores[pos] = score_;
return pos;
}
const HScoreEntry& Highscores::GetScore(int index_) const
{
if ( index_ < 0 || index_ >= MAX_HIGHSCORES )
{
assert(!"Highscores::GetScore invalid index");
return mScores[0];
}
return mScores[index_];
}
yet another way...
Code: Select all
struct hiscore
{
hiscore() : name(0), score(0) {}
hiscore(wchar_t* name, int score) : name(name), score(score) {}
bool operator <(const struct hiscore& other) const
{
return score < other.score;
}
stringw name;
int score;
};
// making score list
core::array<hiscore> scores;
scores.set_used(10);
// adding a score
hiscore score(PlayerName, PlayerScore);
scores.push_back(score);
score.sort(); // sort the score list
scores.set_used(10); // get rid of the last score
Since we're pointing out every way to sort a collection...
Code: Select all
struct ScoreEntry
{
stringw name;
int score;
};
ScoreEntry hstable[6];
Code: Select all
// if you like the standard C library
int compare_high_score(const void* lhs, const void* rhs)
{
ScoreEntry* slhs = (ScoreEntry*)lhs;
ScoreEntry* srhs = (ScoreEntry*)rhs;
return srhs->score < slhs->score;
}
#include <stdlib.h>
qsort(hstable, sizeof(hstable) / sizeof(*hstable), sizeof(*hstable), compare_high_score);
Code: Select all
// if you like the standard C++ library. you might also opt to use
// some other container for storing the scores, possibly a vector.
struct compare_high_score
{
bool operator()(const HScoreEntry& lhs, const HScoreEntry& rhs)
{ return rhs.score < lhs.score; }
};
#include <algorithm>
std::sort(hstable, hstable + sizeof(hstable) / sizeof(*hstable), compare_high_score());
-
- Posts: 17
- Joined: Tue Sep 26, 2006 8:01 am
thx to all
ok im anxious to try every way.
info and more info to fill my brain...
i love it
thx again.
info and more info to fill my brain...
i love it
thx again.
Sorry about my bad english.thanks.
-
- Posts: 17
- Joined: Tue Sep 26, 2006 8:01 am
2nd question...plz help
well I was pretty close allready...it was so simple but thats what u gain for NOT having suficient knowledge...eheh...ok i followed the bitplane way slightly modified...since i want descending order...
check what ive done, i will start with the function to retrive names from a file:
void CGameHiScoreManager::getfromfile()
{
ifstream scoretxt ("score.did");
ifstream namestxt ("names.did");
int s;
core::stringw n;
char nn[7];
for (int i=0; i < 5; i++)
{
scoretxt >> s;
namestxt >> nn;
n = nn;
hiscoretable tscore(n , s);
hstable.push_back(tscore);
}
}
then the function to add the actual player/score
void CGameHiScoreManager::addS(CGameManager* pManager)
{
int s1;
irr::core::stringw n1;
s1 = pManager->getGameScore()->getFinalScore();
n1 = pManager->getPlayer()->getName();
hiscoretable score(n1, s1);
hstable.push_back(score);
hstable.sort(); // sort the score list
//hstable.set_used(5); // "get rid of the last score" if i do this i will get rid of the HIGHEST SCORE...
}
Now comes my second question...since i want to save this new list how should i do it???
I tried this with no sucess...
void CGameHiScoreManager::saveslist()
{
printf("a gravar no file");
ofstream scoretxt ("score.did");
ofstream namestxt ("names.did");
int s;
irr::core::stringw n;
char nn[7];
for (int i=0; i < 5; i++)
{
n = hstable.name;
nn = n <<<< IT GIVES ERROR HERE
s = hstable.score;
scoretxt << s;
namestxt << nn;
}
}
can u help me again plz??? thx in advance
check what ive done, i will start with the function to retrive names from a file:
void CGameHiScoreManager::getfromfile()
{
ifstream scoretxt ("score.did");
ifstream namestxt ("names.did");
int s;
core::stringw n;
char nn[7];
for (int i=0; i < 5; i++)
{
scoretxt >> s;
namestxt >> nn;
n = nn;
hiscoretable tscore(n , s);
hstable.push_back(tscore);
}
}
then the function to add the actual player/score
void CGameHiScoreManager::addS(CGameManager* pManager)
{
int s1;
irr::core::stringw n1;
s1 = pManager->getGameScore()->getFinalScore();
n1 = pManager->getPlayer()->getName();
hiscoretable score(n1, s1);
hstable.push_back(score);
hstable.sort(); // sort the score list
//hstable.set_used(5); // "get rid of the last score" if i do this i will get rid of the HIGHEST SCORE...
}
Now comes my second question...since i want to save this new list how should i do it???
I tried this with no sucess...
void CGameHiScoreManager::saveslist()
{
printf("a gravar no file");
ofstream scoretxt ("score.did");
ofstream namestxt ("names.did");
int s;
irr::core::stringw n;
char nn[7];
for (int i=0; i < 5; i++)
{
n = hstable.name;
nn = n <<<< IT GIVES ERROR HERE
s = hstable.score;
scoretxt << s;
namestxt << nn;
}
}
can u help me again plz??? thx in advance
Sorry about my bad english.thanks.