Repository navigation
Expand file tree
/
Copy pathscores.cpp
More file actions
147 lines (106 loc) 路 4.7 KB
/
Copy pathscores.cpp
File metadata and controls
147 lines (106 loc) 路 4.7 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "scores.h"
bool compare(const Score &a, const Score &b) {
return a.score < b.score;
};
void Scoreboard::prompt() {
std::cout << bold_on << " Please enter your name to save this score: " << bold_off;
std::cin >> name;
}
void Scoreboard::writeToFile() {
std::fstream scores("./data/scores.txt", std::ios_base::app);
scores << std::endl << name << " " << score << " " << win << " " << moveCount << " " << largestTile << " " << duration; endl();
scores.close();
}
void Scoreboard::printScore() {
readFile();
clearScreen();
drawAscii();
std::cout << green << bold_on << " SCOREBOARD" << bold_off << def; endl();
std::cout << green << bold_on << " ----------" << bold_off << def; endl(2);
int size = scoreList.size();
for (int i = size - 1; i >= 0; i--) {
std::string playerName = scoreList[i].name;
ull playerScore = scoreList[i].score;
std::string won = scoreList[i].win ? "Yes" : "No";
long long moveCount = scoreList[i].moveCount;
ull largestTile = scoreList[i].largestTile;
double duration = scoreList[i].duration;
if (i == size - 1) {
std::cout << " +-----+--------------------+----------+------+-------+--------------+--------------+"; endl();
std::cout << " | " << bold_on << "No." << bold_off << " | "
<< bold_on << "Name" << bold_off << " | "
<< bold_on << "Score" << bold_off << " | "
<< bold_on << "Won?" << bold_off << " | "
<< bold_on << "Moves" << bold_off << " | "
<< bold_on << "Largest Tile" << bold_off << " | "
<< bold_on << "Duration (s)" << bold_off << " |";
endl();
std::cout << " +-----+--------------------+----------+------+-------+--------------+--------------+"; endl();
}
std::cout << " | " << std::setw(2) << size - i << ". | "
<< playerName; padding(playerName);
std::cout << " | " << std::setw(8) << playerScore << " | "
<< std::setw(4) << won << " | "
<< std::setw(5) << moveCount << " | "
<< std::setw(12) << largestTile << " | "
<< std::setw(12) << duration << " | ";
endl();
}
if (!size) {
std::cout << " No saved scores.";
endl();
}
else {
std::cout << " +-----+--------------------+----------+------+-------+--------------+--------------+";
}
endl(3);
}
void Scoreboard::printStats() {
Stats stats;
stats.collectStatistics();
std::cout << green << bold_on << " STATISTICS" << bold_off << def; endl();
std::cout << green << bold_on << " ----------" << bold_off << def; endl(2);
std::cout << " +--------------------+------------+"; endl();
std::cout << " | " << bold_on << "Best Score " << bold_off << " | " << std::setw(10) << stats.bestScore << " |"; endl();
std::cout << " | " << bold_on << "Game Count " << bold_off << " | " << std::setw(10) << stats.gameCount << " |"; endl();
std::cout << " | " << bold_on << "Number of Wins " << bold_off << " | " << std::setw(10) << stats.winCount << " |"; endl();
std::cout << " | " << bold_on << "Total Moves Played" << bold_off << " | " << std::setw(10) << stats.totalMoveCount << " |"; endl();
std::cout << " | " << bold_on << "Total Duration (s)" << bold_off << " | " << std::setw(10) << stats.totalDuration << " |"; endl();
std::cout << " +--------------------+------------+"; endl();
endl(3);
std::cout << " Press any key to exit: ";
char c;
std::cin >> c;
exit(EXIT_SUCCESS);
}
void Scoreboard::padding(std::string name) {
int length = name.length();
while (18 - length++) {
std::cout << " ";
}
}
void Scoreboard::readFile() {
std::ifstream scores("./data/scores.txt");
std::string playerName;
ull playerScore;
bool win;
ull largestTile;
long long moveCount;
double duration;
while (scores >> playerName >> playerScore >> win >> moveCount >> largestTile >> duration) {
Score bufferScore;
bufferScore.name = playerName;
bufferScore.score = playerScore;
bufferScore.win = win;
bufferScore.largestTile = largestTile;
bufferScore.moveCount = moveCount;
bufferScore.duration = duration;
scoreList.push_back(bufferScore);
};
std::sort(scoreList.begin(), scoreList.end(), compare);
}
void Scoreboard::save() {
prompt();
writeToFile();
std::cout << green << bold_on << " Score saved!" << bold_off << def; endl();
}