-
Notifications
You must be signed in to change notification settings - Fork 7
/
BasicGame.c
509 lines (434 loc) · 10.6 KB
/
BasicGame.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include <stdbool.h>
#include <math.h>
#define MAX 10
#define A "P"
#define B "Q"
int getRandom(int low, int high);
int getValidInteger(int low, int high);
unsigned int playerRoll(int low, int high);
void seed(void)
{
srand(time(NULL));
}
void space(unsigned int size) // create space
{
printf(" ");
}
char getDisplayType(unsigned int index, unsigned int playerPosition, char playerName) // check the index and return character
{
if (playerName != '#')
{
if (index == playerPosition)
return playerName;
if (index == 0)
return ('C');
if (index % 3 == 0)
if (index % 5 == 0)
if (index % 7 == 0)
return('G');
else return ('L');
else return ('W');
if (index % 5 == 0)
if (index % 7 == 0)
return('G');
else return ('L');
if (index % 7 == 0) {
return('G');
}
else return (' ');
}
if (playerName == '#')
{
if (index == 0)
return ('C');
if (index % 3 == 0) {
if (index % 5 == 0) {
if (index % 7 == 0) {
return('G');
}
else return ('L');
}
else return ('W');
}
if (index % 5 == 0) {
if (index % 7 == 0) {
return('G');
}
else return ('L');
}
if (index % 7 == 0) {
return('G');
}
else
return (' ');
}
}
void firstLine(unsigned int size) // create the upper line of the square for the first and last line
{
int i;
for (i = 0; i < size; i++)
{
printf(" ___ ");
}
printf("\n");
}
void secondLine(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) //create the inside and return the type for the first line
{
int x, j;
for (j = 0; j < size; j++)
{
x = j;
printf("| %c |", getDisplayType(x, playerPosition, playerName));
}
printf("\n");
}
void secondLine2nd(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) { //create the inside and return the type for the last line
int x, y, z;
y = 3 * (size - 1);
printf("| %c |", getDisplayType(y, playerPosition, playerName));
for (i = 0; i < size - 2; i++)
{
z = 2 * (size - 1) + ((size - 2) - i);
printf("| %c |", getDisplayType(z, playerPosition, playerName));
}
x = size + i;
printf("| %c |", getDisplayType(x, playerPosition, playerName));
printf("\n");
}
void thirdLine(unsigned int size) // create lower line for the first and last line
{
int i;
for (i = 0; i < size; i++)
{
printf("|___|");
}
printf("\n");
}
void upperrow(unsigned int size) // create the upper line for the square of the row
{
int i;
printf(" ___");
for (i = 0; i < size - 2; i++) {
space(size);
}
printf(" ___");
printf("\n");
}
void lowerrow(unsigned int size) //create the lower line for the square of the row
{
int i;
printf("|___|");
for (i = 0; i < size - 2; i++) {
space(size);
}
printf("|___|");
printf("\n");
}
void middlerow(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) //create the inside and return the type for the square of the row
{
int q, b, p;
b = i;
q = 3 * (size - 1) + ((size - 1) - i);
printf("| %c |", getDisplayType(q, playerPosition, playerName));
for (b = 0; b < size - 2; b++) {
space(size);
}
p = size - 1 + i;
printf("| %c |", getDisplayType(p, playerPosition, playerName));
printf("\n");
}
char getValidCharacter(char a, char b) //make sure the character input is right//
{
char c, choice;
do {
scanf_s("%c", &choice);
getchar();
if ((choice != 'p' && choice != 'q' && choice != 'r' && choice != 's'))
{
printf("Invalid input, please try again: ");
}
} while (choice != 'p' && choice != 'q' && choice != 'r' && choice != 's');
return choice;
}
int getValidInteger(int low, int high) //validate the input number//
{
int a; int choice;
do {
a = scanf_s("%d", &choice);
if (choice <low || choice >high)
{
printf("Invalid input, please try again: ");
}
} while (choice < low || choice > high);
return choice;
}
int getRandom(int low, int high) //get a rando number
{
int a;
a = rand() % (high - low) + low;
return a;
}
unsigned int playerRoll(int low, int high) //prompt and output the roll
{
int a = 1, b, c, d, e, choice;
do {
printf("\nyour turn, how many dice will you roll : ");
scanf_s("%d", &choice);
if (choice == 1)
{
b = getRandom(low, high);
//printf("b: \n");
//scanf_s("%d", &b);
printf("You rolled %d\n", b);
printf("Advancing %d space\n", b);
a = 0;
return b;
}
else if (choice == 2)
{
c = getRandom(low, high);
//printf("c: ");
//scanf_s("%d", &c);
d = getRandom(low, high);
//printf("d: ");
//scanf_s("%d", &d);
b = c + d;
printf("You rolled %d %d\n ", c, d);
printf("Advancing %d space\n", b);
a = 0;
return b;
}
else if (choice == 3) {
c = getRandom(low, high);
d = getRandom(low, high);
e = getRandom(low, high);
b = c + d + e;
printf("You rolled %d %d %d\n ", c, d, e);
printf("Advancing %d space\n", b);
a = 0;
return b;
}
else
printf("Try again,");
} while (a = 1);
}
void winPrize(int playerPrizes[], unsigned int* prizeCount) //do the winprize function
{
int i;
unsigned int prize;
prize = getRandom(10, 100);
printf("%d\n", prize);
if (*prizeCount < MAX)
{
playerPrizes[*prizeCount] = prize;
printf("you won a prize of %d\n", prize);
*prizeCount = *prizeCount + 1;
}
else
printf("Your inventory is full \n");
}
void winGrandPrize(int playerPrizes[], unsigned int* prizeCount) // do the win grand prize function
{
int i;
unsigned int prize;
prize = getRandom(100, 200);
printf("%d\n", prize);
if (*prizeCount < MAX)
{
playerPrizes[*prizeCount] = prize;
printf("you won a grand prize of %d\n", prize);
*prizeCount = *prizeCount + 1;
}
else
printf("Your inventory is full ");
}
int loseItem(int playerPrizes[], unsigned int *prizeCount) // do the loseitem fuction
{
int i, j, k, r, ran = 2;
if (*prizeCount == 0)
{
printf("Nothing happened,Move On\n");
}
else
{
ran = getRandom(0, *prizeCount);
playerPrizes[ran] = 0;
*prizeCount = *prizeCount - 1;
printf("you lost the prize");
for (i = ran - 1; i < MAX; i++) //arange the array in order
for (j = i; j < MAX; j++)
if (playerPrizes[i] == 0)
{
k = playerPrizes[i];
playerPrizes[i] = playerPrizes[j];
playerPrizes[j] = k;
}
}
}
void initPlayer(int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, int *playerPosition) //do the initplayer function, set everything to 0
{
int i;
playerPrizes[MAX] = 0;
*playerScore = 0;
printf("playerPrizes: %d\n", playerPrizes[MAX]);
*prizeCount = 0;
*playerPosition = 0;
printf("Enter Player ID: ");
scanf_s("%c", playerName);
}
void displayBoard(unsigned int size, unsigned int playerPosition, char playerName) //display the boardgame
{
int k, size1, loop;
float loop2, playerPosition1, size2;
//printf("player name in display board: %c\n", playerName);
//printf("%d\n", r);
//printf("%d", playerPosition);
playerPosition1 = (float)playerPosition;
size1 = (4 * (size - 1));
size2 = (float)size1;
//printf("size2: %.2f\n", size2);
// printf("playerPo1: %.2f\n", playerPosition1);
//playerPosition2 = float size;
//printf("playerPos: %d\n", playerPosition);
loop2 = playerPosition1 / size2;
loop = trunc(loop2);
k = playerPosition - (4 * (size - 1))*loop;
playerPosition = k;
{
int i = 0;
if (size == 1)
{
printf(" ___ \n");
printf(" | ? | \n");
printf(" |___|");
printf("\n");
}
else {
for (i = 0; i < size - 1; i++)
{
if (i == 0)
{
firstLine(size);
secondLine(size, i, playerPosition, playerName);
thirdLine(size);
}
}
for (i = 1; i < size - 1; i++)
{
upperrow(size);
middlerow(size, i, playerPosition, playerName);
lowerrow(size);
}
for (i = size - 2; i < size - 1; i++)
{
firstLine(size);
secondLine2nd(size, i, playerPosition, playerName);
thirdLine(size);
}
}
}
}
int checkout(int *playerScore, int playerPrizes[], unsigned int* prizeCount) //do the checkout
{
int i;
for (i = 0; i < *prizeCount; i++)
*playerScore += playerPrizes[i];
*prizeCount = 0;
printf("You check out for $%d score is now: $%d \n", *playerScore, *playerScore);
if (*playerScore >= 200)
{
return 1;
}
else
{
return 0;
}
}
void playGame(unsigned int size, int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, int* playerPosition) //play the game
{
printf("playerName in playgame %c\n", *playerName);
//printf("%d\n",*prizeCount);
//printf("%d\n", *playerScore);
int i, l = 1;
while (l)
{
displayBoard(size, *playerPosition, *playerName);
printf("Score: %d inventory (%d items): ", *playerScore, *prizeCount);
for (i = 0; i < *prizeCount; i++) {
printf("%d, ", playerPrizes[i]);
}
*playerPosition = *playerPosition + playerRoll(1, 6);
if (*playerPosition >= 4 * (size - 1))
*playerPosition = *playerPosition - 4 * (size - 1);
//printf("player position in display %d\n", *playerPosition);
//printf("display type in play game: %c\n", getDisplayType(*playerPosition, *playerPosition, '#'));
//displayBoard(boardSize, playerPosition, playerName);
if (getDisplayType(*playerPosition, *playerPosition, '#') == 'G')
{
winGrandPrize(playerPrizes, prizeCount);
}
else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'W')
{
winPrize(playerPrizes, prizeCount);
}
else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'L')
{
loseItem(playerPrizes, prizeCount);
}
else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'C')
{
if (checkout(playerScore, playerPrizes, prizeCount) == 1)
{
printf("You Win\n");
l = 0;
}
}
else
printf("nothing happens, go again.\n");
}
}
int main(void)
{
int i, l = 1;
char a, choice;
char c = '#';
int playerScore;
int playerPrizes[MAX];
unsigned int prizeCount;
char playerName;
unsigned int size;
unsigned int playerPosition;
printf("Welcome to CHECKOUT\n");
while (l) {
printf("Main Menu\n");
printf("p-(p)lay q-(q)uit r-inst(r)uctions s-HI(s)core: \n");
choice = getValidCharacter('P', 'Q');
if (choice == 'p') {
printf("Number of players is 1\n");
initPlayer(&playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition);
printf("Enter board size: ");
scanf_s("%d", &size);
playGame(size, &playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition);
getchar();
}
if (choice == 's')
{
printf("--\n");
printf(" \\ ");
printf("_______\n");
printf(" \\++++++|\n");
printf(" \\=====|\n");
printf(" 0--- 0\n");
printf("HI SCORE: %d Player Name: %c \n", playerScore, playerName);
}
if (choice == 'q')
{
printf("dont go, I will miss you :(");
l = 0;
getchar();
}
}
}