-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathscores.cpp
More file actions
173 lines (140 loc) · 5.52 KB
/
scores.cpp
File metadata and controls
173 lines (140 loc) · 5.52 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "scores.hpp"
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();
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;
std::string 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 " << 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)
<< secondsFormat(duration) << " │ ";
endl();
}
if (!size) {
std::cout << " No saved scores.";
endl();
} else {
std::cout << " "
"└─────┴────────────────────┴──────────┴──────┴───────┴───────"
"───────┴──────────────┘";
}
endl(3);
}
void Scoreboard::printStats() {
Stats stats;
if (stats.collectStatistics()) {
std::cout << green << bold_on << " STATISTICS" << bold_off << def;
endl();
std::cout << green << bold_on << " ──────────" << bold_off << def;
endl();
std::cout << " ┌────────────────────┬─────────────┐";
endl();
std::cout << " │ " << bold_on << "Best Score " << bold_off << " │ "
<< std::setw(11) << stats.bestScore << " │";
endl();
std::cout << " │ " << bold_on << "Game Count " << bold_off << " │ "
<< std::setw(11) << stats.gameCount << " │";
endl();
std::cout << " │ " << bold_on << "Number of Wins " << bold_off << " │ "
<< std::setw(11) << stats.winCount << " │";
endl();
std::cout << " │ " << bold_on << "Total Moves Played" << bold_off << " │ "
<< std::setw(11) << stats.totalMoveCount << " │";
endl();
std::cout << " │ " << bold_on << "Total Duration " << bold_off << " │ "
<< std::setw(11) << secondsFormat(stats.totalDuration) << " │";
endl();
std::cout << " └────────────────────┴─────────────┘";
endl();
} else {
std::cout << " No saved statistics.";
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");
if (scores.fail()) {
return;
}
std::string playerName;
ull playerScore;
bool win;
std::string 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();
}