This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
highscore.cpp
86 lines (86 loc) · 2.31 KB
/
highscore.cpp
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
fstream hs_file;
typedef struct player {
string name;
int score;
}data;
data player[10];
bool openfile(bool resetscore = false){
if(resetscore) {
hs_file.open("data/highscore",ofstream::out | ofstream::trunc);
}else{
hs_file.open("data/highscore");
}
if(hs_file.is_open()) {
return true;
}else{
return false;
}
}
// Reading scores from file
bool returnscore(bool just_highscore = false){
if(openfile()) {
if(just_highscore) {
hs_file >> player[0].score >> player[0].name;
}else{
for(int i = 0; i<10; i++) {
hs_file >> player[i].score >> player[i].name;
}
}
}else{
cout<<"ERROR OPENING FILE TO READ DATA";
return false;
}
hs_file.close();
return true;
}
//initializing scores
void File_Init(){
returnscore(true);
int score_init = player[0].score;
if(score_init == -1 || score_init == NULL) {
openfile(true);
hs_file.close();
}
return;
}
//Check struct for sorting
bool sortchecker(data a,data b){
if(a.score >= b.score)
return 1;
else
return 0;
}
//Sort scores decending
void sortscores(){
sort(player,player+10,sortchecker);
}
//Save scores on file decending order
bool savescore(int score, string name){
returnscore();
player[9].score = score;
cout<<player[9].score;
player[9].name = name;
cout<< " "<< player[9].name;
sortscores();
if(openfile(true)) {
for(register int i = 0; i<10; i++) {
hs_file << player[i].score<<" "<<player[i].name<<endl;
}
}else{
cout<<"ERROR OPENIN FILE FOR SAVING DATA";
hs_file.close();
return false;
}
hs_file.close();
return true;
}
//To check player score while playing
bool checkscore(){
returnscore();
sortscores();
if(playerscore > player[9].score) {
return true;
}else{
return false;
}
}