-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacMan.c
553 lines (376 loc) · 13.9 KB
/
PacMan.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <conio.h>
#define H 19
#define W 38
void set_cursor_position(int x, int y)
{
COORD coord = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hidecursor()
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
}
void clearScreen() {
system("cls");
}
char map[H][W] =
{
{ "######################################" },
{ "# #" },
{ "# ########### ########### ########## #" },
{ "# # # # # # #" },
{ "# # ####### # # ####### # # ######## #" },
{ "# # # # ####### # # ######## #" },
{ "# # ######### # # # ######## #" },
{ "# # ######### # ####### # # #" },
{ "# # ######### # ####### # ########## #" },
{ "# #" },
{ "# ############# ########## ######### #" },
{ "# # # # # # ## #" },
{ "# # ### # ### # # ###### # # ## ## # #" },
{ "# # ### # ### # # ###### # # ## ## # #" },
{ "# # ### # ### # # # # ## ## # #" },
{ "# # ### # ### # # ###### # # ## ## # #" },
{ "# # ### # ### # # ###### # # ## ## # #" },
{ "# #" },
{ "######################################" }
};
enum mainMenuCommands {Play = 101, Controls = 107, PrintScores = 119, ExitGame = 113};
enum mainMenuCommandsCapsLock {PLAY = 69, CONTROLS = 75, PRINT_SCORES = 87, EXIT_GAME = 81};
enum gamePause {pauseGame = 112};
enum gamePauseCapsLock {PAUSE_GAME = 80};
enum monsterDirection {UP = 119, DOWN = 115, RIGHT = 100, LEFT = 97};
char CharacterOnPath = ' ';
typedef struct Movement{
int columnIndex;
int rowIndex;
int columnMovement;
int rowMovement;
int direction;
} movement;
struct monsterInfo{
int Direction;
int tempDirection;
double shortestDistance;
movement monster;
}Monster;
struct pacManInfo{
movement pacMan;
}PacMan;
typedef struct{
char name[10];
int score;
}playerInfo;
playerInfo info;
int foodControl = 0;
FILE *pDosya;
void clearScreen();
void clearMap();
void createMainMenu();
void controlKeys();
void prepareCharactersForGame();
void createMap(playerInfo *playerData);
void addRecordToScoreboard();
void printScoreboard(playerInfo *playerData);
bool checkMonsterDirection(int monsterColumnIndex, int monsterRowIndex);
int calculateMonsterPacManDistance(int monsterColumnIndex, int monsterRowIndex);
void determineMonsterDirection();
void calculateMonsterMovementValues();
int moveMonster();
int performActionBasedOnInput();
int movePacMan(playerInfo *playerData);
int createFood();
void writeGameOver(playerInfo *playerData);
int main() {
while(1) {
playerInfo *player = NULL;
player = (playerInfo *) malloc (sizeof(playerInfo));
createMainMenu();
int mainMenuCommand = getch();
if(mainMenuCommand == ExitGame || mainMenuCommand == EXIT_GAME)
{
return 0;
}
if(mainMenuCommand == Controls || mainMenuCommand == CONTROLS)
{
clearScreen();
controlKeys();
printf("\nPress any key to return to the main menu");
getch();
}
if(mainMenuCommand == PrintScores || mainMenuCommand == PRINT_SCORES)
{
clearScreen();
printScoreboard(player);
printf("\nPress any key to return to the main menu");
getch();
}
if(mainMenuCommand == Play || mainMenuCommand == PLAY)
{
clearScreen();
int monsterEncounterWithPacman, pacmanEncounterWithMonster;
prepareCharactersForGame();
player->score = 0;
clearMap();
while(1) {
srand(time(NULL));
set_cursor_position(0,0);
hidecursor();
createMap(player);
if(foodControl == 0)
{
createFood();
}
determineMonsterDirection();
monsterEncounterWithPacman = moveMonster();
if(performActionBasedOnInput()) {
break;
}
pacmanEncounterWithMonster = movePacMan(player);
if(pacmanEncounterWithMonster == 0 || monsterEncounterWithPacman == 0){
writeGameOver(player);
addRecordToScoreboard(player);
free(player);
break;
}
}
}
}
}
void createMainMenu() {
clearScreen();
printf("*********************************************************\n");
printf("PacMan v1 \n\n");
printf("Press e to start the game \n\n");
printf("Press k to view controls \n\n");
printf("Press w for the scoreboard\n\n");
printf("Press q to exit the game\n\n");
printf("*********************************************************");
}
void controlKeys() {
printf("*********************************************************\n");
printf("Move Pac-Man up: w \n\n");
printf("Move Pac-Man down: s \n\n");
printf("Move Pac-Man left: a \n\n");
printf("Move Pac-Man right: d \n\n");
printf("Return to main menu during the game: q \n\n");
printf("Pause/resume game: p \n\n");
printf("*********************************************************");
}
void prepareCharactersForGame(){
PacMan.pacMan.columnIndex = 15;
PacMan.pacMan.rowIndex = 17;
PacMan.pacMan.rowMovement = 0;
PacMan.pacMan.columnMovement = 0;
Monster.monster.columnIndex = 1;
Monster.monster.rowIndex = 1;
Monster.shortestDistance = 2000;
}
void addRecordToScoreboard(playerInfo *playerData){
printf("Player: ");
scanf(" %s", playerData->name);
if((pDosya = fopen("PlayerInfo.txt", "a+")) == NULL)
{
printf("File Could Not Be Opened!\n");
exit(1);
}
if(fwrite(playerData, sizeof(info), 1, pDosya) != 1) {
printf("File Data Addition Error!");
getch();
exit(1);
}
fclose(pDosya);
}
void printScoreboard(playerInfo *playerData){
if((pDosya=fopen("PlayerInfo.txt", "a+")) == NULL)
{
printf("File Could Not Be Opened!\n");
exit(1);
}
while(fread(playerData, sizeof(info), 1, pDosya) != 0)
{
printf("\nPlayer: %s | \t", playerData->name);
printf("Score: %d\n\n", playerData->score);
printf("-----------------------------------------\n");
}
}
void clearMap() {
int i,j;
for(i = 0; i < H; i++) {
for(j = 0; j < W; j++) {
if(map[i][j] == '@' || map[i][j] == '&')
map[i][j] = ' ';
}
}
}
void createMap(playerInfo *playerData) {
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 100000000L; // 100 milliseconds
nanosleep(&req, NULL);
int i, j;
for(i = 0; i < H; i++) {
for(j = 0; j < W; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
printf("Score: %d\n", playerData->score);
}
void writeGameOver(playerInfo *playerData){
clearScreen();
printf("*********************************************************\n\n");
printf("\t\t\t GAME OVER \n\n");
printf("*********************************************************\n\n");
printf("Your Score: %d \n\n", playerData->score);
}
bool checkMonsterDirection(int monsterColumnIndex, int monsterRowIndex) {
bool movementApproval = (map[monsterRowIndex][monsterColumnIndex] != '#') ? true : false;
}
int calculateMonsterPacManDistance(int monsterColumnIndex, int monsterRowIndex) {
int columnDistance, rowDistance;
double distance;
columnDistance = monsterColumnIndex - PacMan.pacMan.columnIndex;
rowDistance = monsterRowIndex - PacMan.pacMan.rowIndex;
distance = columnDistance * columnDistance + rowDistance * rowDistance;
if (distance < Monster.shortestDistance) {
Monster.shortestDistance = distance;
Monster.tempDirection = Monster.Direction;
}
}
void determineMonsterDirection() {
if (Monster.monster.direction != LEFT && checkMonsterDirection(Monster.monster.columnIndex + 1, Monster.monster.rowIndex)) {
Monster.Direction = RIGHT;
calculateMonsterPacManDistance(Monster.monster.columnIndex + 1, Monster.monster.rowIndex);
}
if (Monster.monster.direction != RIGHT && checkMonsterDirection(Monster.monster.columnIndex - 1, Monster.monster.rowIndex)) {
Monster.Direction = LEFT;
calculateMonsterPacManDistance(Monster.monster.columnIndex - 1, Monster.monster.rowIndex);
}
if (Monster.monster.direction != UP && checkMonsterDirection(Monster.monster.columnIndex, Monster.monster.rowIndex + 1)) {
Monster.Direction = DOWN;
calculateMonsterPacManDistance(Monster.monster.columnIndex, Monster.monster.rowIndex + 1);
}
if (Monster.monster.direction != DOWN && checkMonsterDirection(Monster.monster.columnIndex, Monster.monster.rowIndex - 1)) {
Monster.Direction = UP;
calculateMonsterPacManDistance(Monster.monster.columnIndex, Monster.monster.rowIndex - 1);
}
Monster.monster.direction = Monster.tempDirection;
Monster.shortestDistance = 2000;
}
void calculateMonsterMovementValues() {
switch (Monster.monster.direction) {
case UP:
Monster.monster.columnMovement = 0;
Monster.monster.rowMovement = -1;
break;
case DOWN:
Monster.monster.columnMovement = 0;
Monster.monster.rowMovement = 1;
break;
case LEFT:
Monster.monster.columnMovement = -1;
Monster.monster.rowMovement = 0;
break;
case RIGHT:
Monster.monster.columnMovement = 1;
Monster.monster.rowMovement = 0;
break;
}
}
int moveMonster() {
calculateMonsterMovementValues();
int columnCheck, rowCheck;
columnCheck = Monster.monster.columnIndex + Monster.monster.columnMovement;
rowCheck = Monster.monster.rowIndex + Monster.monster.rowMovement;
if (!checkMonsterDirection(columnCheck, rowCheck)) {
Monster.monster.columnMovement = 0;
Monster.monster.rowMovement = 0;
} else if (map[rowCheck][columnCheck] == '@') {
return 0;
} else {
map[Monster.monster.rowIndex][Monster.monster.columnIndex] = CharacterOnPath;
CharacterOnPath = map[rowCheck][columnCheck];
Monster.monster.columnIndex += Monster.monster.columnMovement;
Monster.monster.rowIndex += Monster.monster.rowMovement;
map[Monster.monster.rowIndex][Monster.monster.columnIndex] = '&';
}
}
int performActionBasedOnInput() {
if (kbhit()) {
switch (getch()) {
case 'w':
case 'W':
PacMan.pacMan.columnMovement = 0;
PacMan.pacMan.rowMovement = -1;
return 0;
case 's':
case 'S':
PacMan.pacMan.columnMovement = 0;
PacMan.pacMan.rowMovement = 1;
return 0;
case 'a':
case 'A':
PacMan.pacMan.columnMovement = -1;
PacMan.pacMan.rowMovement = 0;
return 0;
case 'd':
case 'D':
PacMan.pacMan.columnMovement = 1;
PacMan.pacMan.rowMovement = 0;
return 0;
case ExitGame:
case EXIT_GAME:
return 1;
case pauseGame:
case PAUSE_GAME:
getch();
return 0;
default:
return 0;
}
}
}
int movePacMan(playerInfo *playerDetails) {
int columnCheck, rowCheck;
columnCheck = PacMan.pacMan.columnIndex + PacMan.pacMan.columnMovement;
rowCheck = PacMan.pacMan.rowIndex + PacMan.pacMan.rowMovement;
if (map[rowCheck][columnCheck] == '#') {
PacMan.pacMan.columnMovement = 0;
PacMan.pacMan.rowMovement = 0;
}
if (map[rowCheck][columnCheck] == '&') {
return 0;
} else {
map[PacMan.pacMan.rowIndex][PacMan.pacMan.columnIndex] = ' ';
PacMan.pacMan.columnIndex += PacMan.pacMan.columnMovement;
PacMan.pacMan.rowIndex += PacMan.pacMan.rowMovement;
if (map[PacMan.pacMan.rowIndex][PacMan.pacMan.columnIndex] == '+') {
playerDetails->score += 1;
foodControl = 0;
}
map[PacMan.pacMan.rowIndex][PacMan.pacMan.columnIndex] = '@';
}
}
int createFood() {
int foodPositionX, foodPositionY;
foodPositionX = 1 + rand() % (W - 1);
foodPositionY = 1 + rand() % (H - 1);
if (map[foodPositionY][foodPositionX] != '#' && map[foodPositionY][foodPositionX] != '@' && map[foodPositionY][foodPositionX] != '&') {
map[foodPositionY][foodPositionX] = '+';
foodControl++;
} else {
return createFood();
}
}