-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
503 lines (426 loc) · 13.6 KB
/
Game.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#include "Game.h"
#include <conio.h>
#include <fstream>
#include <windows.h>
#include <string>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <time.h>
#include "BattleUnits/EarthSoldier.h"
Game::Game() :RNG(this)
{
gameMode = INTERACTIVE;
timestep = 1;
alienAttacked = true;
earthAttacked = true;
infectionCount = 0;
totalInfectionCount = 0;
umlsoldier = 0;
infectionThreshold = 0;
saverActive = false;
healCount = 0;
loadInput();
// Enabling color support
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
}
bool Game::isInteractive() {
return (gameMode == INTERACTIVE);
}
void Game::printarmies()
{
cout << "\nCurrent Timestep: " << timestep << endl;
earthArmy.print();
alienArmy.print();
allyArmy.print();
}
void Game::printkilledunits()
{
cout << "\t============== \033[1;31mKilled/Destructed Units\033[0m ============\n";
cout << "\t" << killedUnits.getCount() << " units ";
killedUnits.print();
cout << endl;
}
void Game::printUML()
{
cout << "\t============== \033[1;32mUnit Maintenance List\033[0m ==============\n";
cout << "\t" << UML.getCount() << " units ";
UML.print();
cout << endl;
}
bool Game::getEarthUnit(UNIT_TYPE type, Unit*& unit)
{
return earthArmy.getUnit(type, unit);
}
bool Game::getAlienUnit(UNIT_TYPE type, Unit*& unit)
{
return alienArmy.getUnit(type, unit);
}
bool Game::getSaverUnit(Unit*& unit)
{
return allyArmy.getUnit(SU, unit);
}
bool Game::getfromUML(Unit*& unit)
{
int pri;
bool b = UML.dequeue(unit, pri);
EarthSoldier* soldier = dynamic_cast<EarthSoldier*>(unit);
if (soldier && (!soldier->isInfected() || soldier->isImmune())) --umlsoldier;
while (b && timestep - unit->getUMLJoinTime() > 10) {
unit->decrementHealth(unit->getHealth(), timestep);
addToKilled(unit);
b = UML.dequeue(unit, pri);
soldier = dynamic_cast<EarthSoldier*>(unit);
if (soldier && (!soldier->isInfected() || soldier->isImmune())) --umlsoldier;
}
return b;
}
bool Game::isUMLEmpty()
{
return (UML.isEmpty());
}
bool Game::addEarthUnit(Unit*& unit)
{
return earthArmy.addUnit(unit);
}
bool Game::addSaverUnit(Unit*& unit) {
return allyArmy.addUnit(unit);
}
bool Game::addAlienUnit(Unit*& unit)
{
return alienArmy.addUnit(unit);
}
void Game::gameTick() {
attackedIDs = "";
bool saverAttacked;
if (getInfectionPercentage() >= infectionThreshold)
saverActive = true;
else if (infectionCount == 0) {
saverActive = false;
killAllSaver();
}
RNG.generateUnits();
earthAttacked = earthArmy.attack(); // Earth army attack alien army
alienAttacked = alienArmy.attack(); // Alien army attack earth army
if (saverActive)
saverAttacked = allyArmy.attack(); // Saver army attack alien army
spreadInfect(); // Spread earth soldier infection
if (gameMode == INTERACTIVE) {
printarmies();
cout << "\t========== Units fighting at current step =========\n";
cout << attackedIDs;
printkilledunits();
printUML();
cout << "\t===================================================\n";
cout << "\tCurrent Infection Percentage is " << fixed << setprecision(2) << getInfectionPercentage() << "%" << endl;
}
++timestep;
}
void Game::addToAttacked(string ids) {
attackedIDs += ids;
}
void Game::addToKilled(Unit*& unit)
{
EarthSoldier* soldier = dynamic_cast<EarthSoldier*>(unit);
if (soldier && (soldier->isInfected() && !soldier->isImmune())) decrementInfected();
killedUnits.enqueue(unit);
}
void Game::addToUML(Unit*& unit, int joinUMLtime)
{
int pri;
if (unit->getType() == ES) {
pri = (earthData.maxHealth - unit->getHealth()) * 1000;
EarthSoldier* soldier = dynamic_cast<EarthSoldier*>(unit);
if (soldier && (!soldier->isInfected() || soldier->isImmune())) {
umlsoldier++;
}
}
else pri = -1;
UML.enqueue(unit, pri);
unit->setUMLJoinTime(joinUMLtime);
}
void Game::handleUnit(Unit* unit)
{
if (unit->isDead()) addToKilled(unit);
else if (unit->isLow()) addToUML(unit, timestep);
else {
if (unit->getType() == SU)addSaverUnit(unit);
else if (unit->isAlien()) addAlienUnit(unit);
else addEarthUnit(unit);
}
}
void Game::loadInput()
{
int N, Prob;
ifstream input_file;
input_file.open("input.txt", ios::in);
input_file >> N;
input_file >> percentages.percentES >> percentages.percentET >> percentages.percentEG >> percentages.percentHU;
input_file >> percentages.percentAS >> percentages.percentAM >> percentages.percentAD;
input_file >> Prob;
input_file >> earthData.minPower >> earthData.maxPower;
input_file >> earthData.minHealth >> earthData.maxHealth;
input_file >> earthData.minCapacity >> earthData.maxCapacity;
earthData.maxPower *= -1;
earthData.maxHealth *= -1;
earthData.maxCapacity *= -1;
input_file >> alienData.minPower >> alienData.maxPower;
input_file >> alienData.minHealth >> alienData.maxHealth;
input_file >> alienData.minCapacity >> alienData.maxCapacity;
alienData.maxPower *= -1;
alienData.maxHealth *= -1;
alienData.maxCapacity *= -1;
input_file >> percentages.percentIf;
input_file >> infectionThreshold;
input_file >> allyData.minPower >> allyData.maxPower;
input_file >> alienData.minHealth >> allyData.maxHealth;
input_file >> alienData.minCapacity >> allyData.maxCapacity;
allyData.maxPower *= -1;
allyData.maxHealth *= -1;
allyData.maxCapacity *= -1;
RNG.setData(earthData, alienData, allyData, percentages, N, Prob);
input_file.close();
}
EarthArmy* Game::getEarthArmy() {
return &earthArmy;
}
AlienArmy* Game::getAlienArmy() {
return &alienArmy;
}
AllyArmy* Game::getAllyArmy() {
return &allyArmy;
}
bool Game::saverIsActive() {
return saverActive;
}
void Game::incrementInfected()
{
++infectionCount;
++totalInfectionCount;
}
void Game::decrementInfected()
{
--infectionCount;
}
int Game::getAliveSoldiers()
{
return umlsoldier + earthArmy.getSoldierCount();
}
double Game::getInfectionPercentage()
{
if (infectionCount == 0 || getAliveSoldiers() == 0) {
return 0;
}
double b = (double(infectionCount) / getAliveSoldiers()) * 100;
return b;
}
double Game::getSoldierRatio()
{
return (double(earthArmy.getSoldierCount()) / (alienArmy.getSoldierCount())) * 100;
}
int Game::getTimestep()
{
return timestep;
}
void Game::startGame() {
char ch = 0;
string option = "";
cout << "Run The game in interactive or silent mode ? ( [I]nteractive / [S]ilent ): ";
cin >> option;
for (int i = 0; i < option.size() && i < 20; ++i)
option[i] = tolower(option[i]);
if (option[0] == 'i' || option == "interactive") gameMode = INTERACTIVE;
else {
cout << "Silent Mode\nSimulation Starts...\n";
gameMode = SILENT;
}
ch = 0;
while (ch != 27 && (timestep <= 40 || earthArmy.isAlive() && alienArmy.isAlive() && !isDraw())) {
gameTick();
if (gameMode == INTERACTIVE)
ch = _getch();
}
endGame();
}
string Game::getRatio(double x, double y) {
if (y == 0)return "";
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << x / y;
return ss.str();
}
bool Game::isDraw() {
return !(earthAttacked || alienAttacked ) && (earthArmy.isAlive() && alienArmy.isAlive());
}
void Game::spreadInfect()
{
Unit* unit = nullptr;
if (this->infectionCount < 1) return;
int sc = earthArmy.getSoldierCount();
int random = RNG.generator(0, sc);
int random2 = RNG.generator(1,100);
EarthSoldier* s1;
bool infected = false;
if (random2 <= 2)
for (int i = 0; i < sc; i++) {
this->getEarthUnit(ES, unit);
if (i == random) {
s1 = dynamic_cast<EarthSoldier*>(unit);
if (!s1->isInfected()) {
s1->setInfected(true);
incrementInfected();
}
}
this->addEarthUnit(unit);
}
}
void Game::incrementHealCount(){
++healCount;
}
void Game::killAllSaver()
{
Unit* saver;
while (allyArmy.getUnit(SU, saver)) {
saver->decrementHealth(saver->getHealth(), timestep);
addToKilled(saver);
}
}
void Game::endGame() {
Unit* temp;
int pri = 0;
// Kill the remaining units in the UML
while (UML.dequeue(temp, pri)) {
temp->decrementHealth(temp->getHealth(), timestep);
addToKilled(temp);
}
ofstream outputFile;
outputFile.open("output.txt", ios::out);
Unit* deadUnit = nullptr;
int Df, Dd, Db;
int Tj, Ta, Td;
int countES, countAS, countET, countEG, countAM, countAD, countHU;
int totalCountES, totalCountAS, totalCountET, totalCountEG, totalCountAM, totalCountAD, totalCountHU;
int totalInitialEarthCount;
int totalInitialAlienCount;
int totalEarthCount;
int totalAlienCount;
int earthSumDf, earthSumDd, earthSumDb;
int alienSumDf, alienSumDd, alienSumDb;
countES = countAS = countET = countEG = countAM = countAD = countHU = 0;
earthSumDf = earthSumDd = earthSumDb = alienSumDf = alienSumDd = alienSumDb = 0;
int killedCount = killedUnits.getCount();
killedUnits.peek(deadUnit);
while (killedCount) {
killedUnits.dequeue(deadUnit);
Tj = deadUnit->getJoinTime();
Ta = deadUnit->getFirstAttackTime();
Td = deadUnit->getDestructionTime();
Df = Ta - Tj;
Dd = Td - Ta;
Db = Td - Tj;
// Print each killed unit and its properties
outputFile << Td << "\t\t"
<< deadUnit->getId() << "\t\t"
<< Tj << "\t\t"
<< Df << "\t\t"
<< Dd << "\t\t"
<< Db << '\n';
// Count the number of dead units
switch (deadUnit->getType()) {
case ES: ++countES; break;
case ET: ++countET; break;
case EG: ++countEG; break;
case HU: ++countHU; break;
case AS: ++countAS; break;
case AM: ++countAM; break;
case AD: ++countAD; break;
}
if (deadUnit->isAlien()) {
alienSumDf += Df;
alienSumDd += Dd;
alienSumDb += Db;
}
else if(deadUnit->getType() != SU) {
earthSumDf += Df;
earthSumDd += Dd;
earthSumDb += Db;
}
killedUnits.enqueue(deadUnit); // add the unit again for the destructor
--killedCount;
}
string battleResult = "\nBattle result: "; // Assume the win state when earth win not alien (but i am sided with aliens :D )
if ((!earthArmy.isAlive() && !alienArmy.isAlive()) || isDraw()) battleResult += "Draw";
else if (earthArmy.isAlive() && !alienArmy.isAlive()) battleResult += "Win";
else if (!earthArmy.isAlive() && alienArmy.isAlive()) battleResult += "Loss";
outputFile << battleResult << '\n';
// Get the total Count for each unit in each army
totalCountES = earthArmy.getTotalSoldierCount();
totalCountET = earthArmy.getTotalTankCount();
totalCountEG = earthArmy.getTotalGunneryCount();
totalCountHU = earthArmy.getTotalHealCount();
totalCountAS = alienArmy.getTotalSoldierCount();
totalCountAM = alienArmy.getTotalMonsterCount();
totalCountAD = alienArmy.getTotalDroneCount();
// Earth army print
outputFile << "\nFor Earth Army:\n";
// Print the total for each unit
outputFile << "\tTotal ES: " << totalCountES << "\t"
<< "Total ET: " << totalCountET << "\t"
<< "Total EG: " << totalCountEG << "\t"
<< "Total HU: " << totalCountHU << "\n";
// Percent dead units per total units
outputFile << "\tDeadES/TotalES: " << getRatio(countES * 100.0, totalCountES) << "%\t\t"
<< "DeadET/TotalET: " << getRatio(countET * 100.0, totalCountET) << "%\t\t"
<< "DeadEG/TotalEG: " << getRatio(countEG * 100.0, totalCountEG) << "%\t\t"
<< "DeadHU/TotalHU: " << getRatio(countHU * 100.0, totalCountHU) << "%\n";
totalEarthCount = countES + countET + countEG + countHU;
totalInitialEarthCount = totalCountES + totalCountET + totalCountEG + totalCountHU;
// Print total dead units over total earth units
outputFile << "\tDeadEarthUnits/TotalEarthUnits: " << getRatio(totalEarthCount * 100.0, totalInitialEarthCount) << "%\n";
// Average first attack delay, destruction delay, battle time
outputFile << "\tAverage Df: " << getRatio(earthSumDf, totalEarthCount) << "\t\t"
<< "Average Dd: " << getRatio(earthSumDd, totalEarthCount) << "\t\t"
<< "Average Db: " << getRatio(earthSumDb, totalEarthCount) << '\n';
// Percent first attack delay / battle time , destruction delay / battle time
outputFile << "\tDf/Db: " << getRatio(earthSumDf * 100.0, earthSumDb) << "%\t\t"
<< "Dd/Db: " << getRatio(earthSumDd * 100.0, earthSumDb) << "%\n";
// Alien army print
outputFile << "\nFor Alien Army:\n";
// Print the total for each unit
outputFile << "\tTotal AS: " << totalCountAS << "\t"
<< "Total AM: " << totalCountAM << "\t"
<< "Total AD: " << totalCountAD << '\n';
// Percent dead units per total units
outputFile << "\tDeadAS/TotalAS: " << getRatio(countAS * 100.0, totalCountAS) << "%\t\t"
<< "DeadAM/TotalAM: " << getRatio(countAM * 100.0, totalCountAM) << "%\t\t"
<< "DeadAD/TotalAD: " << getRatio(countAD * 100.0, totalCountAD) << "%\n";
totalAlienCount = countAS + countAM + countAD;
totalInitialAlienCount = totalCountAS + totalCountAM + totalCountAD;
// Print total dead units over total alien units
outputFile << "\tDeadAlienUnits/TotalAlienUnits: " << getRatio(totalAlienCount * 100.0, totalInitialAlienCount) << "%\n";
// Average first attack delay, destruction delay, battle time
outputFile << "\tAverage Df: " << getRatio(alienSumDf, totalAlienCount) << "\t\t"
<< "Average Dd: " << getRatio(alienSumDd, totalAlienCount) << "\t\t"
<< "Average Db: " << getRatio(alienSumDb, totalAlienCount) << '\n';
// Percent first attack delay / battle time , destruction delay / battle time
outputFile << "\tDf/Db: " << getRatio(alienSumDf * 100.0, alienSumDb) << "%\t\t"
<< "Dd/Db: " << getRatio(alienSumDd * 100.0, alienSumDb) << "%\n";
// Percent healed unit / total earth units
outputFile << "\nHealedUnits/TotalEarthUnits: " << getRatio(healCount * 100, totalInitialEarthCount) << "%\n";
// Percent infected soldiers / total earth soldiers
outputFile << "\nInfectedSoldier/TotalEarthSoldiers: " << getRatio(totalInfectionCount * 100, totalCountES) << "%\n";
outputFile.close();
cout << "Simulation ends, Output file is created\n";
}
Game::~Game() {
Unit* unit = nullptr;
killedUnits.peek(unit);
while (killedUnits.dequeue(unit))
{
if (unit != nullptr)
delete unit;
}
}