-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
308 lines (297 loc) · 6.74 KB
/
game.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <iostream>
#include <ncurses.h>
#include <string>
#include <fstream>
using namespace std;
string roundList[] = {"Technology", "Sports", "Science", "Politics", "Literature"};
string questionsFileList[] = {"questions/technology.txt", "questions/sports.txt", "questions/science.txt", "questions/politics.txt", "questions/literature.txt"};
string getstring()
{
string input;
// let the terminal do the line editing
nocbreak();
echo();
// this reads from buffer after <ENTER>, not "raw"
// so any backspacing etc. has already been taken care of
int ch = getch();
while ( ch != '\n' )
{
input.push_back( ch );
ch = getch();
}
// restore your cbreak / echo settings here
return input;
}
class display{
public:
int splashScreen()
{
int i, row, col, response;
initscr();
getmaxyx(stdscr, row, col);
ifstream file;
file.open("resources/ascii-art/splashscreen.txt");
string s;
for(int i=0; getline(file, s); i++)
mvprintw(row/4+i, (col-s.size())/2, "%s", s.c_str());
file.close();
mvprintw(row/2, (col-11)/2, "1. NEW GAME\n");
mvprintw(row/2+2, (col-11)/2, "2. HELP\n");
mvprintw(row/2+4, (col-11)/2, "3. EXIT\n");
mvprintw(row-row/6, (col-53)/2, "Enter 1, 2, 3 as per your preference and press enter.");
refresh();
cin>>response;
clear();
refresh();
return response;
}
void help()
{
int i, row, col, response;
initscr();
getmaxyx(stdscr, row, col);
ifstream file;
file.open("resources/ascii-art/help.txt");
string s;
for(int i=0; getline(file, s); i++)
mvprintw(row/4+i, (col-s.size())/2, "%s", s.c_str());
file.close();
getch();
clear();
refresh();
}
int exit()
{
int i, row, col, response;
initscr();
getmaxyx(stdscr, row, col);
ifstream file;
file.open("resources/ascii-art/exit.txt");
string s;
for(int i=0; getline(file, s); i++)
mvprintw(row/3+i, (col-s.size())/2, "%s", s.c_str());
file.close();
getch();
clear();
refresh();
return 0;
}
};
class playerInfo{
protected:
string playerName[6];
public:
void getPlayerName(int playerCount)
{
int i, row, col;
initscr();
getmaxyx(stdscr, row, col);
for(int i=0; i<playerCount; i++)
{
mvprintw(row/3+(2*i), (col-25)/2-5, "Enter name of player %d : ", i+1);
playerName[i] = getstring();
refresh();
}
clear();
refresh();
}
string showPlayer(int playerNumber)
{
return playerName[playerNumber-1];
}
};
class round{
int roundNumber;
public:
void updateRound()
{
roundNumber++;
}
int getRoundNumber()
{
return roundNumber;
}
void displayRound(int j)
{
int i, row, col, response;
initscr();
getmaxyx(stdscr, row, col);
mvprintw(row/3, (col-9)/2, "Round : %d", j+1);
mvprintw(row/3+4, (col-roundList[j].size())/2, "%s", roundList[j].c_str());
getch();
clear();
refresh();
}
};
class score{
protected:
int points[6];
public:
score()
{
for(int i=0; i<6; i++)
points[i]=0;
}
void scoreUpdateDirect(int playerID, bool responseStatus)
{
if(responseStatus==true)
points[playerID]+=10;
else if(responseStatus==false)
points[playerID]-=5;
}
void scoreUpdatePass(int playerID, bool responseStatus)
{
if(responseStatus==true)
points[playerID]+=5;
else if(responseStatus==false)
points[playerID]-=2;
}
int getScore(int playerID)
{
return points[playerID];
}
};
class newGame : public playerInfo, public round, public score{
int players;
public:
void playerCount()
{
int i, row, col, response;
initscr();
getmaxyx(stdscr, row, col);
mvprintw(row/3, (col-25)/2-5, "Enter number of players : ");
mvscanw(row/3, col/2+9, "%d", &players);
clear();
refresh();
}
int getPlayerCount()
{
return players;
}
void showAllScore()
{
int i, row, col, response;
initscr();
getmaxyx(stdscr, row, col);
mvprintw(row/3-3, (col-13)/2, "Current Score");
for(int i=0; i<players; i++)
{
mvprintw(row/3+(2*i), (col-25)/2-5, "%s : %d", playerName[i].c_str(), points[i]);
refresh();
}
getch();
clear();
refresh();
}
void finalScreen()
{
int i, row, col, response;
initscr();
getmaxyx(stdscr, row, col);
mvprintw(row/3-3, (col-15)/2, "Final Standings");
for(int i=0; i<players; i++)
{
mvprintw(row/3+(2*i), (col-25)/2-5, "%s : %d", playerName[i].c_str(), points[i]);
refresh();
}
int large=points[0], winner=0;
for(i=1; i<players; i++)
{
if(points[i]>large)
{
large=points[i];
winner=i;
}
}
mvprintw(row/3+13, (col-6)/2, "Winner");
mvprintw(row/3+15, (col-playerName[winner].size())/2, "%s", playerName[winner].c_str());
getch();
clear();
refresh();
}
};
int main()
{
display disp;
int response=1;
while(response)
{
response=disp.splashScreen();
if(response==1)
{
newGame game;
game.playerCount();
//int num=game.getPlayerCount();
game.getPlayerName(game.getPlayerCount());
for(int i=0; i<5; i++)
{
game.updateRound();
ifstream fin;
fin.open(questionsFileList[i]);
game.displayRound(i);
int row, col;
initscr();
getmaxyx(stdscr, row, col);
for(int j=0; j<game.getPlayerCount(); j++)
{
string ques, a, b, c, d, correctOption, givenOption;
getline(fin, ques);
getline(fin, a);
getline(fin, b);
getline(fin, c);
getline(fin, d);
getline(fin, correctOption);
//cout<<ques<<endl<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<correctOption<<endl;
for(int k=j, m=0; m<game.getPlayerCount()-1; k++, m++)
{
clear();
k=k%game.getPlayerCount();
mvprintw(row/5, (col-9)/2, "Player : %d", k+1);
mvprintw(row/5+2, (col-game.showPlayer(k+1).size())/2, "%s", game.showPlayer(k+1).c_str());
mvprintw(row/3, (col-ques.size())/2, "%s", ques.c_str());
mvprintw(row/3+4, col/3, "%s", a.c_str());
mvprintw(row/3+4, 2*(col/3), "%s", b.c_str());
mvprintw(row/3+8, col/3, "%s", c.c_str());
mvprintw(row/3+8, 2*(col/3), "%s", d.c_str());
mvprintw(row/3+14, col/2-1, " ");
givenOption = getstring();
//cout<<givenOption<<endl;
refresh();
if(givenOption[0]=='P')
continue;
if(givenOption[0]==correctOption[0])
{
clear();
mvprintw(row/3, (col-14)/2, "Correct answer");
refresh();
game.scoreUpdateDirect(k, true);
getch();
break;
}
else{
clear();
mvprintw(row/3, (col-12)/2, "Wrong answer");
mvprintw(row/3+3, (col-correctOption.size())/2-11, "The Correct answer is %s", correctOption.c_str());
refresh();
game.scoreUpdateDirect(k, false);
getch();
break;
}
refresh();
}
}
fin.close();
clear();
game.showAllScore();
refresh();
}
game.finalScreen();
}
if(response==2)
disp.help();
if(response==3)
response=disp.exit();
}
endwin();
return 0;
}