File size: 7,015 Bytes
0ef7a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*

Name:Wong Pui Shan

Sdutent ID:52611804

program: AScISD

Name: HAR Chiu Kwong Samson

Sdutent ID:52629360

program: AScISD

Name: LAM Cheuk Man 

Sdutent ID:52621140

program: AScISD

Name:KO Jeffrey KO

Sdutent ID:525 695 30

program: AScISD



*/
#include <iostream>
#include <fstream>
#include <string>
#include "Utilities.h"
#include "Game.h"
#include "Ghost.h"
#include "Pacman.h"
#include "Maze.h"
#include <climits>
#include <ctime>
using namespace std;
Ghost::Ghost(){
	ax = 1;
	ay = 1;
	direction = 5;
	d = 0;
	g = "A";
	OrgX = 0;
	OrgY = 0;
	limit = 0;
	speed = 0;
	reduceLimit = 100;
}

Ghost::Ghost(int x, int y, string GhostG)
{
	ax = x;
	ay = y;
	direction = 5;
	d = 0;
	g = GhostG;
	OrgX = x;
	OrgY = y;
	limit = 0;
	reduceLimit = 100;
}
void Ghost::increaseSpeed(int &dot){
	if(limit >60){
		if(dot == 20)
			reduceLimit = 80;
		if(dot == 40)
			reduceLimit = 90;
		if(dot == 60)
			reduceLimit = 70;
	}
}
void Ghost::ChangePosition()

{
	if(g == "A")
		g = "a";
	else if(g == "B")
		g = "b";
	else if(g == "C")
		g = "c";
	else if(g == "D")
		g = "d";

}

void Ghost::showGhost(Utilities a)

