-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ghost.cpp
399 lines (294 loc) · 9.46 KB
/
Ghost.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
#include "Ghost.h"
/*----------------------------------------------------------------------------------------------*/
/* Function controls Ghosts movement
* Diverts the movement to the coresponding function based on the difficulty chosen by the player
* Pure virtual function, which is implemented in the Derived classes
*/
//virtual void Ghost::move(char board[][terminal_Size_X], GameObject pacmanPos, vector<Ghost> ghosts) {
//Delete(board);
//char direction = STAY;
//char directions[] = { UP,DOWN , LEFT , RIGHT };
//int randIndex;
//int playDumb;
//if (difficulty == BEST) // BEST
//{
// direction = aStar(board, pacmanPos, ghosts);
// this->dir = direction;
//}
//else if (difficulty == GOOD) {
// //moveMedium
// // generate a random number between 0 and 9 and if it a 7 then move to a random direction for 5 moves.
// if (moveCount < 20) {
// direction = this->dir;
// moveCount++;
// }
// else {
// playDumb = rand() % Medium_Dumb_Ghost_Rate;
// if (playDumb == 0) {
// randIndex = rand() % 4; // Generate a number between 0 to 3
// this->dir = directions[randIndex];
// moveCount = 15;
// direction = this->dir;
// }
// else
// direction = aStar(board, pacmanPos, ghosts);
// this->dir = direction;
// }
//}
//else { // C NOVICE
// //moveRandomly
// if (moveCount < 20)
// moveCount++;
// else {
// randIndex = rand() % 4; // Generate a number between 0 to 3
// this->dir = directions[randIndex];
// moveCount = 0;
// }
// direction = this->dir;
//}
//move(board, direction, ghosts);
//draw();
//}
/*----------------------------------------------------------------------------------------------*/
/* Function implements movement using the A* algorithm
* Take the board and the position of the pacman
* calculates a lovely path to the postion given and returns the direction as a char
*/
char Ghost::aStar(char board[][terminal_Size_X], GameObject end,vector<Ghost*> ghosts) {
int breakOutOfLoop = terminal_Size_X * 2; // in case player out of bounds limit to how many times loop runs
int breaker = 0; // init breaker for loop
int startEndDistance = pow((getX() - end.getX()), 2) + pow((getY() - end.getY()), 2);
PathNode *startNode = new PathNode(getPosition(), 0, startEndDistance); // in the ctor of PathNode, the default values of Parent == nullptr.
PathNode endNode(end, startEndDistance, 0); // in the ctor of PathNode, the default values of Parent == nullptr.
// Initialize both Open and Closed Lists:
vector<PathNode*> openList;
vector<PathNode*> closedList;
vector <PathNode*> children;
// Add the StartNode:
openList.push_back(startNode);
// Loop until you find the end:
while (openList.size() > 0) {
breaker++;
if (breaker == breakOutOfLoop) {
//clean memory allocations
while (!openList.empty()) {
PathNode* node = openList.back();
openList.pop_back();
delete node;
}
while (!closedList.empty()) {
PathNode* node = closedList.back();
closedList.pop_back();
delete node;
}
return moveApproc(end, board, ghosts);
}
// Get the current Node.
PathNode* currentNode = openList[0]; // Default copy ctor.
int index = 0;
for (int i = 0; i < openList.size(); i++) {
if ((*openList[i]).getf() < (*currentNode).getf()) {
index = i;
currentNode = openList[i];
}
}
// Pop Current off openList, and add to closedList:
openList.erase(openList.begin() + index);
closedList.push_back(currentNode);
// If found the end, return:
if (*currentNode == endNode) {
// return direction of the first move of the path:
PathNode* nextNode = currentNode;
PathNode* firstMoveNode = nullptr;
while (*nextNode != *startNode) {
firstMoveNode = nextNode;
nextNode = nextNode->getParent();
}
if (firstMoveNode == nullptr) //for the case of same location
return 's';
GameObject newPos = firstMoveNode->getPosition();
GameObject currentPos = (*startNode).getPosition();
while (!openList.empty()) {
PathNode* node = openList.back();
openList.pop_back();
delete node;
}
while (!closedList.empty()) {
PathNode* node = closedList.back();
closedList.pop_back();
delete node;
}
if (newPos.getX() - currentPos.getX() == 0 && newPos.getY() - currentPos.getY() == 1) {
return DOWN;
}
else if (newPos.getX() - currentPos.getX() == 0 && newPos.getY() - currentPos.getY() == -1) {
return UP;
}
else if (newPos.getX() - currentPos.getX() == 1 && newPos.getY() - currentPos.getY() == 0)
return RIGHT;
else if (newPos.getX() - currentPos.getX() == -1 && newPos.getY() - currentPos.getY() == 0)
return LEFT;
else
return STAY;
}
children.clear();
// temp is used to generate the children of the currentNode.
GameObject temp[4];
temp[0] = GameObject(0, 1); // Position above currentNode
temp[1] = GameObject(1, 0); // Position right of currentNode
temp[2] = GameObject(0, -1); // Position left of currentNode
temp[3] = GameObject(-1, 0); // Position below currentNode
for (int i = 0 ; i < 4; i++) {
// Get Node Position:
GameObject nodePosition(((*currentNode).getPosition() + temp[i]));
// Make Sure within Range of board & Not a Wall (walkable terrain):
if (!checkLegalMove(nodePosition, board, ghosts))
continue;
PathNode * newNode = new PathNode(nodePosition, 0, 0, currentNode);
children.push_back(newNode);
}
// Loop through Children:
for (auto& child : children) {
bool addChild = true;
// Child is on the closedList:
for (auto& closedChild : closedList) {
if (*child == *closedChild) {
addChild = false;
continue;
}
}
if (!addChild)
continue;
// Create the G H and F values:
(*child).setg(((*currentNode).getg() + 1));
(*child).seth(pow(((*child).getPosition().getX() - endNode.getPosition().getX()), 2) + pow(((*child).getPosition().getY() - endNode.getPosition().getY()), 2));
(*child).setf((*child).getg() + (*child).geth());
// Child is already in the openList:
for (auto& openNode : openList) {
if (*child == *openNode && (*child).getg() > (*openNode).getg()) {
addChild = false;
continue;
}
}
if (!addChild) continue;
openList.push_back(child);
}
}
return 's';
}
/*----------------------------------------------------------------------------------------------*/
char Ghost::moveApproc(GameObject end, char board[][terminal_Size_X],vector<Ghost*> ghosts) {
if (end.getX() > getX() && checkLegalMove(RIGHT, board, ghosts))
{
return RIGHT;
}
else if (end.getX() < getX() && checkLegalMove(LEFT, board, ghosts)) {
return LEFT;
}
else if (end.getY() < getY() && checkLegalMove(DOWN, board, ghosts)) {
return DOWN;
}
else if (end.getY() > getY() && checkLegalMove(UP, board, ghosts)) {
return UP;
}
else {
return STAY;
}
}
/*----------------------------------------------------------------------------------------------*/
/* Function controls Ghosts movement
* Generates a random direction and sets the Ghosts
* Location based on that direction UP up 'd' right etc..
*/
void Ghost::move(char board[][terminal_Size_X], char dir, vector<Ghost*> ghosts){
if (checkLegalMove(dir, board, ghosts)) {
if (dir == UP)
{
this->setY(getY() - 1);
}
else if (dir == DOWN) {
this->setY(getY() + 1);
}
else if (dir == RIGHT) {
this->setX(getX() + 1);
}
else if (dir == LEFT) {
this->setX(getX() - 1);
}
this->dir = dir;
}
}
/*----------------------------------------------------------------------------------------------*/
/* Function draws the ghost on the screen
* goes to the Ghost's coordinates and draws it
*/
void Ghost::draw() {
gotoxy(getX(), getY());
cout << '&';
}
/*----------------------------------------------------------------------------------------------*/
/* Hides the Ghost from the User
*/
void Ghost:: Delete(char board[][terminal_Size_X]) {
setTextColor(Color::LIGHTGREY);
gotoxy(getX(), getY());
cout << board[getY()][getX()];
}
/*----------------------------------------------------------------------------------------------*/
/* Function takes current coordinates of Ghost and direction and returns if it's valid
*/
bool Ghost::checkLegalMove(char direction, char board[][terminal_Size_X],vector<Ghost*> ghosts) {
int newX = this->getX();
int newY = this->getY();
if (direction == UP)
{
newY--;
}
else if (direction == DOWN)
{
newY++;
}
else if (direction == RIGHT)
{
newX++;
}
else if (direction == LEFT)
{
newX--;
}
for (auto g: ghosts)
{
if (g->getPosition() == GameObject(newX,newY))
{
return false;
}
}
if (board[newY][newX] == ' ' || board[newY][newX] == FOOD)
return true;
else
return false;
}
/*----------------------------------------------------------------------------------------------*/
/* Function takes current position of currentNode, and returns if it's valid
* This function helps Astar to find the path to the pacman.
*/
bool Ghost::checkLegalMove(GameObject pos, char board[][terminal_Size_X], vector<Ghost*> ghosts) {
pos.getX();
pos.getY();
for (auto g : ghosts)
{
if (g->getPosition() == GameObject(pos.getX(), pos.getY()))
{
return false;
}
}
if (board[pos.getY()][pos.getX()] == ' ' || board[pos.getY()][pos.getX()] == FOOD)
return true;
else
return false;
}
/*----------------------------------------------------------------------------------------------*/
char Ghost::getDirection() const {
return this->dir;
}
/*----------------------------------------------------------------------------------------------*/