-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.cpp
More file actions
173 lines (165 loc) · 5.18 KB
/
Copy pathmenu.cpp
File metadata and controls
173 lines (165 loc) · 5.18 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 <iostream>
#include <fstream>
#include <string>
#include "menu.h"
#include "interactive.h"
#include "startgame.h"
using namespace std;
//make a struct
struct Rank{
string name;
int scorenum;
};
void upd_ranking( int newscore){
ifstream fin;
fin.open("ranking.txt");
Rank scorelist[11];
string score;
int count=0;
while (fin >> score){
int pos=0;
pos = score.find("&");
string scorenum;
scorenum = score.substr(pos+1);
int scoreNum = stoi(scorenum);
string name;
name = score.substr(0,pos);
if (newscore>scoreNum){
string newname;
cout<<"\nCongratulation, you enter the leaderboard!\n";
cout<<"Please enter Please enter your username (No space please):";
cin>>newname;
scorelist[count].scorenum=newscore;
scorelist[count].name=newname;
count+=1;
if (count==10){
break;
}
newscore=-1;
}
scorelist[count].scorenum=scoreNum;
scorelist[count].name=name;
count++;
if (count==10){
break;
}
}
if (count<10 && newscore!=-1){
string newname;
cout<<"\nCongratulation, you enter the leaderboard!\n";
cout<<"Please enter Please enter your username (No space please):";
cin>>newname;
scorelist[count].scorenum=newscore;
scorelist[count].name=newname;
count+=1;
newscore=-1;
}
ofstream fout;
fout.open("ranking.txt");
if (fout.fail()){
cout<<"Error in file opening!!"<<endl;
exit(1);
}
for (int i=0;i<(count<10?count:10);i++){
fout<<scorelist[i].name<<"&"<<scorelist[i].scorenum<<endl;
}
fout.close();
if (newscore==-1){
showranking();
return;
}
wait_to_enter();
}
void showranking(){
ifstream fin;
fin.open("ranking.txt"); // File output
if (fin.fail()){
cout<<"Error in file opening or there is no ranking!"<<endl;
exit(1);
}
int count=0;
string score;
while (fin >> score){
count++; //get the number of lines
}
fin.close();
if (count==0){
printf("\nThe list is empty.\n\n");
wait_to_enter();
return;
}
struct Rank * scorelist = new Rank[count]; //Dynamic memory management
count=0;
fin.open("ranking.txt");
while (fin >> score){ //Add information into the list
int pos=0;
pos = score.find("&");
string scorenum;
scorenum = score.substr(pos+1);
int scoreNum = stoi(scorenum);
scorelist[count].scorenum= scoreNum;
string name;
name = score.substr(0,pos);
scorelist[count].name=name;
count++;
}
fin.close();
//rearranging the list
int i, j, idx;
int max;
for ( i=0; i<count; i++){
max=scorelist[i].scorenum;
idx=i;
for (j=i+1;j<count; j++){
if (scorelist[j].scorenum >max){
max= scorelist[j].scorenum;
idx = j;
}
}
if (idx != i)
swap( scorelist[i], scorelist[idx]);
}
cout<<endl;
cout<<"\n Leaderboard:"<<endl;
for (int index=0; index<count && index<10; index++){
cout<<index+1<<" "<<scorelist[index].name<<": "<<scorelist[index].scorenum<<endl;
}
delete [] scorelist;
wait_to_enter();
}
void print_menu(){
clear_screen();
for (int i=0;i<=screen_N+4;i++){
for (int j=0;j<=screen_M;j++){
putchar(' ');
}
putchar('\n');
}
clear_screen();
cout<<" PLAY FLAPPY BIRD!!! "<<endl;
cout<<" Press 1 to start the game!"<<endl;
cout<<"Press 2 to show the leaderboard!"<<endl;
cout<<" Press 3 to quit"<<endl;
}
void wait_to_enter(){
for (int i=1;i<=15;i++){
wait_to_next();
}
cout<<"\n\nPress SPACE to go to the main menu or Q to quit the game\n";
char ch=keyboard_hit();
while (ch!=' ' && ch!='q' && ch!='Q'){
wait_to_next();
ch=keyboard_hit();
}
if (ch==' '){
return;
}
else{
game_end();
}
}
void game_end(){
cout<<endl;
cout<<"\nGame ended!"<<endl;
exit(0);
}