{
	//Set up the color of the ghosts.
	a.gotoXY(ax, ay);
	if (g == "A")
		a.changeColour(FOREGROUND_RED);
		if (g == "B")
		a.changeColour(0x000d);
			if (g == "C")
		a.changeColour(FOREGROUND_GREEN);
				if (g == "D")
		a.changeColour(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
	if (g == "a" || g == "b" || g == "c" || g == "d")
		a.changeColour(BACKGROUND_WHITE | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
	cout << g;
}

void Ghost::Move(Maze &m, Utilities a)

{
	//Set up the speed of the ghost
	if (limit > 0)
	{
		--limit;
		return;
	}
	limit = reduceLimit;
	//Avoid the ghost to eat the dot, power dot and the fruit on the map
	a.gotoXY(ax,ay);
	a.changeColour(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		if (m.getMazeArray(ax, ay)=='X')
			{
				m.setMazeArray(ax, ay,'X');
				cout << "X";
			}
		else if (m.getMazeArray(ax, ay)=='.')
			{
				m.setMazeArray(ax, ay,'.');
				cout << ".";
			}
		else if (m.getMazeArray(ax, ay)==' ')
			{
				m.setMazeArray(ax, ay,' ');
				cout << " ";
			} 
		a.gotoXY(ax,ay);
		a.changeColour(FOREGROUND_GREEN | FOREGROUND_BLUE);
		if (m.getMazeArray(ax, ay)=='!')
			{
				m.setMazeArray(ax, ay,'!');
				cout << "!";
			} 
				else if (m.getMazeArray(ax, ay)=='*')
			{
				m.setMazeArray(ax, ay,'*');
				cout << "*";
			} 
				else if (m.getMazeArray(ax, ay)=='#')
			{
				m.setMazeArray(ax, ay,'#');
				cout << "#";
			} 
			
		
		do{
			// loop for granerate ghost's direction
			do{
				d = rand() % 4 + 1;
				if (m.getMazeArray(ax, ay-1)!='-' && m.getMazeArray(ax, ay-1)!='|' && d == 1)
					break;
				else if (m.getMazeArray(ax, ay+1)!='-' && m.getMazeArray(ax, ay+1)!='|'&& d == 4)
					break;
				else if (m.getMazeArray(ax-1, ay)!='-' && m.getMazeArray(ax-1, ay)!='|'&& d == 2)
					break;
				else if (m.getMazeArray(ax+1, ay)!='-' && m.getMazeArray(ax+1, ay)!='|'&& d == 3)
					break;
			}while (true);
		}while (d == 5 - direction ||
				(d == 4 && (ax==8 && ay==5))||
				(d == 4 && (ax==9 && ay==5))||
				(d == 4 && (ax==10 && ay==5))||		//It is used for avoid go oppostie direction and go to ghost home 
				(d == 4 && (ax==11 && ay==5))||
				(d == 4 && (ax==12 && ay==5)));

		//change the diection 

		switch(d){
		case 1:
			ay--;
			direction = 1;
			break;
			
		case 4: // Down arrow
			ay++;
			direction = 4;
			break;

		case 2: // Left arrow
			ax--;
			direction = 2;
			break;

		case 3: // Right arrow
			ax++;
			direction = 3;
			break;
		}
		a.gotoXY(ax,ay);
		cout << g;
			
	
}
//if the pacman eat the power dot, the status od the ghost change.
void Ghost::slowMove(Maze &m, Utilities a)

{
	//reduce the speed of the ghosts 
	if (speed > 0)
	{
		--speed;
		return;
	}
	speed = 200;

	//set up the color of the ghosts
	a.gotoXY(ax,ay);
	a.changeColour(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		if (m.getMazeArray(ax, ay)=='X')
			{
				m.setMazeArray(ax, ay,'X');
				cout << "X";
			}
		else if (m.getMazeArray(ax, ay)=='.')
			{
				m.setMazeArray(ax, ay,'.');
				cout << ".";
			}
		else if (m.getMazeArray(ax, ay)==' ')
			{
				m.setMazeArray(ax, ay,' ');
				cout << " ";
			} 
		
				else if (m.getMazeArray(ax, ay)=='!')
			{
				m.setMazeArray(ax, ay,'!');
				cout << "!";
			} 
				else if (m.getMazeArray(ax, ay)=='*')
			{
				m.setMazeArray(ax, ay,'*');
				cout << "*";
			} 
				else if (m.getMazeArray(ax, ay)=='#')
			{
				m.setMazeArray(ax, ay,'#');
				cout << "#";
			} 
			
		
		do{
			// loop for granerate ghost's direction
			do{
				d = rand() % 4 + 1;
				if (m.getMazeArray(ax, ay-1)!='-' && m.getMazeArray(ax, ay-1)!='|' && d == 1)
					break;
				else if (m.getMazeArray(ax, ay+1)!='-' && m.getMazeArray(ax, ay+1)!='|'&& d == 4)
					break;
				else if (m.getMazeArray(ax-1, ay)!='-' && m.getMazeArray(ax-1, ay)!='|'&& d == 2)
					break;
				else if (m.getMazeArray(ax+1, ay)!='-' && m.getMazeArray(ax+1, ay)!='|'&& d == 3)
					break;
			}while (true);
		}while (d == 5 - direction ||
				(d == 4 && (ax==8 && ay==5))||
				(d == 4 && (ax==9 && ay==5))||
				(d == 4 && (ax==10 && ay==5))||		//It is used for avoid go oppostie direction and go to ghost home 
				(d == 4 && (ax==11 && ay==5))||
				(d == 4 && (ax==12 && ay==5)));

		//change the diection 

		switch(d){
		case 1:
			ay--;
			direction = 1;
			break;
			
		case 4: // Down arrow
			ay++;
			direction = 4;
			break;

		case 2: // Left arrow
			ax--;
			direction = 2;
			break;

		case 3: // Right arrow
			ax++;
			direction = 3;
			break;
		}
		a.gotoXY(ax,ay);
		cout << g;
			
	
}


void Ghost::EatPacman(Pacman &p, Game &g, Utilities &a,Utilities &u,Utilities &j, Utilities &k, Utilities &l, Ghost &o, Ghost &m, Ghost &n)

{
	//if ghost met pacman, pacman life reduce and the location of ghosts and Pacman reset 
	if (ax == p.getPacmanX() && ay == p.getPacmanY())
	{
		g.life--;
		Ghost::ResetGhost(a);
		n.ResetGhost(j);
		m.ResetGhost(k);
		o.ResetGhost(l);
		p.Die(u);
	}
}

void Ghost::ResetGhost(Utilities &a){
	a.gotoXY(ax, ay);
	a.changeColour(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
	cout << " ";
	Ghost::ResetLocation();
	a.gotoXY(ax, ay);
	a.changeColour(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
	cout << g;
}
void Ghost::ResetLocation(){
	//save the original location of ghost
	ax = OrgX; 
	ay = OrgY;
}
void Ghost::RechangePosition(){
	//change the status of ghost to nomal  
	if(g == "a")
		g = "A";
	else if(g == "b")
		g = "B";
	else if(g == "c")
		g = "C";
	else if(g == "d")
		g = "D";
}
int Ghost::GetGhostX(){
	return ax;
}
int Ghost::GetGhostY(){
	return ay;
}
string Ghost::GetGhostMember(){
	return g;
}
Ghost::~Ghost(){

}