-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghost.cpp
159 lines (147 loc) · 2.98 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
#include "ghost.h"
#include <cstdlib>
#include <ncurses.h>
int Ghost::decide(int yPacLoc, int xPacLoc, int direction) {
switch(rand() % 4) {
case 0:
if (prevDecision == KEY_DOWN)
break;
return KEY_UP;
break;
case 1:
if (prevDecision == KEY_UP)
break;
return KEY_DOWN;
break;
case 2:
if (prevDecision == KEY_RIGHT)
break;
return KEY_LEFT;
break;
case 3:
if (prevDecision == KEY_LEFT)
break;
return KEY_RIGHT;
break;
default:
break;
}
}
bool Ghost::update(int newDirection) {
int oldDirection;
if (direction != newDirection && ((newDirection == KEY_UP && !checkSpawnGate()) || newDirection == KEY_DOWN || newDirection == KEY_LEFT || newDirection == KEY_RIGHT)) {
oldDirection = direction;
direction = newDirection;
if (checkCollision(getNextY(), getNextX()))
direction = oldDirection;
}
if(checkStatus())
return true;
switch(direction) {
case KEY_UP:
moveUp();
break;
case KEY_DOWN:
moveDown();
break;
case KEY_LEFT:
moveLeft();
break;
case KEY_RIGHT:
moveRight();
break;
default:
break;
}
prevDecision = direction;
return false;
}
void Ghost::moveUp() {
if (!checkCollision(yLoc - 1, xLoc)) {
mvwaddch(window, yTailLoc, xTailLoc, tailChar);
if (mvwinch(window, yLoc - 1, xLoc) != pac) {
tailChar = mvwinch(window, yLoc - 1, xLoc);
} else {
tailChar = ' ';
}
yLoc--;
yTailLoc = yLoc;
xTailLoc = xLoc;
}
}
void Ghost::moveDown() {
if (!checkCollision(yLoc + 1, xLoc)) {
mvwaddch(window, yTailLoc, xTailLoc, tailChar);
if (mvwinch(window, yLoc + 1, xLoc) != pac) {
tailChar = mvwinch(window, yLoc + 1, xLoc);
} else {
tailChar = ' ';
}
yLoc++;
yTailLoc = yLoc;
xTailLoc = xLoc;
}
}
void Ghost::moveLeft() {
if (!checkCollision(yLoc, xLoc - 1) && !(yLoc == 10 && xLoc - 1 == 4)) {
mvwaddch(window, yTailLoc, xTailLoc, tailChar);
if (mvwinch(window, yLoc, xLoc - 1) != pac) {
tailChar = mvwinch(window, yLoc, xLoc - 1);
} else {
tailChar = ' ';
}
xLoc--;
yTailLoc = yLoc;
xTailLoc = xLoc;
}
}
void Ghost::moveRight() {
if (!checkCollision(yLoc, xLoc + 1) && !(yLoc == 10 && xLoc + 1 == 18)) {
mvwaddch(window, yTailLoc, xTailLoc, tailChar);
if (mvwinch(window, yLoc, xLoc + 1) != pac) {
tailChar = mvwinch(window, yLoc, xLoc + 1);
} else {
tailChar = ' ';
}
xLoc++;
yTailLoc = yLoc;
xTailLoc = xLoc;
}
}
bool Ghost::checkStatus() {
if (!hunter && checkEnemyCollision(pac, getNextY(), getNextX())) {
if (tailChar == '.')
tailChar = ' ';
yTailLoc = ySpawnLoc;
xTailLoc = xSpawnLoc;
revive();
return true;
}
}
bool Ghost::checkSpawnGate() {
if (mvwinch(window, yLoc - 1, xLoc) == gate) {
return true;
}
return false;
}
int Ghost::getAvailableDirection() {
switch(rand() % 4) {
case 0:
return KEY_UP;
break;
case 1:
return KEY_DOWN;
break;
case 2:
return KEY_LEFT;
break;
case 3:
return KEY_RIGHT;
break;
default:
break;
}
}
void Ghost::setRole(bool hunt) {
hunter = hunt;
}