-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommercialSystem.cpp
141 lines (128 loc) · 4.04 KB
/
CommercialSystem.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
// CommercialSystem.cpp
#include "CommercialSystem.h"
#include <algorithm>
struct GrowthCell
{
int x, y;
int population;
int adjacentPop;
};
bool CommercialSystem::isPowered(const std::vector<std::vector<Cell>> &grid, int x, int y)
{
for (int dy = -1; dy <= 1; dy++)
{
for (int dx = -1; dx <= 1; dx++)
{
if (dx == 0 && dy == 0)
continue;
int newX = x + dx;
int newY = y + dy;
if (newX >= 0 && newX < grid[0].size() && newY >= 0 && newY < grid.size())
{
char type = grid[newY][newX].getType();
if (type == 'T' || type == '#' || type == 'P')
{
return true;
}
}
}
}
return false;
}
int CommercialSystem::countAdjacentPopulation(const std::vector<std::vector<Cell>> &grid, int x, int y, int minPop)
{
int count = 0;
for (int dy = -1; dy <= 1; dy++)
{
for (int dx = -1; dx <= 1; dx++)
{
if (dx == 0 && dy == 0)
continue;
int newX = x + dx;
int newY = y + dy;
if (newX >= 0 && newX < grid[0].size() && newY >= 0 && newY < grid.size())
{
const Cell &cell = grid[newY][newX];
if (cell.getType() == 'C' && cell.getPopulation() >= minPop)
{
count++;
}
}
}
}
return count;
}
bool CommercialSystem::canGrow(const std::vector<std::vector<Cell>> &grid, int x, int y)
{
const Cell &cell = grid[y][x];
int pop = cell.getPopulation();
switch (pop)
{
case 0:
return isPowered(grid, x, y) || countAdjacentPopulation(grid, x, y, 1) >= 1;
case 1:
return countAdjacentPopulation(grid, x, y, 1) >= 2;
default:
return false;
}
}
void CommercialSystem::update(std::vector<std::vector<Cell>> &grid, int &availableWorkers, int &availableGoods)
{
std::vector<GrowthCell> growthCells;
// First: Identify all potential growth cells
for (size_t y = 0; y < grid.size(); y++)
{
for (size_t x = 0; x < grid[0].size(); x++)
{
if (grid[y][x].getType() == 'C' && canGrow(grid, x, y))
{
growthCells.push_back({static_cast<int>(x),
static_cast<int>(y),
grid[y][x].getPopulation(),
countAdjacentPopulation(grid, x, y, 1)});
}
}
}
// Sort by priority rules
std::sort(growthCells.begin(), growthCells.end(),
[](const GrowthCell &a, const GrowthCell &b)
{
// 1. Population size
if (a.population != b.population)
return a.population > b.population;
// 2. Adjacent population
if (a.adjacentPop != b.adjacentPop)
return a.adjacentPop > b.adjacentPop;
// 3. Y coordinate (smaller first)
if (a.y != b.y)
return a.y < b.y;
// 4. X coordinate (smaller first)
return a.x < b.x;
});
// Apply growth to sorted cells
for (const auto &cell : growthCells)
{
if (availableWorkers >= 1 && availableGoods >= 1)
{
grid[cell.y][cell.x].setPopulation(
grid[cell.y][cell.x].getPopulation() + 1);
availableWorkers--;
availableGoods--;
}
}
}
int CommercialSystem::getTotalPopulation(const std::vector<std::vector<Cell>> &grid)
{
int total = 0;
for (const auto &row : grid)
{
for (const auto &cell : row)
{
if (cell.getType() == 'C')
{
total += cell.getPopulation();
}
}
}
return total;
}