-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
158 lines (129 loc) · 3 KB
/
Grid.cpp
File metadata and controls
158 lines (129 loc) · 3 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
// Grid.cpp -- ALP PISKIN & ANKIT KOTWAL
#include "Cell.h"
#include "Ant.h"
#include "Doodlebug.h"
#include "Grid.h"
#include <iostream>
#include <cstdlib>
#include <vector>
using std::cout;
using std::vector;
vector< vector<Cell*> > createCells2d(unsigned width, unsigned height, Grid* actual) {
vector< vector<Cell*> > grid;
for(unsigned i = 0; i < height; i++) {
vector<Cell*> cells;
grid.push_back(cells);
for(unsigned k = 0; k < width; k++) {
Cell *a = new Cell(i, k, actual);
grid[i].push_back(a);
}
}
return grid;
}
Grid::Grid() {
data = createCells2d(width, height, this);
}
Grid::Grid(unsigned width, unsigned height) {
this->width = width;
this->height = height;
data = createCells2d(width, height, this);
}
/*
struct Index {
unsigned int width;
unsigned int height;
};*/
bool Grid::loadGrid(float spawn_rate) {
return true;
}
bool Grid::loadGrid(unsigned numAnts, unsigned numDoodlebugs) {
unsigned total = numAnts + numDoodlebugs;
unsigned grid_size = width * height;
if(total > grid_size) return false;
for (unsigned i = 0; i < numAnts; i++) {
bool placed = false;
while(!placed){
unsigned x = rand() % width;
unsigned y = rand() % height;
if (data[x][y]->getOrganism() == NULL) {
Organism *to_set = new Ant();
data[x][y]->setOrganism(to_set);
placed = true;
}
}
}
for (unsigned i = 0; i < numDoodlebugs; i++) {
bool placed = false;
while(!placed){
unsigned x = rand() % width;
unsigned y = rand() % height;
if (data[x][y]->getOrganism() == NULL) {
Organism *to_set = new Doodlebug();
data[x][y]->setOrganism(to_set);
placed = true;
}
}
}
return true;
}
void Grid::printGrid() {
cout << "----------------------------------\n";
for(unsigned k = 0; k < height; k++){
for(unsigned i = 0; i < width; i++){
Organism* ptr = data[i][k]->getOrganism();
if(ptr != NULL){
if (ptr->getType().compare(Ant().getType()) == 0) {
cout << "\033[0;31mo \033[0m";
} else if (ptr->getType().compare(Doodlebug().getType()) == 0) {
cout << "\033[1;32mx \033[0m";
}
} else {
cout << " ";
}
}
cout << "\n";
}
}
Grid::~Grid() {
}
vector< vector<Cell*> > Grid::getData(){
return data;
}
unsigned Grid::getHeight(){
return height;
}
unsigned Grid::getWidth(){
return width;
}
void Grid::run(){
vector<Organism*> to_move;
for(unsigned k = 0; k < height; k++){
for(unsigned i = 0; i < width; i++){
Organism* ptr = data[i][k]->getOrganism();
if(ptr != NULL){
if (ptr->getType().compare(Doodlebug().getType()) == 0) {
to_move.push_back(ptr);
}
} else {
}
}
}
for(unsigned i = 0; i < to_move.size(); i++){
to_move[i]->move();
}
to_move.clear();
for(unsigned k = 0; k < height; k++){
for(unsigned i = 0; i < width; i++){
Organism* ptr = data[i][k]->getOrganism();
if(ptr != NULL){
if (ptr->getType().compare(Ant().getType()) == 0) {
to_move.push_back(ptr);
}
} else {
}
}
}
for(unsigned i = 0; i < to_move.size(); i++){
to_move[i]->move();
}
}