-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ghost.cpp
280 lines (249 loc) · 6.2 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
#include "Ghost.h"
#include "Game.h"
void Ghost::move(array <array<bool, MAXCOLS>, MAXROWS>& food, array <array<bool, MAXCOLS>, MAXROWS>& walls,
char& level, const Point& pacman, vector <Ghost>& brotherGhosts)
{
switch (level)
{
case 'a': // BEST
BestMove(walls, food, body, pacman, brotherGhosts);
break;
case 'b': // GOOD
if (Game::getMoveCounter() % 20 == 0
|| Game::getMoveCounter() % 21 == 0
|| Game::getMoveCounter() % 22 == 0
|| Game::getMoveCounter() % 23 == 0
|| Game::getMoveCounter() % 24 == 0)
{
simpleMove(food, walls); // need to take a break.. execution of case 'c';
}
else
{
BestMove(walls, food, body, pacman, brotherGhosts);
}
break;
case 'c': // DUMB_ASS
simpleMove(food, walls);
break;
default:
break;
}
}
void Ghost::printGhost()const
{
if (Game::getSilentMode())
return;
if (Board::IsColorOn())
setTextColor(Color::RED);
body.draw(GHOST); // print new point
hideCursor();
//setTextColor(Color::WHITE);
}
void Ghost::BestMove(array <array<bool, MAXCOLS>, MAXROWS>& walls, array <array<bool, MAXCOLS>, MAXROWS>& food,
Point src, Point dest, vector <Ghost>& brotherGhosts)
{
Point saver = this->body;
if (isFoodInXY(food, body)) {
if (Board::IsColorOn())
setTextColor(Color::WHITE);
body.draw(FOOD); // create effect of a ghost flying over the food
hideCursor();
}
else
{
body.draw(' '); // delete last point
hideCursor();
}
if (Game::getMode() == GameMode::LOAD)
{
direction = (int)Steps.front();
Steps.pop_front();
body.moveWIthDirection(direction);
}
else // SaveMode
{
// Stores the smallest path
string path = "";
pathMoves(walls, src, dest, path);
switch (path[0])
{
case 'U':
direction = (int)Direction::UP;
break;
case 'D':
direction = (int)Direction::DOWN;
break;
case 'L':
direction = (int)Direction::LEFT;
break;
case 'R':
direction = (int)Direction::RIGHT;
break;
default:
break;
};
body.moveWIthDirection(direction);
// callision check;
for (unsigned int i = 0; i < brotherGhosts.size(); i++)
{
if (this->body == brotherGhosts[i].body && this != &brotherGhosts[i])
{
body = saver;
direction = (int)Direction::STAY;
body.moveWIthDirection(direction);
}
}
Steps.push_back(direction);
}
printGhost();
}
void Ghost::pathMoves(array <array<bool, MAXCOLS>, MAXROWS>& mat,Point src, Point dest, string& path)const
{
int dRow[] = { -1, 0, 0, 1 };
int dCol[] = { 0, -1, 1, 0 };
// Stores the distance for each
// cell from the source cell
int d[MAXROWS][MAXCOLS];
memset(d, -1, sizeof d);
// Distance of start cell is 0
d[src.GetY()][src.GetX()] = 0;
// Initialize a visited array to track the positions we are going to explore
bool visited[MAXROWS][MAXCOLS];
memset(visited, false, sizeof visited);
// Mark source cell as visited
visited[src.GetY()][src.GetX()] = true;
// Create a queue for BFS algoritem
queue<Node> q;
// Distance of source cell is 0
Node s = { src, 0 };
// Enqueue source cell
q.push(s);
// Keeps track of whether pacman has been reached or not.
bool missionAcomplished = false;
// Iterate until queue is not empty
while (!q.empty())
{
// Deque front of the queue
Node curr = q.front();
Point p = curr.pt;
// If the destination cell is reached, then find the path
if (p.GetX() == dest.GetX() && p.GetY() == dest.GetY())
{
int x = p.GetX(), y = p.GetY();
int dist = curr.dist;
// Assign the distance of destination to the distance matrix
d[p.GetY()][p.GetX()] = dist;
// Iterate until source is reached
while (x != src.GetX() || y != src.GetY())
{
// Append DOWN
if (y > 0 && d[y - 1][x] == dist - 1)
{
path += 'D';
y--;
}
// Append UP
if (y < MAXROWS - 1 && d[y + 1][x] == dist - 1)
{
path += 'U';
y++;
}
// Append RIGHT
if (x > 0 && d[y][x - 1] == dist - 1) {
path += 'R';
x--;
}
// Append LEFT
if (x < MAXCOLS - 1 && d[y][x + 1]== dist - 1)
{
path += 'L';
x++;
}
dist--;
}
// Reverse the backtracked path
reverse(path.begin(), path.end());
missionAcomplished = true;
break;
}
// Pop the start of queue
q.pop();
// Explore all adjacent directions
for (int i = 0; i < 4; i++)
{
int row = p.GetY() + dRow[i];
int col = p.GetX() + dCol[i];
// If the current cell is valid
// cell and can be traversed
if (isValid(row, col) && (mat[row][col] == false) && !visited[row][col])
{
// Mark the adjacent cells as visited
visited[row][col] = true;
// Enqueue the adjacent cells
Node adjCell = { { col, row }, curr.dist + 1 };
q.push(adjCell);
// Update the distance
// of the adjacent cells
d[row][col] = curr.dist + 1;
}
}
}
// If the destination is not reachable
if (!missionAcomplished)
return;
}
void Ghost::simpleMove(array <array<bool, MAXCOLS>, MAXROWS>& food, array <array<bool, MAXCOLS>, MAXROWS>& walls)
{
Point saver = body;
if (Game::getMode() == GameMode::LOAD)
{
direction = (int)Steps.front();
Steps.pop_front();
}
else
{
if (Game::getMoveCounter() % 20 == 0)
setRandDirection(); // random direction
}
if (isFoodInXY(food, body)) {
if (Board::IsColorOn())
setTextColor(Color::WHITE);
body.draw(FOOD); // create effect of a ghost flying over the food
hideCursor();
}
else
{
body.draw(' '); // delete last point
hideCursor();
}
body.moveWIthDirection(direction);
if (!IsWallInXY(walls, body))
{
printGhost();
}
else
{
body = saver;
printGhost();
}
if (Game::getMode() == GameMode::SAVE)
Steps.push_back(direction);
}
void Ghost::ReverseDirection()
{
switch (direction)
{
case (int)Direction::UP:
setDirection((int)Direction::DOWN);
break;
case (int)Direction::DOWN:
setDirection((int)Direction::UP);
break;
case (int)Direction::RIGHT:
setDirection((int)Direction::LEFT);
break;
case (int)Direction::LEFT:
setDirection((int)Direction::RIGHT);
break;
}
}