Skip to content

Commit f2d6cc5

Browse files
committed
highScore
1 parent 29378f0 commit f2d6cc5

6 files changed

Lines changed: 170 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@
3434
# misc
3535
source/chpt_1_ACppPrimer/vectorProd/*
3636
source/chpt_1_ACppPrimer/writeTypos/*
37+
source/chpt_2_ObjectOrientedDesign/quine/*

source/chpt_2_ObjectOrientedDesign/runtimeException/runtimeException.h renamed to source/chpt_2_ObjectOrientedDesign/runtimeException/RuntimeException.h

File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef GAMEENTRY_H
2+
#define GAMEENTRY_H
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
class GameEntry {
8+
public:
9+
GameEntry(const string& name = "", int score = 0) : name(name), score(score) {}
10+
string getName() const { return name; }
11+
int getScore() const { return score; }
12+
13+
protected:
14+
15+
private:
16+
string name;
17+
int score;
18+
};
19+
20+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef RUNTIMEEXCEPTION_H
2+
#define RUNTIMEEXCEPTION_H
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
class RuntimeException {
8+
public:
9+
RuntimeException(const string& error) {
10+
errorMessage = error;
11+
}
12+
string getMessage() const {
13+
return errorMessage;
14+
}
15+
16+
private:
17+
string errorMessage;
18+
};
19+
20+
class IndexOutOfBounds : RuntimeException {
21+
public:
22+
IndexOutOfBounds(
23+
const string& error = "Index out of bounds"
24+
) : RuntimeException(error) {}
25+
};
26+
27+
#endif
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#ifndef SCORES_H
2+
#define SCORES_H
3+
4+
#include "GameEntry.h"
5+
#include "RuntimeException.h"
6+
#include <iostream>
7+
using namespace std;
8+
9+
class Scores {
10+
public:
11+
Scores(int maxEntry = 10) {
12+
maxEntries = maxEntry;
13+
entries = new GameEntry[maxEntries];
14+
numEntries = 0;
15+
}
16+
17+
~Scores() {
18+
delete[] entries;
19+
}
20+
21+
void add(const GameEntry& entry) {
22+
int newScore = entry.getScore();
23+
24+
if (numEntries == maxEntries) {
25+
if (newScore <= entries[maxEntries - 1].getScore()) {
26+
return;
27+
}
28+
} else {
29+
numEntries++;
30+
}
31+
32+
int i = numEntries - 2;
33+
while (i >= 0 && newScore > entries[i].getScore()) {
34+
entries[i + 1] = entries[i];
35+
i--;
36+
}
37+
38+
entries[i + 1] = entry;
39+
}
40+
41+
GameEntry remove(int i) throw(IndexOutOfBounds) {
42+
if ((i < 0) || (i >= numEntries)) {
43+
throw IndexOutOfBounds("Invalid Index");
44+
}
45+
46+
GameEntry gameEntry = entries[i];
47+
48+
for (int j = (i + 1); j < numEntries; j++) {
49+
entries[j - 1] = entries[j];
50+
}
51+
52+
numEntries--;
53+
return gameEntry;
54+
}
55+
56+
void printScores() {
57+
if (numEntries <= 0) {
58+
return;
59+
}
60+
61+
for (int i = 0; i < maxEntries; i++) {
62+
cout << entries[i].getName() << '\t';
63+
}
64+
cout << endl;
65+
for (int i = 0; i < maxEntries; i++) {
66+
cout << entries[i].getScore() << '\t';
67+
}
68+
cout << endl;
69+
}
70+
71+
protected:
72+
private:
73+
int maxEntries;
74+
int numEntries;
75+
GameEntry* entries;
76+
};
77+
78+
#endif
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "Scores.h"
2+
#include <iostream>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
int main(int argc, char const *argv[]) {
8+
9+
int score;
10+
Scores scores;
11+
12+
vector<string> playerNames ({
13+
"Liam", "Olivia", "Noah",
14+
"Emma", "Oliver", "Nick",
15+
"Jim", "Amelia", "Sophia"
16+
});
17+
18+
vector<int> playerScores ({
19+
12, 4032, 871,
20+
10, 9999, 764,
21+
19, 1290, 101,
22+
});
23+
24+
while (!playerNames.empty() || !playerScores.empty()) {
25+
26+
GameEntry gameEntry(playerNames.back(), playerScores.back());
27+
scores.add(gameEntry);
28+
29+
scores.printScores();
30+
31+
playerNames.pop_back();
32+
playerScores.pop_back();
33+
}
34+
35+
int removeScore;
36+
cout << "\nRemove a score at index: ";
37+
cin >> removeScore;
38+
39+
GameEntry removedEntry = scores.remove(removeScore);
40+
cout << "Removed " << removedEntry.getName() << 's' << ' ' << "score\n\n";
41+
scores.printScores();
42+
43+
return 0;
44+
}

0 commit comments

Comments
 (0)