-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnt.cpp
More file actions
83 lines (64 loc) · 1.43 KB
/
Copy pathAnt.cpp
File metadata and controls
83 lines (64 loc) · 1.43 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
// Ant.cpp -- ALP PISKIN & ANKIT KOTWAL
#include "Ant.h"
Ant::Ant() {
}
Ant::~Ant() {
}
void Ant::move(){
Cell* c = this->getCell();
vector<Cell*> cells = getCell()->getAdjacent();
bool can_move = false;
for (unsigned i = 0; i < cells.size(); i++) {
if(cells[i]->getOrganism() == NULL){can_move = true;}
}
while(can_move){
int x = 0;
int y = 0;
if(rand()%2){
x = rand()%2 ? -1 : 1;
} else {
y = rand()%2 ? -1 : 1;
}
int new_x = c->getX() + x;
int new_y = c->getY() + y;
if ((new_x >=0 && new_x < (int)c->getGrid()->getWidth()) && (new_y >=0 && new_y < (int)c->getGrid()->getHeight())) {
if (c->getGrid()->getData()[new_x][new_y]->getOrganism() == NULL) {
c->getGrid()->getData()[new_x][new_y]->setOrganism(this);
c->setOrganism(NULL);
break;
}
}
}
if (move_count >= 3) {
breed();
}
move_count++;
}
void Ant::breed(){
bool can_breed = false;
vector<Cell*> cells = getCell()->getAdjacent();
for (unsigned i = 0; i < cells.size(); i++) {
if(cells[i]->getOrganism() == NULL){can_breed = true;}
}
if (can_breed) {
unsigned i = 0;
while (true) {
float r = float(rand()) / float(RAND_MAX);
float chance = 1.0/float(cells.size());
if(r > chance){
if(cells[i]->getOrganism() == NULL){
move_count = 0;
cells[i]->setOrganism(new Ant());
break;
}
}
i++;
if (i >= cells.size()) {
i = 0;
}
}
}
}
std::string Ant::getType(){
return "Ant";
}