-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
754 lines (667 loc) · 18.7 KB
/
main.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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//*********************************************************************
// Computer Graphic using OpenGL
// by : Paulo Eduardo Ramos de Araujo
// [02/10/2022]
//*********************************************************************
// LIBRARYS
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#include <sstream>
#include <time.h>
#include <windows.h>
#include <mmsystem.h> // Sound
#include <conio.h>
#include <fstream>
#include "Textura.h"
#include "GameObjectDesigner.h"
// VARIABLES
// -- Window
#define WindowSizeX 720
#define WindowSizeY 720
// -- Game Dificulty [1..10]
#define GAMELEVEL 9
// -- Map
#define GAMEMAPNAME "pacman-map.txt"
#define FLOOR 0
#define WALL 1
#define FOOD 2
#define POWER 3
#define DOOR 4
#define REDGHOST 5
#define BLUEGHOST 6
#define ORANGEGHOST 7
#define PURPLEGHOST 8
#define PACMAN 9
#define MAPSIZE 20
#define TAM 0.1f
#define MAT2X(j) ((j)* TAM - 1)
#define MAT2Y(i) (0.9 - (i) * TAM)
// -- GamePlay
typedef struct
{
int x, y;
} Coord;
typedef struct
{
int gameMap[MAPSIZE][MAPSIZE];
int foodCount;
string gameMapName;
int doorX, doorY;
bool gameOver;
bool gameBeginning;
} PacmanGame;
typedef struct
{
int x, y;
int points;
bool invencible;
int objectBelowPlayer;
bool hasStarted;
} Player;
PacmanGame gameEngine;
Player pacmanPlayer;
Player ghostPlayer[4];
// -- Textures
Textura gameTex[9];
// Program Functions
int main(int argc, char * argv[]);
void initCullFace();
void initLighting();
void initGame();
void readArchiveMap(string mapName);
void reshape(int width, int height);
void display(void);
void drawMap();
void setAndLoadText();
void drawObject(float column, float row, int object);
void checkWinCondition();
void checkPacmanDead();
void key(unsigned char key, int x, int y);
void movePacman(int moveCode);
void clockFunction(int clock);
void knockDownGameDoor();
void moveGhostRandomly(int ghostID);
void showGameMap();
int randNumber(int min, int max);
// Main
int main(int argc, char * argv[])
{
glutInit( & argc, argv); // Init Glut
glutInitWindowSize(WindowSizeX, WindowSizeY); // Define Window Size
glutInitWindowPosition(10, 10); // Window Started Position
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); // Display Mode
glutCreateWindow("PacMan 3D - Pumba Developer"); // Create Window With Name
glClearColor(0, 0, 0, 1); // Window Background Color
initCullFace();
initLighting();
initGame();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutTimerFunc(100, clockFunction, 1);
glutMainLoop();
return EXIT_SUCCESS;
}
void initCullFace()
{
// -- Face Culling
glEnable(GL_DEPTH_TEST); // Enable Z-buffer Algorithm
glEnable(GL_CULL_FACE); // Enable Face Culling
glCullFace(GL_BACK); // Cull Face = Clock Wise (Back)
}
void initLighting()
{
// -- GL_LIGHTING
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
float globalAmb[] = { 0.1f, 0.1f, 0.1f, 1.f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmb);
float light0[4][4] =
{
{0.1f, 0.1f, 0.1f, 1.f}, // ambient
{0.8f, 0.8f, 0.8f, 1.f}, // diffuse
{ 1.f, 1.f, 1.f, 1.f }, // speculargi
{0.f, 0.f, 1.f, 1.f} // position
};
glLightfv(GL_LIGHT0, GL_AMBIENT, & light0[0][0]);
glLightfv(GL_LIGHT0, GL_DIFFUSE, & light0[1][0]);
glLightfv(GL_LIGHT0, GL_SPECULAR, & light0[2][0]);
glLightfv(GL_LIGHT0, GL_POSITION, & light0[3][0]);
}
void initGame()
{
// Reset Game Data
gameEngine.gameBeginning = true;
gameEngine.gameOver = false;
gameEngine.foodCount = 0;
pacmanPlayer.points = 0;
pacmanPlayer.invencible = false;
// Set and Load Game Map
gameEngine.gameMapName = GAMEMAPNAME;
readArchiveMap(gameEngine.gameMapName);
showGameMap();
}
void readArchiveMap(string mapName)
{
char charMapName[mapName.length() + 1]; // Create Array of Char For Map Name
strcpy(charMapName, mapName.c_str()); // Copy String into Array of Char
ifstream archiveMap; // Create Archive Stream Object
archiveMap.open(charMapName); // Open Archive
string archiveCurrentLine; // Variable To Save Current Line
if(archiveMap.is_open())
{
cout << "Arquivo de Mapa Aberto com Sucesso!\n" << endl;
int row = 0;
int column = 0;
while(!archiveMap.eof())
{
getline(archiveMap, archiveCurrentLine);
// cout << archiveCurrentLine << endl;
for(int i = 0; i < (int)archiveCurrentLine.length(); i++) // add line to gamemap row;
{
char str = archiveCurrentLine[i]; // convert to char
int objectNumber = str - '0'; // convert chart ascii to integer
if((int)str != 9) // verify if is a empty value
{
gameEngine.gameMap[row][column] = objectNumber;
// cout << "index:: " << row << "|" << column << " || objectNumber:: " << objectNumber << " || gameMap:: " << gameEngine.gameMap[row][column] << endl;
// verify Objects and Set Params
if(objectNumber == FOOD)
{
gameEngine.foodCount += 1;
}
else if(objectNumber == PACMAN)
{
pacmanPlayer.x = column;
pacmanPlayer.y = row;
}
else if (objectNumber == DOOR)
{
gameEngine.doorX = column;
gameEngine.doorY = row;
}
else if (objectNumber == REDGHOST)
{
ghostPlayer[0].x = column;
ghostPlayer[0].y = row;
ghostPlayer[0].hasStarted = true;
ghostPlayer[0].objectBelowPlayer = 0;
}
else if (objectNumber == BLUEGHOST)
{
ghostPlayer[1].x = column;
ghostPlayer[1].y = row;
ghostPlayer[1].hasStarted = true;
ghostPlayer[1].objectBelowPlayer = 0;
}
else if (objectNumber == PURPLEGHOST)
{
ghostPlayer[2].x = column;
ghostPlayer[2].y = row;
ghostPlayer[2].hasStarted = true;
ghostPlayer[2].objectBelowPlayer = 0;
}
else if (objectNumber == ORANGEGHOST)
{
ghostPlayer[3].x = column;
ghostPlayer[3].y = row;
ghostPlayer[3].hasStarted = true;
ghostPlayer[3].objectBelowPlayer = 0;
}
column += 1; // Incremente Column
}
}
// Protect Matriz Size
if(row >= MAPSIZE - 1 && column >= MAPSIZE - 1)
{
// cout << "CABO PORRA" << endl;
break;
}
else
{
row += 1;
column = 0;
}
}
archiveMap.close();
cout << "Arquivo de Salvo com Sucesso!\n" << endl;
}
else
{
cout << "Problemas na Abertura do Arquivo de Mapa!\n" << endl;
}
}
void reshape(int w, int h)
{
// Evitar Divis�o por Zero
if(h == 0) h = 1;
glViewport (0, 0, w, h); // Viewport Dimensions
// Init Coord System
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)w / (float)h;
gluPerspective(60, aspect, 1.0, 20.0);
gluLookAt(0.0, -1.3, 2.2, // posição da câmera (olho)
0.0, 0.0, 0.0, // centro da cena
0.0, 1.0, 0.0); // direção de cima
glMatrixMode (GL_MODELVIEW);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clean Collor Buffer
glLoadIdentity(); // Load Identity Matriz
// -- Draw The Map And Objects
drawMap();
// -- Checks
checkPacmanDead();
checkWinCondition();
glutSwapBuffers();
}
void drawMap()
{
// Loading Textures
setAndLoadText();
// -- Map
for(int i = 0; i < MAPSIZE; i++)
for(int j = 0; j < MAPSIZE; j++)
{
drawObject(MAT2X(j), MAT2Y(i), gameEngine.gameMap[i][j]);
}
}
void setAndLoadText()
{
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
gameTex[WALL].load("textures/wood.png");
gameTex[DOOR].load("textures/brick.png");
}
void drawObject(float column, float row, int object)
{
glPushMatrix();
glTranslatef (column + TAM, row + TAM, 0.0);
switch(object)
{
case FLOOR:
{
// Nothing
break;
}
case WALL:
{
float color[3] = {0.0, 0.0, 220.0};
desenhaCubo( & gameTex[WALL], TAM, color);
break;
}
case FOOD:
glColor3f(0.9, 0.9, 0.9);
glutSolidSphere(TAM * 0.2, 30, 30);
break;
case POWER:
glColor3f(0.8, 0.8, 0.8);
glutSolidSphere(TAM * 0.4, 30, 30);
break;
case DOOR:
{
float color[3] = {205, 127, 50};
desenhaCubo( & gameTex[DOOR], TAM, color);
break;
}
case REDGHOST:
glColor3f(0.8, 0, 0);
glutSolidSphere(TAM * 0.6, 30, 30);
break;
case BLUEGHOST:
glColor3f(0.8, 0.2, 0.2);
glutSolidSphere(TAM * 0.6, 30, 30);
break;
case ORANGEGHOST:
glColor3f(0.8, 0.2, 0);
glutSolidSphere(TAM * 0.6, 30, 30);
break;
case PURPLEGHOST:
glColor3f(0.8, 0, 0.2);
glutSolidSphere(TAM * 0.6, 30, 30);
break;
case PACMAN:
glColor3f(1, 1, 0);
glutSolidSphere(TAM * 0.7, 30, 30);
break;
}
glPopMatrix();
}
void checkWinCondition()
{
if(pacmanPlayer.points == gameEngine.foodCount)
{
gameEngine.gameOver = true;
PlaySound(TEXT("sounds/gamewin.wav"), NULL, SND_ASYNC);
}
}
void checkPacmanDead()
{
for(int i = 0 ; i < 4 ; i++)
{
if(ghostPlayer[i].objectBelowPlayer == PACMAN)
{
gameEngine.gameOver = true;
PlaySound(TEXT("sounds/gameover.wav"), NULL, SND_ASYNC);
}
}
}
void key(unsigned char key, int x, int y)
{
// Check if Game Over or Beginning
if(gameEngine.gameOver || gameEngine.gameBeginning) return;
switch (key)
{
case 27 :
exit(0);
break;
case 'w':
movePacman(0); // up
break;
case 'a':
movePacman(1); // left
break;
case 's':
movePacman(2); // down
break;
case 'd':
movePacman(3); // right
break;
case 'q':
abort();
break;
}
glutPostRedisplay();
}
void movePacman(int moveCode)
{
// Check if Game Over or Beginning
if(gameEngine.gameOver || gameEngine.gameBeginning) return;
Coord north = {pacmanPlayer.x, pacmanPlayer.y - 1};
Coord left = {pacmanPlayer.x - 1, pacmanPlayer.y};
Coord down = {pacmanPlayer.x, pacmanPlayer.y + 1};
Coord right = {pacmanPlayer.x + 1, pacmanPlayer.y};
switch(moveCode)
{
case 0:
if(gameEngine.gameMap[north.y][north.x] == WALL)
{
cout << "Walking Upper Forbidden || WALL" << endl;
}
else if(gameEngine.gameMap[north.y][north.x] == FLOOR)
{
cout << "Walking Upper || FLOOR" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[north.y][north.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = north.x;
pacmanPlayer.y = north.y;
}
else if (gameEngine.gameMap[north.y][north.x] == FOOD)
{
cout << "Walking Upper || FOOD" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[north.y][north.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = north.x;
pacmanPlayer.y = north.y;
// Add Point to Pacman
pacmanPlayer.points += 1;
}
else if (gameEngine.gameMap[north.y][north.x] == POWER)
{
cout << "Walking Upper || POWER" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[north.y][north.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = north.x;
pacmanPlayer.y = north.y;
// Set PacMan Invensible
pacmanPlayer.invencible = true;
}
break;
case 1:
if(gameEngine.gameMap[left.y][left.x] == WALL)
{
cout << "Walking Left Forbidden || WALL" << endl;
}
else if(gameEngine.gameMap[left.y][left.x] == FLOOR)
{
cout << "Walking Left || FLOOR" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[left.y][left.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = left.x;
pacmanPlayer.y = left.y;
}
else if (gameEngine.gameMap[left.y][left.x] == FOOD)
{
cout << "Walking Left || FOOD" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[left.y][left.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = left.x;
pacmanPlayer.y = left.y;
// Add Point to Pacman
pacmanPlayer.points += 1;
}
else if (gameEngine.gameMap[left.y][left.x] == POWER)
{
cout << "Walking Left || POWER" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[left.y][left.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = left.x;
pacmanPlayer.y = left.y;
// Set PacMan Invensible
pacmanPlayer.invencible = true;
}
break;
case 2:
if(gameEngine.gameMap[down.y][down.x] == WALL)
{
cout << "Walking Down Forbidden || WALL" << endl;
}
else if(gameEngine.gameMap[down.y][down.x] == FLOOR)
{
cout << "Walking Down || FLOOR" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[down.y][down.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = down.x;
pacmanPlayer.y = down.y;
}
else if (gameEngine.gameMap[down.y][down.x] == FOOD)
{
cout << "Walking Down || FOOD" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[down.y][down.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = down.x;
pacmanPlayer.y = down.y;
// Add Point to Pacman
pacmanPlayer.points += 1;
}
else if (gameEngine.gameMap[down.y][down.x] == POWER)
{
cout << "Walking Down || POWER" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[down.y][down.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = down.x;
pacmanPlayer.y = down.y;
// Set PacMan Invensible
pacmanPlayer.invencible = true;
}
break;
case 3:
if(gameEngine.gameMap[right.y][right.x] == WALL)
{
cout << "Walking Right Forbidden || WALL" << endl;
}
else if(gameEngine.gameMap[right.y][right.x] == FLOOR)
{
cout << "Walking Right || FLOOR" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[right.y][right.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = right.x;
pacmanPlayer.y = right.y;
}
else if (gameEngine.gameMap[right.y][right.x] == FOOD)
{
cout << "Walking Right || FOOD" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[right.y][right.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = right.x;
pacmanPlayer.y = right.y;
// Add Point to Pacman
pacmanPlayer.points += 1;
}
else if (gameEngine.gameMap[right.y][right.x] == POWER)
{
cout << "Walking Right || POWER" << endl;
// Change Map Object
gameEngine.gameMap[pacmanPlayer.y][pacmanPlayer.x] = FLOOR; // current position = floor
gameEngine.gameMap[right.y][right.x] = PACMAN; // next position = pacman
// Set New Pacman Position
pacmanPlayer.x = right.x;
pacmanPlayer.y = right.y;
// Set PacMan Invensible
pacmanPlayer.invencible = true;
}
break;
}
// PlaySound(TEXT("sounds/pacman-move.wav"), NULL, SND_ASYNC);
}
void clockFunction(int clock)
{
printf("## Clock %d ##\n", clock);
// Play Beginning Music
if(gameEngine.gameBeginning)
{
PlaySound(TEXT("sounds/beginning.wav"), NULL, SND_SYNC);
gameEngine.gameBeginning = false;
knockDownGameDoor();
}
// Move Ghost
int sortedGhost = randNumber(0, 3);
moveGhostRandomly(sortedGhost);
int clockTime = (1000.0 / GAMELEVEL);
glutTimerFunc(clockTime, clockFunction, clock + 1);
}
void knockDownGameDoor()
{
int doorRow = gameEngine.doorY;
int doorColumn = gameEngine.doorX;
int doorPositionObject = gameEngine.gameMap[doorRow][doorColumn];
if(doorPositionObject == DOOR)
{
gameEngine.gameMap[doorRow][doorColumn] = FLOOR;
}
else
{
cout << "ERROR::N�o foi poss�vel encontrar a porta do game!!" << endl;
}
}
void moveGhostRandomly(int ghostID)
{
// Check if Game Over or Beginning
if(gameEngine.gameOver || gameEngine.gameBeginning) return;
// Check if Ghost Exist
if(!ghostPlayer[ghostID].hasStarted) return;
// Variables
int ghostObjectId = ghostID + 5;
Coord lastGhostCoord = {ghostPlayer[ghostID].x, ghostPlayer[ghostID].y};
Coord nextMove[4] =
{
{ghostPlayer[ghostID].x, ghostPlayer[ghostID].y - 1},
{ghostPlayer[ghostID].x - 1, ghostPlayer[ghostID].y},
{ghostPlayer[ghostID].x, ghostPlayer[ghostID].y + 1},
{ghostPlayer[ghostID].x + 1, ghostPlayer[ghostID].y}
}; // 0 - North | 1 - Left | 2 - South | 3 - Left
int nextObject[4] =
{
gameEngine.gameMap[nextMove[0].y][nextMove[0].x],
gameEngine.gameMap[nextMove[1].y][nextMove[1].x],
gameEngine.gameMap[nextMove[2].y][nextMove[2].x],
gameEngine.gameMap[nextMove[3].y][nextMove[3].x]
}; // 0 - North | 1 - Left | 2 - South | 3 - Left
// Check If Is Prisoned
int possibleMoves = 0;
for(int i = 0; i < 4 ; i++)
{
if(nextObject[i] == FLOOR || nextObject[i] == FOOD || nextObject[i] == POWER || nextObject[i] == PACMAN)
possibleMoves += 1;
}
if(possibleMoves == 0)
{
cout << "Ghost " << ghostID << " is Prisoned!!" << endl;
return;
}
// If Freedom, Generate Valid Random Move
int randMove;
do
{
randMove = randNumber(0, 3);
}
while (
nextObject[randMove] == WALL ||
nextObject[randMove] == REDGHOST ||
nextObject[randMove] == ORANGEGHOST ||
nextObject[randMove] == PURPLEGHOST ||
nextObject[randMove] == BLUEGHOST
);
cout << "Ghost " << ghostObjectId << " Rand Move = " << randMove << " Next Object = " << nextObject[randMove] << endl;
cout << "Ghost Object Below = " << ghostPlayer[ghostID].objectBelowPlayer << " Next Object Below Ghost = " << gameEngine.gameMap[nextMove[randMove].y][nextMove[randMove].x] << endl;
cout << "Ghost Position: (" << lastGhostCoord.x << ", " << lastGhostCoord.y << ") New Ghost Position = " << nextMove[randMove].x << "-" << nextMove[randMove].y << endl;
// Update Next Ghost Location on Map and Ghost Coord Params
gameEngine.gameMap[nextMove[randMove].y][nextMove[randMove].x] = ghostObjectId;
gameEngine.gameMap[lastGhostCoord.y][lastGhostCoord.x] = ghostPlayer[ghostID].objectBelowPlayer;
ghostPlayer[ghostID].objectBelowPlayer = nextObject[randMove];
ghostPlayer[ghostID].x = nextMove[randMove].x;
ghostPlayer[ghostID].y = nextMove[randMove].y;
// Redisplay Objects In New Position
glutPostRedisplay();
}
void showGameMap()
{
cout << "########## GAME MAP ###########" << endl;
for(int i = 0; i < MAPSIZE; i++)
{
for(int j = 0; j < MAPSIZE; j++)
{
cout << gameEngine.gameMap[i][j] << " ";
}
cout << endl;
}
cout << "###############################" << endl;
}
int randNumber(int min, int max)
{
static bool first = true;
if (first)
{
srand( time(NULL) ); //seeding for the first time only!
first = false;
}
return min + rand() % (( max + 1 ) - min);
}