forked from smart-worker/Guessing-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
418 lines (416 loc) · 16.3 KB
/
main.c
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
FILE *f, *f1;
reset_score:
f = fopen("scoreA.csv", "a");
f1 = fopen("scoreN.csv", "a");
int size = 0;
if (f != NULL)
{
fseek(f, 0, SEEK_END);
size = ftell(f);
if (size == 0)
fprintf(f, "Name\t|\tAttempts\n");
}
if (f1 != NULL)
{
fseek(f1, 0, SEEK_END);
size = ftell(f1);
if (size == 0)
fprintf(f1, "Name\t|\tAttempts\n");
}
fclose(f);
fclose(f1);
mainscreen:
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\t\t\tGUESSING GAME\n");
printf("\t\t\t\t\t==================================================================================================================================================");
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\t> Press S to start a NEW game\n\n\t\t\t\t\t\t\t\t\t\t\t\t> Press V to view SCORECARD\n\n\t\t\t\t\t\t\t\t\t\t\t\t> Press Q to QUIT\n\n");
printf("\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
char choice = toupper(getch());
if (choice == 'Q')
{
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\tThank You for trying this game. Hope to see you back soon.\n");
exit(1);
}
else if (choice == 'V')
{
view_score:
system("cls");
int boardchoice = 0;
printf("\t\t\t\t\t\t\t\t\t\t\t\t\t[1] - Number Scoreboard\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[2] - Alphabet Scoreboard\n\n");
printf("\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
scanf("%d", &boardchoice);
if (boardchoice == 1)
{
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\t\t\tNUMBER SCOREBOARD\t\t\t\t\t\t[R] - Reset\n");
printf("\t\t\t\t\t>>--------------------------------------------------------------------------------------------------------------------------------------------<<\n\n");
printf("\t\t\t\t\t\t\t\t\t\t\t\tNAME\t\tATTEMPTS\n");
printf("\t\t\t\t\t\t\t\t\t\t\t\t----\t\t--------\n\n");
FILE *file;
file = fopen("scoreN.csv", "r");
char buffer[500];
int row = 0;
int column = 0;
while (fgets(buffer, 500, file))
{
column = 0;
row++;
if (row == 1)
continue;
char *value = strtok(buffer, ", ");
while (value)
{
// Column 1
if (column == 0)
{
printf("\t\t\t\t\t\t\t\t\t\t\t\t");
}
// Column 2
if (column == 1)
{
printf("\t\t ");
}
printf("%s", value);
value = strtok(NULL, ", ");
column++;
}
printf("\n");
}
fclose(file);
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[Q]Exit Game\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[M]Return to Main Menu\n\n");
printf("\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
char Vchoice = toupper(getch());
if (Vchoice == 'Q')
{
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\tThank You for trying this game. Hope to see you back soon.\n\n");
exit(1);
}
else if (Vchoice == 'M')
{
goto mainscreen;
}
else if (Vchoice == 'R')
{
printf("\n\t\t\t\t\t\t\t\t\t\t\tAre you sure to reset the scoreboard?(Y/N)");
char exitchoice = getch();
if (toupper(exitchoice) == 'Y')
{
remove("scoreN.csv");
goto reset_score;
}
else
goto view_score;
}
else
printf("\n\n\t\t\t\t\t\t\t\t\t\t\tPlease enter a valid choice. Exiting the game.");
}
else if (boardchoice == 2)
{
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\t\t\tALPHALBET SCOREBOARD\t\t\t\t\t\t[R] - Reset\n");
printf("\t\t\t\t\t>>--------------------------------------------------------------------------------------------------------------------------------------------<<\n\n");
printf("\t\t\t\t\t\t\t\t\t\t\t\tNAME\t\tATTEMPTS\n");
printf("\t\t\t\t\t\t\t\t\t\t\t\t----\t\t--------\n\n");
FILE *file1;
file1 = fopen("scoreA.csv", "r");
char buffer1[500];
int row = 0;
int column = 0;
while (fgets(buffer1, 500, file1))
{
column = 0;
row++;
if (row == 1)
continue;
char *value1 = strtok(buffer1, ", ");
while (value1)
{
// Column 1
if (column == 0)
{
printf("\t\t\t\t\t\t\t\t\t\t\t\t");
}
// Column 2
if (column == 1)
{
printf("\t\t ");
}
printf("%s", value1);
value1 = strtok(NULL, ", ");
column++;
}
printf("\n");
}
fclose(file1);
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[Q]Exit Game\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[M]Return to Main Menu\n\n");
printf("\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
char Vchoice = toupper(getch());
if (Vchoice == 'Q')
{
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\tThank You for trying this game. Hope to see you back soon.\n\n");
exit(1);
}
else if (Vchoice == 'M')
goto mainscreen;
else if (Vchoice == 'R')
{
printf("\n\t\t\t\t\t\t\t\t\t\t\tAre you sure to reset the scoreboard?(Y/N)");
char exitchoice1 = getch();
if (toupper(exitchoice1) == 'Y')
{
remove("scoreA.csv");
goto reset_score;
}
else
goto view_score;
}
else
printf("\n\n\t\t\t\t\t\t\t\t\t\t\tPlease enter a valid choice. Exiting the game.");
}
else
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\tPlease enter a valid choice. Exiting the game.");
}
}
else if (choice == 'S')
{
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\t\tDo you want to play with:\t\t\t\t\t\t\t\t\t[3]Exit\n\n \t\t\t\t\t\t\t\t\t\t\t\t\t1) Numbers\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t2) Alphabets\n\n\t\t\t\t\t\t\t\t\t\t\t\tPlease enter your choice: ");
int gamechoice = 0;
scanf("%d", &gamechoice);
switch (gamechoice)
{
case 1:
system("cls");
char name[20];
printf("\t\t\t\t\t\t\t\t\t\t\t\tEnter your username: ");
scanf("%s", &name);
FILE *file;
file = fopen("scoreN.csv", "a");
fprintf(file, "%s\t\t", name);
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\t\tWhich difficulty level would you like?\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[1]-Easy\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[2]-Medium\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[3]-Hard\n");
int difficulty = 0;
printf("\n\t\t\t\t\t\t\t\t\t\t\t\t\t");
scanf("%d", &difficulty);
switch (difficulty)
{
case 1:
system("cls");
srand(time(0));
int target = (rand() % 20) + 1;
int guess = 0, nGuess = 0;
printf("\t\t\t\t\t\t\t\t\t\t\t\tChoose a number between 1 and 20: ");
while (guess != target)
{
scanf("%d", &guess);
nGuess++;
if (guess == target)
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tYou are correct. You made it in %d guesses.", nGuess);
fprintf(file, "%d\n", nGuess);
break;
}
else if (guess > target)
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a lower number: ");
}
else
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a higher number: ");
}
}
break;
case 2:
system("cls");
srand(time(0));
target = (rand() % 50) + 1;
nGuess = 0;
guess = 0;
printf("\t\t\t\t\t\t\t\t\t\t\t\tChoose a number between 1 and 50: ");
while (guess != target)
{
scanf("%d", &guess);
nGuess++;
if (guess == target)
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tYou are correct. You made it in %d guesses.", nGuess);
fprintf(file, "%d\n", nGuess);
break;
}
else if (guess > target)
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a lower number: ");
}
else
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a higher number: ");
}
}
break;
case 3:
system("cls");
srand(time(0));
target = (rand() % 100) + 1;
nGuess = 0;
guess = 0;
printf("\t\t\t\t\t\t\t\t\t\t\t\tChoose a number between 1 and 100: ");
while (guess != target)
{
scanf("%d", &guess);
nGuess++;
if (guess == target)
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tYou are correct. You made it in %d guesses.", nGuess);
fprintf(file, "%d\n", nGuess);
break;
}
else if (guess > target)
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a lower number: ");
}
else
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a higher number: ");
}
}
break;
}
fclose(file);
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\t[Q]Exit Game\n\n\t\t\t\t\t\t\t\t\t\t\t\t[M]Return to Main Menu\n\n");
int Vchoice = toupper(getch());
if (Vchoice == 'Q')
{
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\tThank You for trying this game. Hope to see you back soon.\n\n");
exit(1);
}
else if (Vchoice == 'M')
{
goto mainscreen;
}
break;
case 2:
system("cls");
char name1[20];
printf("\t\t\t\t\t\t\t\t\t\t\t\tEnter your username: ");
scanf("%s", &name1);
FILE *file1;
file1 = fopen("scoreA.csv", "a");
fprintf(file1, "%s\t\t", name1);
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\t\tWhich difficulty level would you like?\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[1]-Beginner\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[2]-Pro\n\n");
difficulty = 0;
printf("\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
scanf("%d", &difficulty);
int target, nGuess, Ltarget, Htarget;
char targ1, targ2;
char target1, guessAlpha = '*';
switch (difficulty)
{
case 1:
system("cls");
srand(time(0));
target = 0;
nGuess = 0;
target = (rand() % 26) + 97;
target1 = (char)target;
printf("\t\t\t\t\t\t\t\t\t\t\t\tEnter a lower-case alphabet: ");
while (guessAlpha != target1)
{
guessAlpha = getch();
printf("%c", guessAlpha);
nGuess++;
if (guessAlpha == target1)
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tYou are correct. You made it in %d guesses.", nGuess);
fprintf(file1, "%d\n", nGuess);
break;
}
else if (guessAlpha < target1)
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a higher alphabet towards z: ");
else
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a lower alphabet towards a: ");
}
break;
case 2:
Ltarget = 0, Htarget = 0, nGuess = 0;
system("cls");
srand(time(0));
Ltarget = (rand() % 26) + 97;
Htarget = (rand() % 26) + 65;
targ1 = (char)Ltarget;
targ2 = (char)Htarget;
printf("\t\t\t\t\t\t\t\t\t\t\t\tEnter any alphabet: ");
while (guessAlpha != targ1 && guessAlpha != targ2)
{
guessAlpha = getch();
printf("%c", guessAlpha);
nGuess++;
if (guessAlpha == targ1 || guessAlpha == targ2)
{
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tYou are correct. You made it in %d guesses.", nGuess);
fprintf(file1, "%d\n", nGuess);
break;
}
else if (guessAlpha < 97)
{
if (guessAlpha < targ2)
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a uppercase alphabet towards Z: ");
else
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a uppercase alphabet towards A: ");
}
else if (guessAlpha < targ1)
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a lowercase alphabet towards z: ");
else
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\tGuess a lowercase alphabet towards a: ");
}
break;
}
fclose(file1);
printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[Q]Exit Game\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t[M]Return to Main Menu\n\n");
printf("\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
Vchoice = toupper(getch());
if (Vchoice == 'Q')
{
system("cls");
printf("\t\t\t\t\t\t\t\t\t\t\tThank You for trying this game. Hope to see you back soon.\n\n");
exit(1);
}
else if (Vchoice == 'M')
{
goto mainscreen;
}
case 3:
goto mainscreen;
break;
default:
printf("\n\n\t\t\t\t\t\t\t\t\t\t\tPlease enter a valid choice. Exiting the game.");
}
}
else
{
printf("\n\n\n\n\t\t\t\t\t\t\t\t\t\t\t\t\tEnter a valid option.\n\n\t\t\t\t\t\t\t\t\t\t\t\tIf you wish to return to mainscreen enter R.\n\n\n\t\t\t\t\t\t\t\t\t\t\t\t\tPress anything else to quit.\n\n");
char c;
c = toupper(getch());
if (c == 'R')
goto mainscreen;
else
{
printf("\t\t\t\t\t\t\t\t\t\t\tThank You for trying this game. Hope to see you back soon.\n\n");
exit(1);
}
}
return 0;
}