-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassifica.cpp
More file actions
74 lines (61 loc) · 1.53 KB
/
Classifica.cpp
File metadata and controls
74 lines (61 loc) · 1.53 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
#include "Classifica.hpp"
Classifica::Classifica(WINDOW *win){
read_classifica();
this->win = win;
}
void Classifica::update_classifica(int score){
int i = 0;
while (i < 10 && scores[i] >= score) {
i++;
}
if(i < 9){
for (int j = 9; j > i; j--){
scores[j] = scores[j - 1];
}
scores[i] = score;
}
write_classifica();
}
void Classifica::display_classifica(){
read_classifica();
mvwprintw(win, 3, 3, "Scores:");
for (int i = 0; i < 10; i++) {
mvwprintw(win, 4 + i, 3, "%d. %d", i + 1, scores[i]);
}
wrefresh(win);
wgetch(win);
}
void Classifica:: write_classifica(){
ofstream file("scores.txt", ios::out); // out mode
for(int i = 0; i < 10; i++){
char score_str[100];
sprintf(score_str, "%d", scores[i]);
if (file.is_open()) {
file << scores[i] << endl;
}
}
file.close();
}
void Classifica:: read_classifica(){
ifstream obj("scores.txt");
if (obj.is_open()) {
char line[100];
for(int i = 0; i < 10; i++){
if(obj.getline(line, 100)){
scores[i] = stoi(line);
}
}
obj.close();
}
}
void Classifica:: initialize_classifica(){
ofstream file("scores.txt");
for(int i = 0; i < 10; i++){
file << "0" << endl;
}
file.close();
}
bool Classifica::fileExists(){
ifstream file("scores.txt");
return file.is_open();
}