forked from supermop3000/antgame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnt.cpp
203 lines (187 loc) · 6.08 KB
/
Ant.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
/**************************************************************************************************
** Program name:Group Project - Predator-Prey Game
** Author: Group 4
** Date: 2019/02/04
** Description: THIS IS THE IMPLEMENTATION FILE OF THE ANT CLASS
** This program runs a Predator-Prey Game with "Doodlebug"
** predators and "Ant" prey. The program starts with a menu and
** has the user set the number steps for the simulation to take.
** Once the simulation is complete, the user is queried for the
** whether they want to continue with more steps or quit.
**************************************************************************************************/
// Ant.cpp
#include "Critter.hpp"
#include "Ant.hpp"
#include "Group4Utils.hpp"
/**************************************************************************************************
** Description: Ant()
** This is the default constructor for the Ant Class
**************************************************************************************************/
Ant::Ant(int x_pos, int y_pos):Critter(x_pos, y_pos)
{
this->breedAge = 3;
this->symbol = 'O';
}
/**************************************************************************************************
** Description: void move(Critter***, int, int))
** This function moves an Ant on the board.
** The Critter*** argument represents the board the Critters are on.
** The two integers represent the dimensions of the board.
**************************************************************************************************/
void Ant::move(Critter *** &boardIn, int size_x, int size_y)
{
int new_x, new_y;
bool cellFree = false;
bool inBounds = false;
int random = randIntRange(0,3);
int attempts = 0;
// switch (random)
while(attempts < 4)
{
//case 0:
if(random == 0)
{
new_x = this->x_pos - 1;
new_y = this->y_pos;
inBounds = checkBounds(new_x, new_y, size_x, size_y);
if(inBounds && boardIn[new_x][new_y] == NULL)
{
cellFree = true;
break;
}
else if(attempts >= 4)
break;
else
{
attempts++;
random = 1;
}
}
if(random == 1)
{
//case 1:
new_x = this->x_pos;
new_y = this->y_pos + 1;
inBounds = checkBounds(new_x, new_y, size_x, size_y);
if(inBounds && boardIn[new_x][new_y] == NULL)
{
cellFree = true;
break;
}
else if(attempts >= 4)
break;
else
{
attempts++;
random = 2;
}
}
if(random == 2)
{
//case 2:
new_x = this->x_pos + 1;
new_y = this->y_pos;
inBounds = checkBounds(new_x, new_y, size_x, size_y);
if(inBounds && boardIn[new_x][new_y] == NULL)
{
cellFree = true;
break;
}
else if(attempts >= 4)
break;
else
{
attempts++;
random = 3;
}
}
if(random == 3)
{
//case 3:
new_x = this->x_pos;
new_y = this->y_pos - 1;
inBounds = checkBounds(new_x, new_y, size_x, size_y);
if(inBounds && boardIn[new_x][new_y] == NULL)
{
cellFree = true;
break;
}
else if(attempts >= 4)
break;
else
{
attempts++;
random = 0;
}
}
}
if(cellFree == true)
{
//delete boardIn[new_x][new_y];
boardIn[new_x][new_y] = boardIn[this->x_pos][this->y_pos];
boardIn[this->x_pos][this->y_pos] = NULL;
this->x_pos = new_x;
this->y_pos = new_y;
this->moveSuccess = 1;
}
else
{
this->moveSuccess = 1;
return;
}
// age ants
age++;
}
/* if (direction == 0) //move North (up)
{
//check that board[x_pos -1][y] is not out of bounds and has symbol = ' '
if (this->x_pos -1 > 0)
{
if (boardIn[this->x_pos -1][this->y_pos]->getSymbol() == ' ')
{
//Copy Ant to New Location
//Delete Ant in Old Location
}
}
//else the ant stays in its current cell
}
else if (direction == 1) //move South (down)
{
//check that board[x_pos + 1][y] is not out of bounds and has symbol = ' ' (check that x_pos+1 > rows -1)
if (this->x_pos + 1 <rows)
{
if (boardIn[this->x_pos + 1][this->y_pos]->getSymbol() == ' ')
{
//Copy Ant to New Location
//Delete Ant in Old Location
}
}
//else the ant stays in its current cell
}
else if (direction == 2) //move East (right)
{
//check that position[x_pos][y_pos + 1] is not out of bounds and has symbol = ' '
if (this->y_pos + 1 < cols)
{
if (boardIn[this->x_pos][this->y_pos + 1]->getSymbol() == ' ')
{
//Copy Ant to New Location
// Ant in Old Location
}
}
//else the ant stays in its current cell
}
else //move West (left)
{
//check that position[x_pos][y_pos -1] is not out of bounds and has null pointer or symbol ' '
if (this->y_pos -1 > 0)
{
if (boardIn[this->x_pos][this->y_pos-1]->getSymbol() == ' ')
{
//Copy Ant to New Location
//Delete Ant in Old Location
}
}
//else ant stays in its current cell
}
this->age++;*/