-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghost.cpp
More file actions
252 lines (233 loc) · 6.64 KB
/
ghost.cpp
File metadata and controls
252 lines (233 loc) · 6.64 KB
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
#include "ghost.h"
#include "game.h"
#include <QDebug>
extern Game *game;
ghost::ghost(QPixmap &ghostName, int column, int gh_type) //takes the pixmap of the ghost (Inky/Pinky/Clyde) and assigns it to the current ghost and sets him at his correct position
{
if (gh_type% 3 == 0)
type = Slow;
else if (gh_type%3 == 1)
type = Medium;
else
type = Fast;
num =0;
stop = false;
ghostName= ghostName.scaledToWidth(30);
ghostName = ghostName.scaledToHeight(30);
setPixmap(ghostName);
col = column;
row = 14;
setPos(10+30*col, 20+30*row);
k="";
srand(time(NULL));
max = 0;
for (int i = 0; i < 31; i++)
for (int c = 0; c < 28; c++)
if (max < game->boardData[i][c])
max = game->boardData[i][c];
max++;
matrix = new int*[max];
for (int i = 0; i < max; i++)
matrix[i] = new int[max];
for (int i = 0; i < max; i++)
for (int c = 0; c < max; c++)
matrix[i][c] = 0;
for (int i = 0; i < max; i++)
{
int row, col;
for (int r = 0; r < 31; r++)
for (int c = 0; c < 28; c++)
{
if (i == game->boardData[r][c])
{
row = r;
col = c;
}
}
if (game->boardData[row - 1][col] != -1)
matrix[i][game->boardData[row - 1][col]] = 1;
if (game->boardData[row + 1][col] != -1)
matrix[i][game->boardData[row + 1][col]] = 1;
if (i == 136)
{
matrix[i][161] = 1;
}
else
if (game->boardData[row][col - 1] != -1)
matrix[i][game->boardData[row][col - 1]] = 1;
if (i == 161)
matrix[i][136] = 1;
else
if (game->boardData[row][col + 1] != -1)
matrix[i][game->boardData[row][col + 1]] = 1;
}
}
void ghost::setrow(int x)
{
row = x;
}
void ghost::setcolumn (int x)
{
col = x;
}
int ghost::getcolumn()
{
return col;
}
int ghost::getrow()
{
return row;
}
void ghost::moveRandomly() //changes the private variable k to up/down/right/left randomly
{
int startnode = game->boardData[row][col];
int goal = game->boardData[game->pacman->getrow()][game->pacman->getcolumn()];
vector<vector<int>> results = Dijkstra(startnode);
int next;
if (results[goal].size()>=2)
next = results[goal].at(results[goal].size()-2);
else
{
k="";
next = -10;
}
if (next == game->boardData[row-1][col])
k = "up";
if (next == game->boardData[row+1][col])
k = "down";
if (next == 161 && startnode == 136)
k = "left";
else
if (next == game->boardData[row][col-1])
k = "left";
if ((next == 136 && startnode == 161) || next == game->boardData[row][col+1])
k = "right";
}
void ghost::stopghost()
{
stop = true;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(moveghost()));
timer->setSingleShot(true);
timer->start(2500);
}
void ghost::moveghost()
{
stop = false;
delete timer;
timer = NULL;
}
void ghost::advance(int phase) //move everytime the signal timeout() is emitted
{ //makes a random direction and changes row and column according to it
if (!phase) return;
if (!(game->pacman->movement())&&!stop)
{
bool go = false;
if (type == Fast)
if (num%7 != 3)
go = true;
if (type == Medium)
if (num%7 ==0 || num%7==2 || num %7 ==4||num%7==6)
go = true;
if (type == Slow)
if (num%7 ==1 || num %7 == 3|| num%7 == 5)
go = true;
num++;
if (go)
{
moveRandomly();
if (k=="up" && game->boardData[row-1][col] != -1)
row--;
else if (k=="down" && game->boardData[row+1][col] != -1)
row++;
else if (k=="left")
{
if(game->boardData[row][col] == 136) //if ghost is at the is at left portal and wants to go left move him to the other end of the portal
{
col=27;
}else if(game->boardData[row][col-1] != -1)
{
col--;
}
}
else if (k== "right" )
{
if(game->boardData[row][col] == 161) //if ghost is at the is at right portal and wants to go right move him to the other end of the portal
{
col=0;
}else if(game->boardData[row][col+1] != -1)
{
col++;
}
}
setPos(10+30*col, 20+30*row);
}
}
}
vector<vector<int>> ghost::Dijkstra(int startNode)
{
int temp[max][max], costs[max], previous[max];
bool visited[max];
// temp
for (int i = 0; i < max; i++)
for (int j = 0; j < max; j++)
if (matrix[i][j] == 0)
temp[i][j] = Infinity;
else
temp[i][j] = matrix[i][j];
// costs, previous, and visited
for (int i = 0; i < max; i++)
{
costs[i] = temp[startNode][i];
previous[i] = startNode;
visited[i] = false;
}
// startNode
costs[startNode] = 0;
visited[startNode] = true;
int count = 1, nextNode, minimumCost;
while (count < max)
{
// Find the minimum cost in order to visit a node.
minimumCost = Infinity;
for (int i = 0; i < max; i++)
if ((costs[i] < minimumCost) && (visited[i] == false))
{
minimumCost = costs[i];
nextNode = i;
}
// Visit the node.
visited[nextNode] = true;
// Update the costs of the children of the visited node.
for (int i = 0; i < max; i++)
if ((minimumCost + temp[nextNode][i] < costs[i]) && (visited[i] == false))
{
costs[i] = minimumCost + temp[nextNode][i];
previous[i] = nextNode;
}
count++;
}
// Fill the paths.
int j;
vector<vector<int>> paths;
paths.resize(max);
for (int i = 0; i < max; i++)
{
paths[i].push_back(i);
if (i != startNode)
{
j = i;
do
{
j = previous[j];
paths[i].push_back(j);
} while (j != startNode);
}
}
return paths;
}/*
void ghost::getpac(Pacman* pac)
{
pacman = pac;
}
*/