Persistent High Score Board

closed account (2Tf21hU5)
Hello!

Currently, we've been told to create a project where we're supposed to create a game. I'm having trouble with knowing how to work my way with introducing the scoreboard mainly because our professors haven't taught us how to do so (we've only reached arrays as of now, and we're going to pass this project in a week, or maybe after two days!)

I've already asked help from other forums before I resorted to coming here. Basically the guys on the other forum have taught me much to understand things that I bet our professors would never teach us in this quarter; vectors, classes, structs, and a whole lot more, all I been forcing down to my own throat.

Someone constructed for me (with a whole lot of thanks because he explained every part of it so nicely) a class that typically functions as a scoreboard.

The main problem with the scoreboard is that it isn't persistent; and this is help that I need because I don't have a clue in manipulating ifstream and ofstream. I know it requires making an external .txt file. I know how to save the vectors to the file and load them up and display the .txt file, but what I don't know how to do is to make the items in the .txt file be stored to the vector.

I'm assuming fread, fseekg and fget has some contributions to making it persistent, but like I said, I have limited knowledge on how to work with ifstream and ofstream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
struct Entry{
    std::string name;
    int score;
};

class Scoreboard{
 

public:
    void AddPlayer(std::string playerName, int points){
        Entry e;
		e.name = playerName;
		e.score = points;
        m_scoreboard.push_back(e);
    }
 
    void Print(){
        for(int i=0; i < m_scoreboard.size(); ++i){
            std::cout << (i+1) << ":\t" << m_scoreboard[i].name << "\t" << m_scoreboard[i].score << "\n";
        }
    }
 
    void RemovePlayer(std::string nameWeWantToRemove){
        for(std::vector<Entry>::iterator i = m_scoreboard.begin(); i != m_scoreboard.end(); ++i){
            if( i->name == nameWeWantToRemove ){
                m_scoreboard.erase(i);
                break;
            }
        }
    }

	void Sort() {
		for(int x=0; x<m_scoreboard.size(); x++)
		{
			for(int y=0; y<m_scoreboard.size()-1; y++)
			{
                if(m_scoreboard[y].score < m_scoreboard[y+1].score)
                {
                        Entry temporary = m_scoreboard[y+1];
                        m_scoreboard[y+1] = m_scoreboard[y];
                        m_scoreboard[y] = temporary;
                }
			}
		}
	}
 
private:
    std::vector<Entry> m_scoreboard;
};
If your teacher has only taught you arrays, then only use arrays!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
using namespace std; // because your teacher probably taught you to do this

void displayScores(string names[], int score[], const int amountOfPlayers);
void sort(string names[], int score[], const int amountOfPlayers);

int main(void)
{
  // create variables
  const int PLAYERS = 5;
  string names[PLAYERS];
  int scores[PLAYERS];
  

  // initialize arrays
  names[0] = "John";
  names[1] = "Mary";
  names[2] = "Frank";
  names[3] = "Larry";
  names[4] = "Samantha";
  
  scores[0] = 3;
  scores[1] = 2;
  scores[2] = 5;
  scores[3] = 1;
  scores[4] = 4;

  
  // Display
  cout << "Before Sorting:\n";
  displayScores(names, scores, PLAYERS);
  sort(names, scores, PLAYERS);
  cout << "\nAfter Sorting:\n";
  displayScores(names, scores, PLAYERS);

  return 0;
}

void displayScores(string names[], int scores[], const int amountOfPlayers)
{
  for (int i = 0; i < amountOfPlayers; i++)
  {
    cout << names[i] << ": " << scores[i] << endl;
  }

}

void sort(string names[], int scores[], const int amountOfPlayers)
{
  for (int i = 0; i < amountOfPlayers - 1; i++)
  {
    for (int j = i + 1; j < amountOfPlayers; j++)
    {
      if (scores[i] < scores[j])
      {
        string storeName = names[i];
        int storeScore = scores[i];
        names[i] = names[j];
        scores[i] = scores[j];
        names[j] = storeName;
        scores[j] = storeScore;
      }
    }
  }
}


I'll leave it to you to solve your homework.
Last edited on
closed account (2Tf21hU5)
Here's the catch.

We're supposed to make a game, and our professor is requiring us to do things she hasn't taught us at all. Examples are a timer, where she hasn't even explained the ctime library, graphics, where she hasn't even told us which approach or what the approaches actually are (OpenGL, SDML, et cetera), and lastly, a scoreboard.

If someone is telling you to do things they haven't actually taught you, its expected you do research and ask other professionals for help. At least, this is usually the case in our institute. I've done research (and still doing), and now I'm asking for help.

If you thought that this was a simple homework of making a scoreboard with initial names and scores, then sorry for not thoroughly explaining the situation.
What is your assignment?
Last edited on
closed account (2Tf21hU5)
Our project is to create a game (we choose which - mine is some kind of bowman game). I'm finished with coding the game, and the only thing I really need now is a scoreboard.

The thing is, our scoreboard will have to take its inputs from the performance of a player. If he finishes the level, then his score will be added to the list, all sorted out.

In a sense, I want the player's score to be compared with scores that are in the .txt file, and save his score there if its high enough to be there and be sorted out. The thing I can't actually figure out is how I would do so, as in, compare the new score with the old scores in the .txt file.

I assumed that the process goes like this:

• add new score to vector, m_scoreboard
• open file for loading
• get nth name and nth score inside .txt file
• save them inside the vector, m_scoreboard
• close file

• sort the scoreboard, and remove entries greater than 10

• open file for saving
• write contents of vector to the .txt file
• close file

What I really fail is how to get the vector to store the contents of the txt file correctly. e.g. How can I "get" the name and score of an entry, and then store them to the vector, or simple just set them to a variable.
Topic archived. No new replies allowed.