-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoodlebug.cpp
More file actions
186 lines (158 loc) · 7.58 KB
/
Doodlebug.cpp
File metadata and controls
186 lines (158 loc) · 7.58 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
/* Hunter Trautz, hctrautz
Gabe Aponte, gaaponte
PA5, Polymorphism
Doodlebug.cpp */
#include "Organism.h"
#include "Doodlebug.h"
#include <cstddef>
#include <iostream>
using namespace std;
/** Checks the area surronding the doodlebug, and if there is an Ant near eat it (if there is more than one eat a random one),
* otherwise move normally to a random empty position
* @Param **board array of pointers to organisms representing the gameboard
* @Param size the size of the gameboard, used to make sure we don't go off the end of the gameboard
* @Param stats stores the statistics of the simulation so that they can be printed at the end
* @Return void, moves the doodlebug
*/
void Doodlebug::move(Organism ***board, int size, Statistics *stats){
vector<int> canEatRow;
vector<int> canEatColumn;
//checks above the doodlebug and if there is an Ant add the coordinates of the cell to the vectors
if(this->row-1 >= 0 && board[this->row-1][this->column] != NULL && board[this->row-1][this->column]->type == Prey){
canEatRow.push_back(this->row-1);
canEatColumn.push_back(this->column);
}
//checks to the right of the doodlebug and if there is an Ant add the coordinates of the cell to the vectors
if(this->column+1 < size && board[this->row][this->column+1] != NULL && board[this->row][this->column+1]->type == Prey){
canEatRow.push_back(this->row);
canEatColumn.push_back(this->column+1);
}
//checks to the left of the doodlebug and if there is an Ant add the coordinates of the cell to the vectors
if(this->column-1 >= 0 && board[this->row][this->column-1] != NULL && board[this->row][this->column-1]->type == Prey){
canEatRow.push_back(this->row);
canEatColumn.push_back(this->column-1);
}
//checks below the doodlebug and if there is an Ant add the coordinates of the cell to the vectors
if(this->row+1 < size && board[this->row+1][this->column] != NULL && board[this->row+1][this->column]->type == Prey){
canEatRow.push_back(this->row+1);
canEatColumn.push_back(this->column);
}
//if there is an Ant that the doodlebug can eat, eat it
if(canEatRow.size() > 0){
//generate a random Ant to eat from the vectors
int randomEatIndex = rand()%canEatRow.size();
int randomEatRow = canEatRow[randomEatIndex];
int randomEatColumn = canEatColumn[randomEatIndex];
//cast board variable of type Organism to Ant
Ant *antEat = (Ant*)board[randomEatRow][randomEatColumn];
this->eat(antEat, board);
} else /* otherwise move normally */ {
vector<int> emptyRowNumbers;
vector<int> emptyColumnNumbers;
//checks above the doodlebug and if it's empty add the coordinates of the cell to the vectors
if(this->row-1 >= 0 && board[this->row-1][this->column] == NULL){
emptyRowNumbers.push_back(this->row-1);
emptyColumnNumbers.push_back(this->column);
}
//checks to the right of the doodlebug and if it's empty add the coordinates of the cell to the vectors
if(this->column+1 < size && board[this->row][this->column+1] == NULL){
emptyRowNumbers.push_back(this->row);
emptyColumnNumbers.push_back(this->column+1);
}
//checks below the doodlebug and if it's empty add the coordinates of the cell to the vectors
if(this->row+1 < size && board[this->row+1][this->column] == NULL){
emptyRowNumbers.push_back(this->row+1);
emptyColumnNumbers.push_back(this->column);
}
//checks to the left the doodlebug and if it's empty add the coordinates of the cell to the vectors
if(this->column-1 >= 0 && board[this->row][this->column-1] == NULL){
emptyRowNumbers.push_back(this->row);
emptyColumnNumbers.push_back(this->column-1);
}
//if the doodlebug can't move terminate the function
if(emptyRowNumbers.size() == 0){
return;
}
//generate a random position to move to from the vectors
int randomMoveIndex = rand()%emptyRowNumbers.size();
int randomMoveRow = emptyRowNumbers[randomMoveIndex];
int randomMoveColumn = emptyColumnNumbers[randomMoveIndex];
Doodlebug* newBug = new Doodlebug(this->turnCounter+1, this->type, randomMoveRow, randomMoveColumn, this->turnsSinceEating+1, true);
//place the new doodlebug on the board
board[randomMoveRow][randomMoveColumn] = newBug;
//remove it from its old position
board[this->row][this->column] = NULL;
//if the doodlebug meets the condition for starving run starve() on it and update the stats
if(newBug->turnsSinceEating > 2){
newBug->starve(board);
}
//if the doodlebug meets the condition for breeding run breed() on it and update the stats
if(newBug->turnCounter > 7){
newBug->breed(board, size);
newBug->turnCounter = 0;
stats->doodlebugTotal = stats->doodlebugTotal+1;
}
}
}
/** Replaces the cell containg an Ant with a Doodlebug
* @Param stats stores the statistics of the simulation so that they can be printed at the end
* @Param **board array of pointers to organisms representing the gameboard
* @Return void, Replaces the cell containg an Ant with a Doodlebug
*/
void Doodlebug::eat(Ant *ant, Organism ***board){
int antRow = ant->row;
int antColumn = ant->column;
//new doodlebug containg the same fields as the ant the function was called on
Doodlebug *temp = new Doodlebug(this->turnCounter+1, this->type, antRow, antColumn, 0, true);
//replaces the cell containg the ant with the new doodlebug
board[antRow][antColumn] = temp;
//removes the doodlebug from its old location
board[this->row][this->column] = NULL;
}
/** Checks the area surronding the doodlebug, and if there is an empty cell around the doodlebug, it will create a new doodlebug in a random empty space
* @Param **board array of pointers to organisms representing the gameboard
* @Param size the size of the gameboard, used to make sure we don't go off the end of the gameboard
* @Return adds a new Doodlebug to the board around the already exisiting doodlebug
*/
void Doodlebug::breed(Organism ***board, int size){
vector<int> emptyRowNumbers;
vector<int> emptyColumnNumbers;
//checks above the doodlebug and if it's empty add the coordinates of the cell to the vectors
if(this->row-1 >= 0 && board[this->row-1][this->column] == NULL){
emptyRowNumbers.push_back(this->row-1);
emptyColumnNumbers.push_back(this->column);
}
//checks to the right the doodlebug and if it's empty add the coordinates of the cell to the vectors
if(this->column+1 < size && board[this->row][this->column+1] == NULL){
emptyRowNumbers.push_back(this->row);
emptyColumnNumbers.push_back(this->column+1);
}
//checks below the doodlebug and if it's empty add the coordinates of the cell to the vectors
if(this->row+1 < size && board[this->row+1][this->column] == NULL){
emptyRowNumbers.push_back(this->row+1);
emptyColumnNumbers.push_back(this->column);
}
//checks to the left the doodlebug and if it's empty add the coordinates of the cell to the vectors
if(this->column-1 >= 0 && board[this->row][this->column-1] == NULL){
emptyRowNumbers.push_back(this->row);
emptyColumnNumbers.push_back(this->column-1);
}
//if the doodlebug can't move terminate
if(emptyRowNumbers.size() == 0){
return;
}
//generate a random position to place the new doodlebug from the vectors
int randomIndex = rand()%emptyRowNumbers.size();
int randomRow = emptyRowNumbers[randomIndex];
int randomColumn = emptyColumnNumbers[randomIndex];
Doodlebug* newBug = new Doodlebug(0, this->type, randomRow, randomColumn, 0, true);
//place the new doodlebug in the randomly generated cell
board[randomRow][randomColumn] = newBug;
}
/** Removes the doodlebug from the board
* @Param **board array of pointers to organisms representing the gameboard
* @Return void, Removes the doodlebug from the board
*/
void Doodlebug::starve(Organism ***board){
board[this->row][this->column] = NULL;
}