-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacman.cpp
More file actions
294 lines (272 loc) · 5.8 KB
/
Copy pathpacman.cpp
File metadata and controls
294 lines (272 loc) · 5.8 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
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
/* Name: Daniel Westrich
* Title: pacman.cpp
* Purpose: to define the pacman class
*/
#include <iostream>
#include "termfuncs.h"
#include "constants.h"
#include "pacman.h"
// Constructor
// Arguments: none
// Returns: none
Pacman::Pacman()
{
alive = true;
center();
num_moves = 0;
score = 0;
}
// Purpose: initializes pacman in the center of the board
// Arguments: none
// Returns: none
void Pacman::center()
{
row = ROWS/2;
col = COLS/2;
head = UP;
}
// Purpose: moves pacman
// Arguments: a 2D char board array, a char command
// Returns: bool
bool Pacman::move(char board[][COLS], char command)
{
bool dot_eaten = false;
board[row][col] = ' ';
go_lat(board, command);
go_vert(board, command);
wrap_around();
dot_eaten = check_if_eaten(board); // returns true if pacman eats a dot
num_moves++;
place_on_board(board);
return dot_eaten;
}
// Purpose: moves pacman laterally
// Arguments: a 2D char board array, a char command
// Returns: none
void Pacman::go_lat(char board[][COLS], char command)
{ // vert and lat were split so that the function would not exceed the limit
if(direction(command) == RIGHT){
head = RIGHT;
if(hits_boulder_lat(board, head) == false){
col++;
}
}
if(direction(command) == LEFT){
head = LEFT;
if(hits_boulder_lat(board, head) == false){
col--;
}
}
}
// Purpose: moves pacman vertically
// Arguments: a 2D char board array, a char command
// Returns: none
void Pacman::go_vert(char board[][COLS], char command)
{
if(direction(command) == UP){
head = UP;
if(hits_boulder_vert(board, head) == false){
row--;
}
}
if(direction(command) == DOWN){
head = DOWN;
if(hits_boulder_vert(board, head) == false){
row++;
}
}
}
// Purpose: to check if pacman is hitting a boulder
// Arguments: a 2d char board array, a char head
// Returns: bool
bool Pacman::hits_boulder_lat(char board[][COLS], char head)
{
if(head == RIGHT){
if(col == COLS-1 and board[row][0] == BOULDER){
return true;
} // if boulder prevents wrap around, return true
if(board[row][col+1] == BOULDER){
return true;
}else{
return false;
}
}
if(head == LEFT){
if(col == 0 and board[row][COLS-1] == BOULDER){
return true;
}
if(board[row][col-1] == BOULDER){
return true;
}else{
return false;
}
}else{
return false;
}
}
// Purpose: to check if pacman is hitting a boulder
// Arguments: a 2d char board array, a char head
// Returns: bool
bool Pacman::hits_boulder_vert(char board[][COLS], char head)
{ // vert and lat were split so that the function would not exced the limit
if(head == UP){
if(row == 0 and board[ROWS-1][col] == BOULDER){
return true;
}
if(board[row-1][col] == BOULDER){
return true;
}else{
return false;
}
}
if(head == DOWN){
if(row == ROWS-1 and board[0][col] == BOULDER){
return true;
}
if(board[row+1][col] == BOULDER){
return true;
}else{
return false;
}
}else{
return false;
}
}
// Purpose: to wrap around the board
// Arguments: none
// Returns: none
void Pacman::wrap_around()
{
if(row < 0){
row = ROWS-1;
}
if(row > ROWS-1){
row = 0;
}
if(col < 0){
col = COLS-1;
}
if(col > COLS-1){
col = 0;
}
}
// Purpose: to decide the direction in which pacman points
// Arguments: a char command
// Returns: char
char Pacman::direction(char command)
{
if(command == CMD_UP){
return UP;
}
if(command == CMD_DWN){
return DOWN;
}
if(command == CMD_RGT){
return RIGHT;
}
if(command == CMD_LFT){
return LEFT;
}else{
return 1;
}
}
// Purpose: to place pacman on the board
// Arguments: a 2d char board array
// Returns: none
void Pacman::place_on_board(char board[][COLS])
{
if(board[row][col] != GHOST){
board[row][col] = head;
}
}
// Purpose: to get the number of moves made
// Arguments: none
// Returns: int
int Pacman::get_num_moves()
{
return num_moves;
}
// Purpose: to set the col of pacman
// Arguments: an int i
// Returns: none
void Pacman::set_col(int i)
{
col = i;
}
// Purpose: to set the row of pacman
// Arguments: an int i
// Returns: none
void Pacman::set_row(int i)
{
row = i;
}
// Purpose: to get pacman's col
// Arguments: none
// Returns: int
int Pacman::get_col()
{
return col;
}
// Purpose: to get pacman's row
// Arguments: none
// Returns: int
int Pacman::get_row()
{
return row;
}
// Purpose: to get pacman's head ;)
// Arguments: none
// Returns: char
char Pacman::get_head()
{
return head;
}
// Purpose: to say whether pacman is at a specific location
// Arguments: an int r, an int c
// Returns: bool
bool Pacman::is_at(int r, int c)
{
if(r == row and c == col){
return true;
}else{
return false;
}
}
// Purpose: to add to pacman's score
// Arguments: an int n
// Returns: none
void Pacman::add_to_score(int n)
{
score += n;
}
// Purpose: to get pacman's score
// Arguments: none
// Returns: int
int Pacman::get_score()
{
return score;
}
// Purpose: to return whether or not pacman is alive
// Arguments: none
// Returns: bool
bool Pacman::is_alive()
{
return alive;
}
// Purpose: to check if pacman has eaten a dot
// Arguments: a 2D char board array
// Returns: bool
bool Pacman::check_if_eaten(char board[][COLS])
{
if(board[row][col] == DOT){
return true;
}
return false;
}
// Purpose: to delete the previous pacman character from the board
// Arguments: a 2D char board array
// Returns: none
void Pacman::clear_pacman(char board[][COLS])
{
board[row][col] = ' ';
}