-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.cpp
338 lines (300 loc) · 6.41 KB
/
ui.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "quiz.hpp"
int sub_num, lvl;
char lefthead[STDSTRLEN] = "Difficulty: ";
char righthead[STDSTRLEN] = "Subject: ";
int total_correct, ques_attempted;
int getmode()
{
coord f_uleft, bullet1;
int response, height_ops[SUB_MAX];
//Checking if user wants to play subjectwise or random
clrscr();
frame();
int mode;
char options[2][BIGSTRLEN(2)] = {
"Play according to subject and level",
"Get random questions from all subjects and levels"
};
bullet1 = props(2, options, "Select mode: ", f_uleft, height_ops, NUM);
gotoxy(1, f_uleft.y - 2);
printc(TITLE);
prhead("", "Press ESC to exit", "");
mode = select(bullet1, 2, height_ops, ESC);
if(mode == -2)
{
exit(0);
}
return mode;
}
int prscr_m1(int screen_num)
{
coord f_uleft, bullet1;
int response, height_ops[SUB_MAX];
switch(screen_num)
{
//Screen 1: Select Subject
case 1:
{
clrscr();
frame();
char sub_names[SUB_MAX][BIGSTRLEN(2)];
for(int i = 0; i < num_subs; i++)
{
strcpy(sub_names[i], subs[i].sub_name);
}
bullet1 = props(num_subs, sub_names, "Select Subject:", f_uleft, height_ops, NUM);
gotoxy(1, f_uleft.y - 2);
printc(TITLE);
prhead("", "Press BACKSPACE to go back", "");
response = select(bullet1, 3, height_ops, BCKSPC);
return response;
}
//Screen 2: Select Difficulty
case 2:
{
clrscr();
frame();
prhead("", "Press BACKSPACE to go back", righthead);
char diff_levels[3][BIGSTRLEN(2)] = {"Easy", "Intermediate", "Hard"};
bullet1 = props(3, diff_levels, "Select difficulty level:", f_uleft, height_ops);
gotoxy(1, f_uleft.y - 2);
printc(TITLE);
response = select(bullet1, 3, height_ops, BCKSPC);
return response;
}
//Screen 3: Questions
case 3:
{
int num_ques = subs[sub_num].q_per_level[lvl];
int q_asked[MAXQPERLVL];
for(int i = 0; i < num_ques; i++)
{
q_asked[i] = 0;
}
total_correct = 0;
ques_attempted = 0;
for(i = 0; i < num_ques; i++)
{
//Determining question number
time_t t;
srand((unsigned) time(&t));
int q_num;
do
{
q_num = rand() % num_ques;
}
while(q_asked[q_num] == 1);
q_asked[q_num] = 1;
int res = askq(lvl, sub_num, q_num, i+1);
if(res == -1)
{
break;
}
}
return 0;
}
//Screen 4: Result and asking user if he wants to play again
case 4:
{
clrscr();
frame();
prhead(lefthead, "", righthead);
gotoxy(1, 5); printc(TITLE);
int len = strlen("You got _ out of _ questions correct");
gotoxy((width - len) / 2 + 1, (height - 3) / 2 + 1);
cout << "You got " << total_correct << " out of " << ques_attempted << " questions correct";
cout << endl << endl;
printc("Do you want to play again (y/n) ?");
while(1)
{
char c = getch();
switch(c)
{
case 'y':
case 'Y':
return 1;
case 'n':
case 'N':
return 0;
}
}
}
}
return -1;
}
int prscr_m2()
{
int total_q = 0;
for(int i = 0; i < num_subs; i++)
{
for(int j = 0; j < 3; j++)
{
total_q += subs[i].q_per_level[j];
}
}
time_t t;
srand((unsigned) time(&t));
int q_asked[SUB_MAX][3][MAXQPERLVL];
for(i = 0; i < num_subs; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = 0; k < subs[i].q_per_level[j]; k++)
{
q_asked[i][j][k] = 0;
}
}
}
total_correct = 0;
ques_attempted = 0;
for(i = 0; i < total_q; i++)
{
int sub_num, lvl, q_num;
do
{
sub_num = rand() % num_subs;
lvl = rand() % 3;
q_num = rand() % subs[sub_num].q_per_level[lvl];
}
while(q_asked[sub_num][lvl][q_num] == 1);
q_asked[sub_num][lvl][q_num] = 1;
strcpy(lefthead, "Difficulty: ");
switch(lvl)
{
case 0:
strcat(lefthead, "Easy");
break;
case 1:
strcat(lefthead, "Intermediate");
break;
case 2:
strcat(lefthead, "Hard");
break;
}
strcpy(righthead, "Subject: ");
strcat(righthead, subs[sub_num].short_name);
int res = askq(lvl, sub_num, q_num, i+1);
if(res == -1)
{
break;
}
}
//Screen 4: Result and asking user if he wants to play again
return prscr_m1(4);
}
int play()
{
int mode = getmode();
switch(mode)
{
case 0:
do
{
strcpy(righthead, "Subject: ");
sub_num = prscr_m1(1);
if(sub_num == -1)
{
return 1;
}
strcat(righthead, subs[sub_num].short_name);
lvl = prscr_m1(2);
}
while(lvl == -1);
strcpy(lefthead, "Difficulty: ");
switch(lvl)
{
case 0:
strcat(lefthead, "Easy");
break;
case 1:
strcat(lefthead, "Intermediate");
break;
case 2:
strcat(lefthead, "Hard");
break;
}
prscr_m1(3);
return prscr_m1(4);
case 1:
return prscr_m2();
}
return -1;
}
int askq(int lvl, int sub_num, int q_num, int q_num2) //q_num2 is for printing purposes
{
coord f_uleft, bullet1;
int response, height_ops[SUB_MAX];
//Initialising ques with the question
question ques;
init_ques(lvl, sub_num, q_num, ques);
//Randomising the order of options and setting the correct option number
//according to the new order
int op_chosen[4], op_order[4], op_num = 0;
for(int j = 0; j < 4; j++)
{
op_chosen[j] = 0;
}
time_t t;
srand((unsigned) time(&t));
for(j = 0; j < 4; j++)
{
do
{
op_num = rand() % 4;
}
while(op_chosen[op_num] == 1);
op_chosen[op_num] = 1;
op_order[j] = op_num;
}
int correct;
for(j = 0; j < 4; j++)
{
if(ques.correct == op_order[j])
{
correct = j;
}
}
//Setting the options according to order
char ordered_ops[4][BIGSTRLEN(2)];
for(j = 0; j < 4; j++)
{
strcpy(ordered_ops[j], ques.options[op_order[j]]);
}
//Printing the options
clrscr();
frame();
char centerhead[STDSTRLEN] = "Question ";
char a[5]; itoa(q_num2, a, 10);
strcat(centerhead, a);
prhead(lefthead, centerhead, righthead);
prfoot("", "Press ESC to end the round", "");
bullet1 = props(4, ordered_ops, ques.q, f_uleft, height_ops, ALPHA);
gotoxy(1, f_uleft.y - 2 - 2);
printc(TITLE);
response = select(bullet1, 4, height_ops, ESC);
if(response == -2)
{
return -1;
}
gotoxy(1, f_uleft.y - 2);
ques_attempted++;
if(response == correct)
{
printc("Correct Answer!");
total_correct++;
}
else
{
char out[STDSTRLEN] = "Incorrect Answer! The correct answer was ";
char a[] = {(char) ('A' + correct), '\0'};
strcat(out, a);
printc(out);
}
char c = getch();
switch(c)
{
case 27:
return -1;
}
return 0;
}