-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghost.cpp
125 lines (119 loc) · 3.56 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
/*******************************************************************************
* Ghost Class Implementation *
* For Pacman Game (COMP 11 Project 1 | 2016 Spring) *
* Purpose: to move the ghost towards pacman and end game if it reaches pacman *
* written by: Michael Edegware *
* Date: 3/26/2016 *
* Joke: Q. How can a man go eight days without sleep? *
* A. He sleeps at night *
******************************************************************************/
#include <iostream>
#include <cmath>
using namespace std;
#include "termfuncs.h"
#include "constants.h"
#include "ghost.h"
/*
* Ghost constructor
* Purpose: to initialize private members
* Arguments: none (constructor)
* Return value: none (constructor)
*/
Ghost::Ghost() {
row = 0;
col = 0;
rowGhostMove = 0;
colGhostMove = 0;
}
/* set_location
* Purpose: for initializing location
* Arguments: integer
* Returns: None
*/
void Ghost::set_location(int r, int c) {
row = r;
col = c;
}
/*move
* Purpose: for implimenting the ghost movement
* Arguments: an array of strings and two integers
* Returns: true or false
*/
bool Ghost::move(char board[ROWS][COLS], int target_r, int target_c) {
rowGhostMove = target_r - row;
colGhostMove = target_c - col;
if (abs(rowGhostMove) >= abs(colGhostMove)) {
increase_row(board, rowGhostMove);
} else if (abs(colGhostMove) >= abs(rowGhostMove)) {
increase_col(board, colGhostMove);
} else {
return false;
}
// check if ghost reaches pacman;
if (row == target_r || col == target_c) {
return true;
}
return false;
}
/*
* place_on_board
* Purpose: to places Ghost on the board, at Ghost's location
* Arguments: 2D array of board
* Returns: None.
*/
void Ghost::place_on_board(char board[ROWS][COLS]) {
board[row][col] = GHOST;
}
/*
* is_at
* purpose: to return true if the ghost is at this row and col
* Arguments: two integers
* Returns: true or false
*/
bool Ghost::is_at(int r, int c) {
if (r == row && c == col) {
return true;
}
return false;
}
/* increase_row
* Purpose: to increase the ghost's col
* Arguments: 2D array of board and an integer
* Returns: None.
*/
void Ghost::increase_col(char board[ROWS][COLS], int colGhostMove) {
if (colGhostMove >= 0) {
if (board[row][col + 1] == GHOST) {
return;
} else {
col++;
}
} else if (colGhostMove <= 0) {
if (board[row][col - 1] == GHOST) {
return;
} else {
col--;
}
}
}
/*
* increase_row
* Purpose: to increase the ghost's row
* Arguments: 2D array of board and an integer
* Returns: None.
*/
void Ghost::increase_row(char board[ROWS][COLS], int rowGhostMove) {
if (rowGhostMove >= 0) {
if (board[row + 1][col] == GHOST) {
return;
} else {
row++;
}
} else if (rowGhostMove <= 0) {
if (board[row - 1][col] == GHOST) {
return;
} else {
row--;
}
}
}