highscore structs

Started by
11 comments, last by Rob Loach 18 years, 11 months ago
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
Advertisement
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.
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
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
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;
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
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.
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
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.
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
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...:)
Gary.Goodbye, and thanks for all the fish.
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
Read in from a high score text file (already sorted)
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}
And remember for the sorting: the easiest sort algorithm is the one you don't have to write :) [google] std::sort.
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? :|

This topic is closed to new replies.

Advertisement