Repository navigation
Expand file tree
/
Copy pathSimulationManager.cpp
More file actions
180 lines (148 loc) · 5.12 KB
/
Copy pathSimulationManager.cpp
File metadata and controls
180 lines (148 loc) · 5.12 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "SimulationManager.h"
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
SimulationManager::SimulationManager(int width, int height)
: grid(width, height), stepCount(0), autoMode(true), rescued(0), burned(0) {
srand(time(nullptr));
initializeSimulation();
}
void SimulationManager::initializeSimulation() {
int numPeople = 8;
for (int i = 0; i < numPeople; i++) {
Position pos;
int attempts = 0;
do {
pos.x = rand() % (grid.getWidth() - 4) + 2;
pos.y = rand() % (grid.getHeight() - 4) + 2;
attempts++;
} while (!grid.isWalkable(pos) && attempts < 50);
if (attempts < 50) {
people.emplace_back(pos, i);
grid.setCell(pos, 'P');
}
}
int numFires = 2;
for (int i = 0; i < numFires; i++) {
Position pos;
int attempts = 0;
do {
pos.x = rand() % (grid.getWidth() - 4) + 2;
pos.y = rand() % (grid.getHeight() - 4) + 2;
attempts++;
} while (grid.getCell(pos) != ' ' && attempts < 50);
if (attempts < 50) {
fire.addFire(pos, grid);
}
}
}
void SimulationManager::step() {
stepCount++;
for (Person& person : people) {
person.update(grid, fire.getFirePositions(), stepCount);
}
if (stepCount % 2 == 0) {
fire.spread(grid);
}
updateStats();
}
void SimulationManager::updateStats() {
rescued = 0;
burned = 0;
for (const Person& person : people) {
if (person.isRescued()) rescued++;
else if (!person.isAlive()) burned++;
}
}
void SimulationManager::displayStats() {
cout << "\n=== FIRE EVACUATION SIMULATION ===" << endl;
cout << "Step: " << stepCount << endl;
cout << "People Rescued: " << rescued << endl;
cout << "People Burned: " << burned << endl;
cout << "Still Evacuating: " << (people.size() - rescued - burned) << endl;
cout << "\nLegend: P=Person, F=Fire, E=Exit, #=Wall" << endl;
if (autoMode) {
cout << "Press 'q' to quit, 'm' for manual mode" << endl;
} else {
cout << "Press SPACE for next step, 'a' for auto mode, 'q' to quit" << endl;
}
displayAlgorithmStats();
}
void SimulationManager::displayAlgorithmStats() {
cout << "\n=== ALGORITHM STATISTICS ===" << endl;
bool hasActivePeople = false;
for (const Person& person : people) {
if (person.isAlive() && !person.isRescued()) {
hasActivePeople = true;
break;
}
}
if (!hasActivePeople) {
cout << "No active people remaining" << endl;
} else {
cout << "Individual People Status:" << endl;
for (const Person& person : people) {
if (person.isAlive() && !person.isRescued()) {
cout << " Person " << person.getId() << ": "
<< person.getAlgorithmName()
<< " at (" << person.getPosition().x
<< "," << person.getPosition().y << ") " << endl;
}
}
}
cout << "======================================" << endl;
}
bool SimulationManager::isSimulationComplete() {
return rescued + burned >= people.size() || stepCount > 100;
}
void SimulationManager::run() {
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
cursorInfo.bVisible = false;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
grid.clearScreen();
while (!isSimulationComplete()) {
grid.display();
displayStats();
if (autoMode) {
Sleep(800);
if (_kbhit()) {
char key = _getch();
if (key == 'q' || key == 'Q') break;
if (key == 'm' || key == 'M') {
autoMode = false;
cout << "\nSwitched to manual mode - Press SPACE to continue..." << endl;
_getch();
}
}
} else {
char input = _getch();
if (input == 'q' || input == 'Q') break;
if (input == 'a' || input == 'A') {
autoMode = true;
cout << "\nSwitched to auto mode" << endl;
Sleep(1000);
}
}
step();
}
grid.display();
displayStats();
// Clear screen and add spacing before final results
grid.clearScreen();
cout << "\n=== SIMULATION COMPLETE ===" << endl;
cout << "Final Results:" << endl;
cout << "- Total Steps: " << stepCount << endl;
cout << "- People Rescued: " << rescued << endl;
cout << "- People Lost: " << burned << endl;
if (people.size() > 0) {
cout << "- Success Rate: " << (rescued * 100.0 / people.size()) << "%" << endl;
}
cursorInfo.bVisible = true;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
cout << "\nPress any key to exit..." << endl;
_getch();
}