-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGame.cpp
More file actions
519 lines (440 loc) · 10.2 KB
/
Game.cpp
File metadata and controls
519 lines (440 loc) · 10.2 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
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
#include "Game.h"
Game::Game(char _mode)
{
earthArmy = new EarthArmy();
alienArmy = new AlienArmy();
allyArmy = new AllyArmy();
randGenerator = new randGen(this);
mode = _mode;
game_status = 0;
timeStep = 0;
for (int i = 0; i < Unit::LastType; ++i) {
DestructedUnitsCount[i] = 0;
}
}
Game::~Game()
{
delete earthArmy;
delete alienArmy;
delete allyArmy;
delete randGenerator;
delete logger;
Unit* unit;
int dummy;
while (Killed.dequeue(unit) && unit) delete unit;
while (UML.dequeue(unit, dummy) && unit) delete unit;
}
void Game::PlayGame()
{
string s = "";
while (GetCurrentTimeStep() < 40 || checkGameStatus() == 0) {
if (s == "skip") {
mode = 's';
}
NextTimeStep();
if (mode == 'i') {
cout << "Press any key to move to next timestep (type skip to switch to silence mode)" << endl;
getline(cin, s);
}
}
int game_status = checkGameStatus();
if (game_status != 0) {
// Kill all units in UML
while (!UML.isEmpty()) {
Unit* unit = nullptr;
int p;
UML.dequeue(unit, p);
AddToKilledList(unit);
}
}
LogGameResult();
//In Silent Don't print this
if (game_status == 1) {
cout << endl << "Earth Army Won" << endl;
}
else if (game_status == -1) {
cout << endl << "Aliens Army Won" << endl;
}
else if (game_status == 2) {
cout << endl << "TIE" << endl;
}
cout << "Simulation ends, Output file is created" << endl;
}
void Game::LogGameResult()
{
logger->LogEarthArmy(earthArmy);
logger->LogAlienArmy(alienArmy);
logger->LogGameStatus();
}
char Game::GetGameMode()
{
return mode;
}
int Game::checkGameStatus()
{
int e_count = earthArmy->GetArmyCount();
int a_count = alienArmy->GetArmyCount();
if (e_count == 0 && a_count != 0)
game_status = -1; // aliens won
else if (a_count == 0 && e_count != 0)
game_status = 1; // earth won
else if ((e_count == 0 && a_count == 0) || !canArmiesAttack()) {
game_status = 2; // tie
}
return game_status;
}
void Game::NextTimeStep()
{
timeStep++;
if(timeStep >= 10)
CheckingUML();
bool retreated = false;
if (earthArmy->GetInfectedCount() == 0) {
retreated = allyArmy->ArmyRetreat();
}
randGenerator->execute();
// Printing
if (mode == 'i') {
cout << "\nCurrent Timestep " << timeStep << endl;
if (retreated) {
cout << endl << "Ally Army retreated" << endl << endl;
}
// Printing alive lists
earthArmy->Print();
alienArmy->Print();
allyArmy->Print();
cout << "============================== Units Fighting At Current Step =====================" << endl;
}
// Printing ongoing fights
earthArmy->Attack();
earthArmy->SpreadInfection();
allyArmy->Attack();
alienArmy->Attack();
// Printing dead units
if (mode == 'i')
{
cout << endl;
PrintUML();
PrintKilledList();
}
}
int Game::GetCurrentTimeStep() const
{
return timeStep;
}
int Game::getNextUnitId(char army)
{
if (army == 'a')
return alienArmy->getNextUnitId();
else if(army=='s')
{
return allyArmy->getNextUnitId();
}
return earthArmy->getNextUnitId();
}
int Game::getTj()
{
return timeStep;
}
Game* Game::GetGamePtr()
{
return this;
}
bool Game::canArmiesAttack()
{
if (earthArmy->GetUnitCount(Unit::ES) > 0
&& alienArmy->GetUnitCount(Unit::AS) > 0) { // ES attacks AS (& vv)
return true;
}
if (earthArmy->GetUnitCount(Unit::ET) > 0
&& (alienArmy->GetUnitCount(Unit::AM) > 0
|| alienArmy->GetUnitCount(Unit::AS) > 0)) { // ET attacks AM & AS
return true;
}
if (earthArmy->GetUnitCount(Unit::EG) > 0
&& (alienArmy->GetUnitCount(Unit::AM) > 0
|| alienArmy->GetUnitCount(Unit::AD) > 0)) { // EG attacks AM & AD
return true;
}
if (alienArmy->GetUnitCount(Unit::AM) > 0
&& (earthArmy->GetUnitCount(Unit::ET) > 0
|| earthArmy->GetUnitCount(Unit::ES) > 0)) { // AM attacks ET & ES
return true;
}
if (alienArmy->GetUnitCount(Unit::AD) > 0
&& (earthArmy->GetUnitCount(Unit::ET) > 0
|| earthArmy->GetUnitCount(Unit::EG) > 0)) { // AD attacks ET & EG
return true;
}
// otherwise, none can hit the other
return false;
}
bool Game::CanEtAttackAs()
{
double currentRatio = GetRatio();
if (ETapproval) {
if (currentRatio >= 80.0) ETapproval = false;
}
else {
if (currentRatio < 30.0) ETapproval = true;
}
return ETapproval;
}
double Game::GetRatio()
{
if(alienArmy->GetUnitCount(Unit::AS) != 0.0)
return (double(earthArmy->GetUnitCount(Unit::ES)) / double(alienArmy->GetUnitCount(Unit::AS))) * 100.0;
return 100.0;
}
void Game::addUnit(Unit* unit, bool newUnit)
{
if (!unit) return;
if(unit->getType() == Unit::EG || unit->getType() == Unit::ES || unit->getType() == Unit::ET|| unit->getType() == Unit::HU)
earthArmy->AddUnit(unit, newUnit);
else if (unit->getType() == Unit::SU) {
allyArmy->AddUnit(unit, newUnit);
}
else alienArmy->AddUnit(unit, false, newUnit);
}
void Game::AddToKilledList(Unit* unit)
{
DestructedUnitsCount[unit->getType()]++;
if(unit->getType() == Unit::HU)
unit->TakeDamage(unit->GetHealth()); // to actually kill the healer
unit->SetDestructionTime(timeStep);
Killed.enqueue(unit);
logger->LogUnit(unit);
}
int Game::GetDestructedUnitCount(Unit::UnitType type)
{
return DestructedUnitsCount[type];
}
Unit* Game::pickAlienunit(Unit::UnitType type,bool fromBack)
{
switch (type)
{
case(Unit::AS):
{
return alienArmy->RemoveUnit(Unit::AS);
break;
}case(Unit::AM):
{
return alienArmy->RemoveUnit(Unit::AM);
break;
}
case(Unit::AD):
{
return alienArmy->RemoveUnit(Unit::AD, fromBack);
break;
}
}
return nullptr;
}
void Game::ReturnAlienUnit(Unit* r)
{
alienArmy->AddUnit(r);
}
double Game::GetInfectionProb()
{
return alienArmy->GetInfectionProb();
}
void Game::AddInfectedCountTotal()
{
earthArmy->AddInfectedCountTotal();
}
double Game::GetInfectedRatio()
{
int escount = earthArmy->GetUnitCount(Unit::ES);
int infectedcount = earthArmy->GetInfectedCount();
if (escount == 0)
return 0.0;
else
{
double ratio = double(infectedcount) / double(escount);
return ratio*100.0;
}
}
Unit* Game::PickEarthUnit(Unit::UnitType type)
{
switch (type)
{
case (Unit::EG):
{
return earthArmy->RemoveUnit(Unit::EG);
break;
}
case (Unit::ES):
{
return earthArmy->RemoveUnit(Unit::ES);
break;
}
case (Unit::ET):
{
return earthArmy->RemoveUnit(Unit::ET);
break;
}
}
return nullptr;
}
void Game::ReturnEarthUnit(Unit* r)
{
earthArmy->AddUnit(r);
}
Unit* Game::PickAllyUnit()
{
Unit *unit =nullptr;
unit=allyArmy->RemoveUnit();
return unit;
}
void Game::ReturnAllyUnit(Unit* r)
{
allyArmy->AddUnit(r);
}
Unit* Game::GetFromUML()
{
Unit* p = nullptr;
int x;
if (UML.dequeue(p, x))
return p;
else
return nullptr;
}
void Game::ReturnToUML(Unit* unit)
{
//// use for tanks and earth solider only //////////
if (!unit) return;
float pri = 0;
if (unit->getType() == Unit::ES) {
pri = 100 - unit->GetHealth();
UML.enqueue(unit, pri);
}
if (unit->getType() == Unit::ET) {
UML.enqueue(unit, pri);
}
}
void Game::IncrementHealedUnitCount()
{
++HealedUnitCount;
}
int Game::GetHealedUnitCount() const
{
return HealedUnitCount;
}
bool Game::canAddUnit(char army)
{
if(army == 'a')
return alienArmy->canAddUnit();
else if(army == 'e')
return earthArmy->canAddUnit();
else
return allyArmy->canAddUnit();
}
void Game::AddToUML(Unit* unit)
{
//// use for tanks and earth solider only //////////
if (!unit) return;
float pri = 0;
if (unit->getType()==Unit::ES) {
pri = 100 - unit->GetHealth();
unit->SetHealtime(this->GetCurrentTimeStep());
UML.enqueue(unit, pri);
}
if (unit->getType() == Unit::ET) {
unit->SetHealtime(this->GetCurrentTimeStep());
UML.enqueue(unit, pri);
}
}
void Game::CheckingUML()
{
/// checking for the 10 time step ////////////////
/// used when you next time step /////////////////
if (UML.isEmpty()) return;
priQueue<Unit*>tempcheck;
while (!UML.isEmpty())
{
int p;
Unit* ptr;
UML.dequeue(ptr, p);
if (abs(ptr->GetHealtime() - this->GetCurrentTimeStep()) >= 10) {
AddToKilledList(ptr);
}
else {
tempcheck.enqueue(ptr, p);
}
}
while (!tempcheck.isEmpty())
{
int p;
Unit* ptr;
tempcheck.dequeue(ptr, p);
UML.enqueue(ptr, p);
}
}
void Game::PrintKilledList() {
cout << "============================== Killed/Destructed Units =============================" << endl;
cout << Killed.GetCount() << " units [";
Killed.Print();
cout << "] " << endl << endl;
}
void Game::PrintUML() {
cout << "============================== Unit Maintenance List (UML) =========================" << endl;
cout << UML.GetCount() << " units [";
UML.Print();
cout << "] " << endl << endl;
}
void Game::loadFile(int& N, int& Prob, EarthArmyConfig* eParams, AlienArmyConfig* aParams, AllyArmyConfig* allyParams)
{
cout << "Please, Enter the Name of the input file: ";
cin >> file;
// check if the file name has .txt extension
if (file.find(".txt") == string::npos) {
file += ".txt";
}
file = "Input/" + file;
cin.ignore();
ifstream inFile(file);
while (!inFile.is_open()) {
cout << "Couldn't Find it\nPlease Enter Your File name Correctly: ";
cin >> file;
if (file.find(".txt") == string::npos) {
file += ".txt";
}
file = "Input/" + file;
cin.ignore();
inFile.open(file);
}
double infectionProb;
inFile >> N
>> eParams->ES >> eParams->ET >> eParams->EG >> eParams->HU
>> aParams->AS >> aParams->AM >> aParams->AD
>> Prob >> allyParams->threshold >> infectionProb
>> eParams->ePowCeil >> eParams->ePowFloor
>> eParams->eHealCeil >> eParams->eHealFloor
>> eParams->eCapCeil >> eParams->eCapFloor
>> aParams->aPowCeil >> aParams->aPowFloor
>> aParams->aHealCeil >> aParams->aHealFloor
>> aParams->aCapCeil >> aParams->aCapFloor
>>allyParams->allyPowCeil >> allyParams->allyPowFloor
>> allyParams->allyHealCeil >> allyParams->allyHealFloor
>> allyParams->allyCapCeil >> allyParams->allyCapFloor;
eParams->ePowFloor *= -1;
eParams->eHealFloor *= -1;
eParams->eCapFloor *= -1;
aParams->aPowFloor *= -1;
aParams->aHealFloor *= -1;
aParams->aCapFloor *= -1;
allyParams->allyPowFloor *= -1;
allyParams->allyHealFloor *= -1;
allyParams->allyCapFloor *= -1;
inFile.close();
alienArmy->SetInfectionProb(infectionProb);
string output_file;
cout << "Enter Output File Name: ";
cin >> output_file;
// check if the file name has .txt extension
if (output_file.find(".txt") == string::npos) {
output_file += ".txt";
}
cin.ignore();
logger = new OutputLogger("Output/" + output_file, this);
}