-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
687 lines (557 loc) · 24.8 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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Game.h"
#include "UnitClasses/Unit.h"
#include "UnitClasses/AlienMonster.h"
// Global helper functions
float calculateRatio(int numerator, int denominator) // Helper function to calculate the ratio of two numbers
{
if (denominator == 0)
return 0;
return (float) numerator / denominator;
}
float calculatePercentage(int numerator, int denominator) // Helper function to calculate the percentage of two numbers
{
return calculateRatio(numerator, denominator) * 100;
}
Game::Game(): gameMode(GameMode::INTERACTIVE), currentTimestep(0), earthArmy(this), alienArmy(this), earthAlliedArmy(this), randomGenerator(this)
{}
void Game::run(GameMode gameMode, const std::string& inputFileName, const std::string& outputFileName)
{
// Change the game mode
setGameMode(gameMode);
// Load the parameters from the file and set the parameters in the random generator
if (!loadParameters(inputFileName)) // If the file is not found, print an error message and return
{
std::cout << "Error: File not found!" << std::endl;
return;
}
// Run the game
bool didArmiesAttack = true;
do
{
// Increment Timestep
currentTimestep++;
// Generate units for both armies
randomGenerator.generateUnits();
// Start fight
didArmiesAttack = startAttack();
// Check if earth no longer has infected soldiers to kill the savers in the allied army
if (earthArmy.getInfectedSoldiersCount() == 0)
killSaverUnits();
// Spread infection in the Earth Army
earthArmy.spreadInfection();
// Print the output
if (gameMode == GameMode::INTERACTIVE)
{
printAll();
std::cout << "Press Enter to continue..." << std::endl;
while (std::cin.get() != '\n');
}
} while (!battleOver(didArmiesAttack));
// Empty the unit maintenance list by returning the units to the appropriate army
emptyUnitMaintenanceList();
// Produce the output file
generateOutputFile(outputFileName);
// Print the final results
printFinalResults();
}
bool Game::startAttack()
{
// Make both armies attack
bool didEarthArmyAttack = earthArmy.attack();
bool didAlienArmyAttack = alienArmy.attack();
bool didEarthAlliedAttack = earthAlliedArmy.attack();
// Return if any of the armies successfully attacked
return didEarthArmyAttack || didAlienArmyAttack || didEarthAlliedAttack;
}
void Game::setGameMode(GameMode gameMode)
{
this->gameMode = gameMode;
}
bool Game::battleOver(bool didArmiesAttack) const
{
// Game ending cases
bool anArmyDied = earthArmy.isDead() || alienArmy.isDead(); // If one army completely killed the other
bool noAttackTie = !didArmiesAttack; // If both armies weren't able to attack - considered as a tie
// Don't check for end battle condition unless it has run for at least 40 timesteps
return currentTimestep >= 40 && (anArmyDied || noAttackTie);
}
void Game::killSaverUnits()
{
Unit* saverToKill = earthAlliedArmy.removeUnit(UnitType::SU); // Remove a saver from its list
while (saverToKill)
{
saverToKill->receiveDamage(saverToKill->getHealth()); // Prepare it to be killed
addToKilledList(saverToKill); // Add it to the killed list
saverToKill = nullptr;
saverToKill = earthAlliedArmy.removeUnit(UnitType::SU); // Remove another saver
}
}
bool Game::doesEarthNeedHelp() const
{
return earthArmy.needAllyHelp();
}
void Game::printFinalResults() const
{
std::cout << std::endl;
// Print an overflow error message
if (Unit::cantCreateEarthUnit() || Unit::cantCreateAlienUnit() || Unit::cantCreateEarthAlliedUnit())
std::cout << "Warning: The maximum number of units has been reached!" << std::endl << std::endl;
// Print battle results
std::cout << "What a battle!" << std::endl;
std::cout << "Battle Result: " << battleResult() << std::endl;
std::cout << "Check the output file for a detailed conclusion" << std::endl;
}
void Game::emptyUnitMaintenanceList()
{
HealableUnit* unit = nullptr;
int dummyPri = 0;
while (unitMaintenanceList.dequeue(unit, dummyPri))
{
unit->receiveDamage(unit->getHealth()); // Kill the unit
addToKilledList(unit); // Send it to the killed list
unit = nullptr; // Nullify pointer
}
}
std::string Game::battleResult() const
{
if (earthArmy.isDead() && !alienArmy.isDead()) // If the Earth army is dead and the Alien army is not dead, the Alien army wins
return "Alien Army wins!";
else if (!earthArmy.isDead() && alienArmy.isDead()) // If the Earth army is dead and the Alien army is not dead, the Earth army wins
return "Earth Army wins!";
else // If both armies are dead or no army was able to attack, the result is a draw
return "Draw!";
}
void Game::addUnit(Unit* unit)
{
if (!unit)
return;
ArmyType armyType = unit->getArmyType();
switch (armyType)
{
case ArmyType::EARTH:
earthArmy.addUnit(unit);
break;
case ArmyType::ALIEN:
alienArmy.addUnit(unit);
break;
case ArmyType::EARTH_ALLIED:
earthAlliedArmy.addUnit(unit);
break;
}
}
Unit* Game::removeUnit(ArmyType armyType, UnitType unitType)
{
switch (armyType)
{
case ArmyType::EARTH:
return earthArmy.removeUnit(unitType);
case ArmyType::ALIEN:
return alienArmy.removeUnit(unitType);
case ArmyType::EARTH_ALLIED:
return earthAlliedArmy.removeUnit(unitType);
}
return nullptr;
}
LinkedQueue<Unit*> Game::getEnemyList(ArmyType armyType, UnitType unitType, int attackCapacity)
{
LinkedQueue<Unit*> enemyUnits;
Unit* enemyUnitPtr = nullptr;
// Loop on the armies' lists depending on the attacker's attack capacity
// if the required enemy unit is found, enqueue it to be sent
switch (armyType)
{
case ArmyType::EARTH:
for (int i = 0; i < attackCapacity; i++)
{
enemyUnitPtr = earthArmy.removeUnit(unitType);
if (enemyUnitPtr)
enemyUnits.enqueue(enemyUnitPtr);
else
break;
}
break;
case ArmyType::ALIEN:
for (int i = 0; i < attackCapacity; i++)
{
enemyUnitPtr = alienArmy.removeUnit(unitType);
if (enemyUnitPtr)
enemyUnits.enqueue(enemyUnitPtr);
else
break;
}
break;
case ArmyType::EARTH_ALLIED:
for (int i = 0; i < attackCapacity; i++)
{
enemyUnitPtr = earthAlliedArmy.removeUnit(unitType);
if (enemyUnitPtr)
enemyUnits.enqueue(enemyUnitPtr);
else
break;
}
}
return enemyUnits;
}
void Game::registerAttack(Unit* currentAttacker, const std::string& currentAction, const std::string& currentFoughtUnits)
{
attackers.enqueue(currentAttacker); // Store the current attacker
attackActions.enqueue(currentAction); // Store the action it made
foughtUnits.enqueue(currentFoughtUnits); // Store the list of units the action happened on
}
void Game::addToKilledList(Unit* unit)
{
// Add the unit to the killed list
killedList.enqueue(unit);
// Set the destruction time of the unit
unit->setDestructionTime(currentTimestep);
}
void Game::addUnitToMaintenanceList(HealableUnit* unit)
{
// Enqueue the unit with its priority to the maintenance list
unitMaintenanceList.enqueue(unit, unit->getHealPriority());
// Set the time when the unit joined the UML
unit->setUMLjoinTime(currentTimestep);
}
LinkedQueue<HealableUnit*> Game::getUnitsToMaintainList(int attackCapacity)
{
LinkedQueue<HealableUnit*> unitsToMaintain;
HealableUnit* unit = nullptr;
int dummyPri = 0;
for (int i = 0; i < attackCapacity; i++)
{
unit = nullptr;
if (unitMaintenanceList.dequeue(unit, dummyPri))
unitsToMaintain.enqueue(unit);
else
break;
}
return unitsToMaintain;
}
void Game::printKilledList() const
{
std::cout << killedList.getCount() << " units [";
killedList.printList();
std::cout << "]" << std::endl;
}
void Game::printUnitMaintenanceList() const
{
std::cout << unitMaintenanceList.getCount() << " units [";
unitMaintenanceList.printList();
std::cout << "]" << std::endl;
}
void Game::printUnitsFighting()
{
Unit* currentAttacker = nullptr;
std::string currentFoughtUnits, currentAction;
while (attackers.dequeue(currentAttacker) && attackActions.dequeue(currentAction) && foughtUnits.dequeue(currentFoughtUnits))
{
currentAttacker->printUnit();
std::cout << " " << currentAction << " [" << currentFoughtUnits << "]" << std::endl;
// Nullify the pointer
currentAttacker = nullptr;
}
}
void Game::printAll()
{
std::cout << std::endl;
std::cout << "Current Timestep " << currentTimestep << std::endl;
std::cout << "============== Earth Army Alive Units =========================" << std::endl;
earthArmy.printArmy();
std::cout << std::endl << "============== Alien Army Alive Units =========================" << std::endl;
alienArmy.printArmy();
std::cout << std::endl << "============== Earth Allied Army Alive Units ===================" << std::endl;
earthAlliedArmy.printArmy();
std::cout << std::endl << (attackers.isEmpty() ? "============== No units fighting at current step ==============" : "============== Units fighting at current step =================") << std::endl;
printUnitsFighting();
std::cout << std::endl << "============== Maintenance List Units =========================" << std::endl;
printUnitMaintenanceList();
std::cout << std::endl << "============== Killed/Destructed Units ========================" << std::endl;
printKilledList();
}
GameStatistics Game::countStatistics()
{
GameStatistics gameStatistics = { 0 };
// Earth Army Statistics
const int earthUnitTypesCount = 4;
UnitType earthUnitTypes[] = { UnitType::ES, UnitType::ET, UnitType::EG, UnitType::EH };
countArmyStatistics(gameStatistics, ArmyType::EARTH, earthUnitTypes, earthUnitTypesCount);
// Alien Army Statistics
const int alienUnitTypesCount = 3;
UnitType alienUnitTypes[] = { UnitType::AS, UnitType::AM, UnitType::AD };
countArmyStatistics(gameStatistics, ArmyType::ALIEN, alienUnitTypes, alienUnitTypesCount);
// Allied Army Statistics
const int alliedUnitTypesCount = 1;
UnitType alliedUnitTypes[] = { UnitType::SU };
countArmyStatistics(gameStatistics, ArmyType::EARTH_ALLIED, alliedUnitTypes, alliedUnitTypesCount);
// Killed Units Statistics
countKilledUnitsStatistics(gameStatistics);
return gameStatistics;
}
void Game::countArmyStatistics(GameStatistics& gameStatistics, ArmyType armyType, UnitType unitTypes[], int unitTypesCount)
{
// Count the statistics of the given army
Unit* unit = nullptr;
for (int i = 0; i < unitTypesCount; i++)
{
int count = getUnitsCount(armyType, unitTypes[i]);
for (int j = 0; j < count; j++)
{
// Remove the unit from the army
unit = removeUnit(armyType, unitTypes[i]);
// Unit Counts
gameStatistics.unitCounts[unitTypes[i]]++;
gameStatistics.totalUnitsCount++;
// Total Army Units Count
gameStatistics.armyStatistics[armyType].totalUnitsCount++;
// Count the healed units
HealableUnit* healableUnit = dynamic_cast<HealableUnit*>(unit);
if (healableUnit && healableUnit->hasBeenHealedBefore())
gameStatistics.totalHealedUnits++;
// Count the infected Earth Soldiers
if (unitTypes[i] == UnitType::ES)
{
EarthSoldier* earthSoldier = dynamic_cast<EarthSoldier*>(unit);
if (earthSoldier->isInfected() || earthSoldier->isImmune()) // Check if the unit is infected or immune (has been infected before)
gameStatistics.totalInfectedESCount++;
}
// Add the unit back to the army
addUnit(unit);
// Nullify the pointer
unit = nullptr;
}
}
}
void Game::countKilledUnitsStatistics(GameStatistics& gameStatistics)
{
Unit* unit = nullptr;
int count = killedList.getCount();
for (int i = 0; i < count; i++)
{
// Remove the unit from the killed list
killedList.dequeue(unit);
// Get the unit type and army type
ArmyType armyType = unit->getArmyType();
UnitType unitType = unit->getUnitType();
// Unit Counts
gameStatistics.unitCounts[unitType]++;
gameStatistics.totalUnitsCount++;
// Destructed Unit Counts
gameStatistics.destructedUnitCounts[unitType]++;
gameStatistics.totalDestructedUnitsCount++;
// Total Army Units Count
gameStatistics.armyStatistics[armyType].totalUnitsCount++;
gameStatistics.armyStatistics[armyType].totalDestructedUnitsCount++;
// Count the healed units
HealableUnit* healableUnit = dynamic_cast<HealableUnit*>(unit);
if (healableUnit && healableUnit->hasBeenHealedBefore())
gameStatistics.totalHealedUnits++;
// Count the infected Earth Soldiers
if (unitType == UnitType::ES)
{
EarthSoldier* earthSoldier = dynamic_cast<EarthSoldier*>(unit);
if (earthSoldier->isInfected() || earthSoldier->isImmune()) // Check if the unit is infected or immune (has been infected before)
gameStatistics.totalInfectedESCount++;
}
// Delays
gameStatistics.armyStatistics[armyType].totalFirstAttackDelays += unit->getFirstAttackDelay();
gameStatistics.armyStatistics[armyType].totalBattleDelays += unit->getBattleDelay();
gameStatistics.armyStatistics[armyType].totalDestructionDelays += unit->getDestructionDelay();
// Add the unit back to the killed list
killedList.enqueue(unit);
// Nullify the pointer
unit = nullptr;
}
}
void Game::generateOutputFile(const std::string& outputFileName)
{
// Open the output file
std::ofstream fout(outputFileName);
// Count the statistics
GameStatistics gameStatistics = countStatistics();
// Print decorated the battle results
fout << "======================================================================" << std::endl;
fout << std::right << std::setw(40);
fout << "Battle Results" << std::endl;
fout << "======================================================================" << std::endl;
fout << "Battle Result: " << battleResult() << std::endl;
fout << "Total Timesteps: " << currentTimestep << std::endl;
// Print the killed units
fout << std::endl;
fout << "======================================================================" << std::endl;
fout << std::right << std::setw(40);
fout << "Killed Units" << std::endl;
fout << "======================================================================" << std::endl;
// Print the header
fout << std::left;
fout << std::setw(12) << "Td";
fout << std::setw(12) << "ID";
fout << std::setw(12) << "Tj";
fout << std::setw(12) << "Df";
fout << std::setw(12) << "Dd";
fout << std::setw(12) << "Db" << std::endl;
// Print the units
Unit* killedUnit = nullptr;
for (int i = 0; i < gameStatistics.totalDestructedUnitsCount; i++)
{
killedList.dequeue(killedUnit);
fout << std::setw(12) << killedUnit->getDestructionTime();
fout << std::setw(12) << killedUnit->getId();
fout << std::setw(12) << killedUnit->getJoinTime();
fout << std::setw(12) << killedUnit->getFirstAttackDelay();
fout << std::setw(12) << killedUnit->getDestructionDelay();
fout << std::setw(12) << killedUnit->getBattleDelay() << std::endl;
killedList.enqueue(killedUnit);
}
// Earth Army Statistics
fout << std::endl;
fout << "======================================================================" << std::endl;
fout << std::right << std::setw(45);
fout << "Earth Army Statistics" << std::endl;
fout << "======================================================================" << std::endl;
fout << "Total ES Count: " << gameStatistics.unitCounts[UnitType::ES] << std::endl;
fout << "Total ET Count: " << gameStatistics.unitCounts[UnitType::ET] << std::endl;
fout << "Total EG Count: " << gameStatistics.unitCounts[UnitType::EG] << std::endl;
fout << "Total EH Count: " << gameStatistics.unitCounts[UnitType::EH] << std::endl;
fout << "======================================================================" << std::endl;
fout << "Destructed ESs/Total ESs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::ES], gameStatistics.unitCounts[UnitType::ES]) << "%" << std::endl;
fout << "Destructed ETs/Total ETs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::ET], gameStatistics.unitCounts[UnitType::ET]) << "%" << std::endl;
fout << "Destructed EGs/Total EGs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::EG], gameStatistics.unitCounts[UnitType::EG]) << "%" << std::endl;
fout << "Destructed EHs/Total EHs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::EH], gameStatistics.unitCounts[UnitType::EH]) << "%" << std::endl;
fout << "Total Destructed Earth Units/Total Earth Units = " << calculatePercentage(gameStatistics.armyStatistics[ArmyType::EARTH].totalDestructedUnitsCount, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << "%" << std::endl;
fout << "Total Healed Units/Total Earth Units = " << calculatePercentage(gameStatistics.totalHealedUnits, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << "%" << std::endl;
fout << "Total Infected ESs/Total Earth Units = " << calculatePercentage(gameStatistics.totalInfectedESCount, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << "%" << std::endl;
fout << "======================================================================" << std::endl;
fout << "Average of First Attack Delay = " << calculateRatio(gameStatistics.armyStatistics[ArmyType::EARTH].totalFirstAttackDelays, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << std::endl;
fout << "Average of Destruction Delay = " << calculateRatio(gameStatistics.armyStatistics[ArmyType::EARTH].totalDestructionDelays, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << std::endl;
fout << "Average of Battle Delay = " << calculateRatio(gameStatistics.armyStatistics[ArmyType::EARTH].totalBattleDelays, gameStatistics.armyStatistics[ArmyType::EARTH].totalUnitsCount) << std::endl;
fout << "======================================================================" << std::endl;
fout << "Df/Db = " << calculatePercentage(gameStatistics.armyStatistics[ArmyType::EARTH].totalFirstAttackDelays, gameStatistics.armyStatistics[ArmyType::EARTH].totalBattleDelays) << "%" << std::endl;
fout << "Dd/Db = " << calculatePercentage(gameStatistics.armyStatistics[ArmyType::EARTH].totalDestructionDelays, gameStatistics.armyStatistics[ArmyType::EARTH].totalBattleDelays) << "%" << std::endl;
// Alien Army Statistics
fout << std::endl;
fout << "======================================================================" << std::endl;
fout << std::right << std::setw(45);
fout << "Alien Army Statistics" << std::endl;
fout << "======================================================================" << std::endl;
fout << "Total AS Count: " << gameStatistics.unitCounts[UnitType::AS] << std::endl;
fout << "Total AM Count: " << gameStatistics.unitCounts[UnitType::AM] << std::endl;
fout << "Total AD Count: " << gameStatistics.unitCounts[UnitType::AD] << std::endl;
fout << "======================================================================" << std::endl;
fout << "Destructed ASs/Total ASs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::AS], gameStatistics.unitCounts[UnitType::AS]) << "%" << std::endl;
fout << "Destructed AMs/Total AMs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::AM], gameStatistics.unitCounts[UnitType::AM]) << "%" << std::endl;
fout << "Destructed ATs/Total ATs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::AD], gameStatistics.unitCounts[UnitType::AD]) << "%" << std::endl;
fout << "Total Destructed Alien Units/Total Alien Units = " << calculatePercentage(gameStatistics.armyStatistics[ArmyType::ALIEN].totalDestructedUnitsCount, gameStatistics.armyStatistics[ArmyType::ALIEN].totalUnitsCount) << "%" << std::endl;
fout << "======================================================================" << std::endl;
fout << "Average of First Attack Delay = " << calculateRatio(gameStatistics.armyStatistics[ArmyType::ALIEN].totalFirstAttackDelays, gameStatistics.armyStatistics[ArmyType::ALIEN].totalUnitsCount) << std::endl;
fout << "Average of Destruction Delay = " << calculateRatio(gameStatistics.armyStatistics[ArmyType::ALIEN].totalDestructionDelays, gameStatistics.armyStatistics[ArmyType::ALIEN].totalUnitsCount) << std::endl;
fout << "Average of Battle Delay = " << calculateRatio(gameStatistics.armyStatistics[ArmyType::ALIEN].totalBattleDelays, gameStatistics.armyStatistics[ArmyType::ALIEN].totalUnitsCount) << std::endl;
fout << "======================================================================" << std::endl;
fout << "Df/Db = " << calculatePercentage(gameStatistics.armyStatistics[ArmyType::ALIEN].totalFirstAttackDelays, gameStatistics.armyStatistics[ArmyType::ALIEN].totalBattleDelays) << "%" << std::endl;
fout << "Dd/Db = " << calculatePercentage(gameStatistics.armyStatistics[ArmyType::ALIEN].totalDestructionDelays, gameStatistics.armyStatistics[ArmyType::ALIEN].totalBattleDelays) << "%" << std::endl;
// Allied Army Statistics
fout << std::endl;
fout << "======================================================================" << std::endl;
fout << std::right << std::setw(45);
fout << "Earth Allied Army Statistics" << std::endl;
fout << "======================================================================" << std::endl;
fout << "Total SU Count: " << gameStatistics.unitCounts[UnitType::SU] << std::endl;
fout << "======================================================================" << std::endl;
fout << "Destructed SUs/Total SUs = " << calculatePercentage(gameStatistics.destructedUnitCounts[UnitType::SU], gameStatistics.unitCounts[UnitType::SU]) << "%" << std::endl;
fout << "======================================================================" << std::endl;
fout << "Average of Battle Delay = " << calculateRatio(gameStatistics.armyStatistics[ArmyType::EARTH_ALLIED].totalBattleDelays, gameStatistics.armyStatistics[ArmyType::EARTH_ALLIED].totalUnitsCount) << std::endl;
// Close the output file
fout.close();
}
bool Game::loadParameters(const std::string& fileName)
{
std::fstream fin(fileName);
if (fin.is_open())
{
int N = 0;
int prob = 0;
int ESPercentage = 0;
int ETPercentage = 0;
int EGPercentage = 0;
int EHPercentage = 0;
int ASPercentage = 0;
int AMPercentage = 0;
int ADPercentage = 0;
Range earthPowerRange = { 0, 0 };
Range earthHealthRange = { 0, 0 };
Range earthAttackCapacityRange = { 0, 0 };
Range alienPowerRange = { 0, 0 };
Range alienHealthRange = { 0, 0 };
Range alienAttackCapacityRange = { 0, 0 };
Range earthAlliedPowerRange = { 0, 0 };
Range earthAlliedHealthRange = { 0, 0 };
Range earthAlliedAttackCapacityRange = { 0, 0 };
int infectingProbability = 0;
int infectionThreshold = 0;
fin >> N >> ESPercentage >> ETPercentage >> EGPercentage >> EHPercentage >> ASPercentage >> AMPercentage >> ADPercentage >> prob;
char dummyHyphen = '\0'; // Dummy variable to read the hyphen
fin >> earthPowerRange.min >> dummyHyphen >> earthPowerRange.max;
fin >> earthHealthRange.min >> dummyHyphen >> earthHealthRange.max;
fin >> earthAttackCapacityRange.min >> dummyHyphen >> earthAttackCapacityRange.max;
fin >> alienPowerRange.min >> dummyHyphen >> alienPowerRange.max;
fin >> alienHealthRange.min >> dummyHyphen >> alienHealthRange.max;
fin >> alienAttackCapacityRange.min >> dummyHyphen >> alienAttackCapacityRange.max;
fin >> infectingProbability;
fin >> earthAlliedPowerRange.min >> dummyHyphen >> earthAlliedPowerRange.max;
fin >> earthAlliedHealthRange.min >> dummyHyphen >> earthAlliedHealthRange.max;
fin >> earthAlliedAttackCapacityRange.min >> earthAlliedAttackCapacityRange.max;
fin >> infectionThreshold;
randomGenerator.setN(N); // Set the number of units to generate
randomGenerator.setProb(prob); // Set the probability of generating a unit
randomGenerator.setEarthParameters(ESPercentage, EGPercentage, ETPercentage, EHPercentage, earthPowerRange, earthHealthRange, earthAttackCapacityRange); // Set the parameters for the Earth army
randomGenerator.setAlienParameters(ASPercentage, AMPercentage, ADPercentage, alienPowerRange, alienHealthRange, alienAttackCapacityRange); // Set the parameters for the Alien army
randomGenerator.setEarthAlliedParameters(earthAlliedPowerRange, earthAlliedHealthRange, earthAlliedAttackCapacityRange);
AlienMonster::setInfectingProbability(infectingProbability); // Set the infecting probability for the Alien Monster
EarthArmy::setInfectionThreshold(infectionThreshold); // Set the infection threshold percentage for the Earth Army
fin.close(); // Close the file
return true; // File loaded successfully
}
return false; // File failed to load
}
int Game::getCurrentTimestep() const
{
return currentTimestep;
}
int Game::getUnitsCount(ArmyType armyType, UnitType unitType) const
{
// Get the count of current alive units in their armies lists
switch (armyType)
{
case ArmyType::EARTH:
return earthArmy.getUnitsCount(unitType);
case ArmyType::ALIEN:
return alienArmy.getUnitsCount(unitType);
case ArmyType::EARTH_ALLIED:
return earthAlliedArmy.getUnitsCount(unitType);
}
return 0;
}
int Game::getInfectedUnitsCount() const
{
return earthArmy.getInfectedSoldiersCount();
}
Game::~Game()
{
Unit* unit = nullptr;
int dummyPri = 0;
// Delete the units in the killed list
while (killedList.dequeue(unit))
{
delete unit;
unit = nullptr;
}
// Delete the units in the maintenance list
HealableUnit* healableUnit = nullptr;
while (unitMaintenanceList.dequeue(healableUnit, dummyPri))
{
delete healableUnit;
healableUnit = nullptr;
}
}