Public Group

highscore structs

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi guys decided to add a high scores list to my tetris clone, im having a few initial problems. I have the name and score read in from a file to a temporary struct this struct is then pushed into a stack. This is repeated 10 times. Everything seems to compile but im not sure how to access the struct members in the stack. heres the code i have for the function:
struct HScores //structure for scores and names
{
char name[3];
int score;
};

stack<HScores> HighScoreStack; //stack to put 10 HScore structs into

void HighScores() //function to handle the highscores
{

ifstream input("scores.dat"); //file to read in from
for (int m = 0; m < 10; ++m)
{
HScores SBuffer;
input >> SBuffer.name;
input >> SBuffer.score;
HighScoreStack.push(SBuffer);
}

input.close();
}


Im unsure if this is even working as im not sure how to access the members. anyhelp appreciated lone

Share on other sites
Pop it into a buffer and access it from there. Seems a bit overkill, though, I'd just use a normal array. Access everything directly. No overhead for the pop/push functions. Meh.

Share on other sites
i want to be able to sort the scores by top score ect, could i do that with a normal array? also i havnt really worked with structs and stacks before could you give me a quick example of how to acces one of them ?

cheers
lone

Share on other sites
Yeah, you could do manual sorting on an array.

Anyways, to access the struct in the stack I'd do:

//pseudo code
struct foo{
int x;
int y;
}

stack<foo> foostack;

// Push stuff on somewhere in here.

// Now to get it off
foo bar;
bar = foostack.pop();
cout << bar.x << " " << bar.y;

Share on other sites
And for sorting on an array, here's the bubble sort. Hugely inefficient, but simple to program, and doesn't really matter how bad it is, if it's used on an array of 10.
  for(x = 0; x < ARRAY_SIZE; x++)    for(y = 0; y < ARRAY_SIZE-1; y++)      if(iarray[y] > iarray[y+1]) {        holder = iarray[y+1];        iarray[y+1] = iarray[y];        iarray[y] = holder;      }

You could look it up on wikipedia if you wanted to know how it works.

Share on other sites
Oh, and come to think of it, you wouldn't want to use a stack if you're going to sort. A stack's entire purpose is first in last out. Unless you're loading the scores in the opposite order of how you want to read them, it doesn't make much sense.

Share on other sites
When I did a high score table I used a list<> of structures. That way inserting new scores, when a new high score is achieved, in the middle of the list is easy.

Also, if the high score file is always in order then you'll read them a line at a time, construct your stucture and push_back() onto the list, so the high scores should always be in order...:)

Share on other sites
If you are only going to have 10 high scores then skip the stack
and just do an array. Why write a pop, push, insert, delete etc...
for something so simple?

Create an array of structs
At the end of a game check
if (currentScore > highScore[9]) // if current score is greater{                                // than the lowest high score    highScore[9] =  currentScore;  // replace lowest score with current score    sortArray();                   // sort the array to put new                                    // score into correct position    writeToHighScoreTextFile();    // writes new sorted high scores to                                    // the text file; or you could do this                                   // when the player quits the game}

Share on other sites
And remember for the sorting: the easiest sort algorithm is the one you don't have to write :) [google] std::sort.

Share on other sites
the main problem im having is i want to have the player names in the array also, would it be better to just have 2 arrays one with names and one with scores? im unsure how id get a name and a number into an array that are somehow related to each other but only sorting by the highscore. that was my main reason for the structs. any ideas? :|

1. 1
2. 2
JoeJ
20
3. 3
frob
19
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• Forum Statistics

• Total Topics
632204
• Total Posts
3004763

×