-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoughnutModeGame.cpp
200 lines (154 loc) · 6.94 KB
/
DoughnutModeGame.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
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
// a. Michael Bertagna
// b. 2353491
// c. bertagna@chapman.edu
// d. CPSC 350-01
// e. Assignment 3
/* DoughnutModeGame.cpp contains the implementation of the DoughnutModeGame derived class which inherits from the Game abstract class.
It calls Game's constructors and destructor and implements three functions:
1) checkAboveNeighbors: checks number of neighbors above a cell in the grid
2) checkBelowNeighbors: checks number of neighbors below a cell in the grid
3) checkSideNeighbors: checks number of neighbors to the side of a cell in the grid
*/
#include "DoughnutModeGame.h"
// checks number of neighbors above a cell in the grid
int DoughnutModeGame::checkAboveNeighbors(int row, int col){
int numNeighbors = 0;
//check if cell in a top corner
if((col == m_currGrid->getWidth()) && (row == 1)){//top right corner
if(m_currGrid->isSpecifiedCellOccupied(m_currGrid->getHeight(), 1))//bottom left cell
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(m_currGrid->getHeight(), col))//cell "above"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(m_currGrid->getHeight(), col-1))//cell "above" and left
++numNeighbors;
}
else if((col == 1) && (row == 1)){//top left corner
if(m_currGrid->isSpecifiedCellOccupied(m_currGrid->getHeight(), m_currGrid->getWidth()))//bottom right cell
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(m_currGrid->getHeight(), col))//cell "above"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(m_currGrid->getHeight(), col+1))//cell "above" and right
++numNeighbors;
}
//check if cell in first row but not corner
else if(row == 1){
if(m_currGrid->isSpecifiedCellOccupied(m_currGrid->getHeight(), col-1))//cell "above" and left
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(m_currGrid->getHeight(), col))//cell "above"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(m_currGrid->getHeight(), col+1))//cell "above" and right
++numNeighbors;
}
//check if cell in first column
else if(col == 1){
if(m_currGrid->isSpecifiedCellOccupied(row-1, m_currGrid->getWidth()))//cell "above" and left
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row-1, col))//cell "above"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row-1, col+1))//cell "above" and right
++numNeighbors;
}
//check if cell in last column
else if(col == m_currGrid->getWidth()){
if(m_currGrid->isSpecifiedCellOccupied(row-1, col-1))//cell "above" and left
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row-1, col))//cell "above"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row-1, 1))//cell "above" and right
++numNeighbors;
}
//if not in first row
else{
if(m_currGrid->isSpecifiedCellOccupied(row-1, col))//check cell directly above target cell
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row-1, col+1))//check cell above and right of target cell
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row-1, col-1))//check cell above and left of target cell
++numNeighbors;
}
return numNeighbors;
}
// checks number of neighbors below a cell in the grid
int DoughnutModeGame::checkBelowNeighbors(int row, int col){
int numNeighbors = 0;
//check if cell in bottom corner
if((col == 1) && (row == m_currGrid->getHeight())){//bottom left corner
if(m_currGrid->isSpecifiedCellOccupied(1, m_currGrid->getWidth()))//top right cell
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(1, col))//cell "below"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(1, col+1))//cell "above" and right
++numNeighbors;
}
else if((col == m_currGrid->getWidth()) && (row == m_currGrid->getHeight())){//bottom right corner
if(m_currGrid->isSpecifiedCellOccupied(1, 1))//top right cell
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(1, col))//cell "below"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(1, col-1))//cell "below" and left
++numNeighbors;
}
//check if cell in last row but not corner
else if(row == m_currGrid->getHeight()){
if(m_currGrid->isSpecifiedCellOccupied(1, col+1))//cell "below" and right
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(1, col))//cell "below"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(1, col-1))//cell "below" and left
++numNeighbors;
}
//check if cell in first column
else if(col == 1){
if(m_currGrid->isSpecifiedCellOccupied(row+1, m_currGrid->getWidth()))//cell "below" and left
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row+1, col))//cell "below"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row+1, col+1))//cell "below" and right
++numNeighbors;
}
//check if cell in last column
else if(col == m_currGrid->getWidth()){
if(m_currGrid->isSpecifiedCellOccupied(row+1, col-1))//cell "below" and left
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row+1, col))//cell "below"
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row+1, 1))//cell "below" and right
++numNeighbors;
}
//if cell not in last row
else{
if(m_currGrid->isSpecifiedCellOccupied(row+1, col))//check cell directly below target cell
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row+1, col+1))//check cell below and right of target cell
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row+1, col-1))//check cell below and left of target cell
++numNeighbors;
}
return numNeighbors;
}
// checks number of neighbors to the side of a cell in the grid
int DoughnutModeGame::checkSideNeighbors(int row, int col){
int numNeighbors = 0;
//check if cell in first column
if(col == 1){
if(m_currGrid->isSpecifiedCellOccupied(row, m_currGrid->getWidth()))
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row, col+1))
++numNeighbors;
}
//check if cell in last column
else if(col == m_currGrid->getWidth()){
if(m_currGrid->isSpecifiedCellOccupied(row, col-1))
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row, 1))
++numNeighbors;
}
//if cell not in first or last column
else{
if(m_currGrid->isSpecifiedCellOccupied(row, col-1))
++numNeighbors;
if(m_currGrid->isSpecifiedCellOccupied(row, col+1))
++numNeighbors;
}
return numNeighbors;
}