-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
254 lines (222 loc) · 8.8 KB
/
main.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
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
/******************************************************************************
** Author: Winter 162 400 Group 4
** Date: 02/01/2019
** Description: This program runs a Predator-Prey Game with "Doodlebug"
** predators and "Ant" prey.
******************************************************************************/
#include "Group4Utils.hpp"
#include "Critter.hpp"
#include "Doodlebug.hpp"
#include "Ant.hpp"
#include "menuFunctions.hpp"
#include <iostream>
#include <string>
using std::cout;
//Prototype functions for displaying moving, breeding, and starving Critter functions
void moveCritters(Critter ***, int, int, char);
void resetCritterMoveSuccess(Critter ***, int, int);
void breedCritters(Critter ***, int, int, char);
void starveCritters(Critter ***, int, int, char);
int main()
{
int playGame;
int timeSteps;
int size_x = 20;
int size_y = 20;
int numAnts = 100;
int numDoodbugs = 5;
Critter ***board;
/*********************************************************************************
** This area serves as the menu for the user to configure the ants, doodlebugs,
** the board, and the number of steps the program should take.
*********************************************************************************/
//Display the main menu
dispMainMenu();
//Make menu choice (input validated to ensure only a 1 or 2 is selected)
playGame = validSelection();
if(playGame == 1) //Play the game
{
queryNumSteps(); //Ask the user for the number of steps
timeSteps = validInt(); //Validate it's a non-negative integer
}
else //quit game
{
return 0;
}
/*********************************************************************************
** This area holds the creation of the board, along with its accompanying Ants
** and Doodlebugs.
*********************************************************************************/
//Initialize the board
board = new Critter**[size_x];
for(int i = 0; i < size_x; i++)
{
board[i] = new Critter*[size_y];
for(int j = 0; j < size_y; j++)
{
board[i][j] = NULL;
}
}
//Counters for the number of each Critter being placed.
int doodCount = 0;
int antCount = 0;
//Place Doodlebugs on the board
placeDoodles(board, doodCount, numDoodbugs, size_x, size_y);
//Place Ants on the board
placeAnts(board, antCount, numAnts, size_x, size_y);
//Print starting board
printBoard(board, size_x, size_y);
/*********************************************************************************
** This area contains all the simulated movement/feeding, breeding, and starving
** the Ants and the Doodlebugs will take part in.
*********************************************************************************/
// loop through the simulation until the user decides to end the simulation
while (timeSteps != 0)
{
/*************************************
** MOVE THE DOODLEBUGS
*************************************/
moveCritters(board, size_x, size_y, 'X');
resetCritterMoveSuccess(board, size_x, size_y);
/*************************************
** MOVE THE ANTS
*************************************/
moveCritters(board, size_x, size_y, 'O');
resetCritterMoveSuccess(board, size_x, size_y);
/*************************************
** STARVE THE DOODLEBUGS
*************************************/
starveCritters(board, size_x, size_y, 'X');
/*************************************
** BREED THE CRITTERS
*************************************/
breedCritters(board, size_x, size_y, 'X');
breedCritters(board, size_x, size_y, 'O');
//Print board
printBoard(board, size_x, size_y);
// decrease the number of steps
timeSteps--;
// if the program is about to end, ask the user to enter more steps or quit
if (timeSteps == 0)
{
queryContinue();
timeSteps = validNonNegative();
}
}
/*********************************************************************************
** This area deletes the dynamically allocated Critter array
*********************************************************************************/
for(int i = 0; i < size_x; i++)
{
for(int j = 0; j < size_y; j++)
{
delete board[i][j];
}
delete[] board[i];
}
delete[] board;
board = NULL;
return 0;
}
/*********************************************************************************
** moveCritters(Critter***, int, int, char)
** Description: The moveCritters function iterates through the game board, checking
** to see if there is a pointer to a Critter at each entry and that
** the Critter symbol matches the one inputted. If these conditions
** are met and the Critter has not moved yet, the move function is
** called for that Critter.
*********************************************************************************/
void moveCritters(Critter *** board, int size_x, int size_y, char symbol)
{
//Iterate through the game board
for(int i=0; i<size_y; i++)
{
for(int j=0; j<size_x; j++)
{
/*Check if there is a Critter and
that the Critter symbol matches the input symbol */
if(board[i][j] != NULL && board[i][j]->getSymbol() == symbol)
{
//Check to see if the Critter has moved yet
if(board[i][j]->getMoveSuccess() == 0)
{
//Run the move function for the Critter
board[i][j]->move(board, size_x, size_y);
}
}
}
}
}
/*********************************************************************************
** resetCritterMoveSuccess(Critter***, int, int)
** Description: The resetCritterMoveSuccess function iterates through the game
** board, and resets the move success for use in the next time step.
*********************************************************************************/
void resetCritterMoveSuccess(Critter *** board, int size_x, int size_y)
{
//Iterate through the board
for(int i=0; i<size_y; i++)
{
for(int j=0; j<size_x; j++)
{
//Check if there is a pointer to a Critter
if(board[i][j] != NULL)
{
//Reset moveSuccess for next time step
board[i][j]->resetMoveSuccess();
}
}
}
}
/*********************************************************************************
** breedCritter(Critter***, int, int, char)
** Description: The breedCritter function iterates through the game board, checking
** to see if there is a pointer to a Critter at each entry and that
** the Critter symbl matches the one inputted. If these conditions
** are met, then the breed function is called for that Critter.
*********************************************************************************/
void breedCritters(Critter *** board, int size_x, int size_y, char symbol)
{
//Iterate through the game board
for(int i=0; i<size_y; i++)
{
for(int j=0; j<size_x; j++)
{
/*Check if there is a Critter and
that the Critter symbol matches the input symbol */
if(board[i][j] != NULL && board[i][j]->getSymbol() == symbol)
{
//Run the breed function
board[i][j]->breed(board, size_x, size_y);
}
}
}
}
/*********************************************************************************
** starveCritter(Critter***, int, int, char)
** Description: The starveCritter function iterates through the game board, checking
** to see if there is a pointer to a Critter at each entry and that
** the Critter symbl matches the one inputted. If these conditions
** are met, then the starve function is called for that Critter.
*********************************************************************************/
void starveCritters(Critter *** board, int size_x, int size_y, char symbol)
{
//Iterate through game board
for(int i=0; i<size_y; i++)
{
for(int j=0; j<size_x; j++)
{
/*Check if there is a Critter and
that the Critter symbol matches the input symbol */
if(board[i][j] != NULL && board[i][j]->getSymbol() == symbol)
{
//If the Critter starved, remove it from the board
if(board[i][j]->starve(board))
{
delete board[i][j];
board[i][j] = NULL;
}
}
}
}
}