-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCity.cpp
More file actions
127 lines (108 loc) · 4 KB
/
City.cpp
File metadata and controls
127 lines (108 loc) · 4 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
// City.cpp
#include <cstdlib>
#include <random>
#include "City.h"
#include "Organism.h"
#include "GameSpecs.h"
#include "Human.h"
#include "Zombie.h"
using namespace std;
int City::generateRandomNumber(int startRange, int endRange) const {
return rand() % (endRange - startRange + 1) + startRange;
}
City::City(){
srand(time(NULL));
timeStepCount = 0;
for (int x = 0; x < GRID_SIZE; x++)
for (int y = 0; y < GRID_SIZE; y++)
grid[x][y] = nullptr;
// Create and place initial humans
for (int i = 0; i < HUMAN_START_COUNT; ++i) {
int x = rand() % (GRID_SIZE / 2); // Random x coordinate within half of the grid width
int y = rand() % (GRID_SIZE / 2); // Random y coordinate within half of the grid height
// Distribute humans in each quadrant
x += (i % 2) * (GRID_SIZE / 2); // Shift x coordinate for alternating columns
y += (i / (HUMAN_START_COUNT / 2)) * (GRID_SIZE / 2); // Shift y coordinate for each quadrant
// Check if the chosen cell is empty before placing a human
if (grid[x][y] == nullptr) {
grid[x][y] = new Human(this, x, y);
} else {
--i;
}
}
// Create and place initial zombies
for (int i = 0; i < ZOMBIE_START_COUNT; ++i) {
int x = rand() % (GRID_SIZE / 2); // Random x coordinate within half of the grid width
int y = rand() % (GRID_SIZE / 2); // Random y coordinate within half of the grid height
// Distribute zombies in each quadrant
x += (i % 2) * (GRID_SIZE / 2); // Shift x coordinate for alternating quadrants
y += (i / (ZOMBIE_START_COUNT / 2)) * (GRID_SIZE / 2); // Shift y coordinate for each quadrant
// Check if the chosen cell is empty before placing a zombie
if (grid[x][y] == nullptr) {
grid[x][y] = new Zombie(this, x, y);
} else {
--i;
}
}
}
void City::takeTimeStep(){
timeStepCount++;
for (int x = 0; x < GRID_SIZE; x++){
for (int y = 0; y < GRID_SIZE; y++){
if (grid[x][y] != nullptr) {
grid[x][y]->turn();
}
}
}
for (int x = 0; x < GRID_SIZE; x++){
for (int y = 0; y < GRID_SIZE; y++){
if (grid[x][y] != nullptr && grid[x][y]->starves()) {
delete grid[x][y];
grid[x][y] = new Human(this, x, y); // if starves add a human in this place
}
}
}
// Reset moved flag for the next time step
for (int x = 0; x < GRID_SIZE; x++) {
for (int y = 0; y < GRID_SIZE; y++) {
if (grid[x][y] != nullptr) {
grid[x][y]->setMoved(false);
}
}
}
}
int City::getGeneration() {
return timeStepCount;
}
int City::countType(char organismType) {
int count = 0;
// Iterate through the grid to count organisms of a specific type
for (int i = 0; i < GRID_SIZE; ++i) {
for (int j = 0; j < GRID_SIZE; ++j) {
if (grid[i][j] != nullptr && grid[i][j]->getType() == organismType) {
count++;
}
}
}
return count;
}
std::ostream& operator<<(std::ostream& output, const City& city) {
for (int x = 0; x < GRID_SIZE; x++){
for (int y = 0; y < GRID_SIZE; y++){
if (city.grid[x][y] != nullptr) {
if (city.grid[x][y]->getType() == ZOMBIE_CH) {
output << "\033[38;5;" << ZOMBIE_COLOR << "m";
output << 'Z' << " ";
} else if (city.grid[x][y]->getType() == HUMAN_CH) {
output << "\033[38;5;" << HUMAN_COLOR << "m";
output << 'H' << " ";
}
output << "\033[0m"; // Reset text color to default
} else {
output << SPACE_CH << " ";
}
}
output << std::endl;
}
return output;
}