-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrders.cpp
More file actions
771 lines (626 loc) · 21.2 KB
/
Orders.cpp
File metadata and controls
771 lines (626 loc) · 21.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
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#include "Orders.h"
#include "LoggingObserver.h"
#include <stdexcept>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <typeinfo>
#include <random>
using namespace std;
// Orders Base Class Implementation ----------------------------------------------------------------
Order::Order() : name("Order"), executed(false), effect("None") {} // Default constructor
Order::Order(const std::string& name) : name(name), executed(false), effect("None") {} // Parameterized constructor
Order::Order(const Order& other) : name(other.name), executed(other.executed), effect(other.effect) {} // Copy constructor
Order& Order::operator=(const Order& other) // Assignment operator
{
if (this != &other)
{
name = other.name;
executed = other.executed;
effect = other.effect;
}
return *this;
}
Order::~Order() {} // Destructor
std::string Order::toString() const // Converts order details to string
{
return "Order: " + name + ", Executed: " + (executed ? "Yes" : "No") + ", Effect: " + effect;
}
bool Order::isExecuted() const // Checks if order has been executed
{
return executed;
}
void Order::setEffect(const std::string& eff) // Sets the effect description
{
this->effect = eff;
}
void Order::setName(const std::string& name) // Sets the name of the order
{
this->name = name;
}
void Order::setExecuted(bool executed) // Sets the executed status
{
this->executed = executed;
}
std::ostream& operator<<(std::ostream& os, const Order& order) // Stream insertion operator
{
os << order.toString();
if (order.executed)
{
os << " Effect: " << order.effect;
}
return os;
};
std::string Order::stringToLog() const {
return "Order: " + name + " | Executed: " + (executed ? "Yes" : "No") + " | Effect: " + effect;
}
string Order::getName() const {
return name;
}
// Deploy Class Implementation ---------------------------------------------------------------------
Deploy::Deploy() : Order("Deploy"), issuer(nullptr), target(nullptr), armies(0) {} // Default constructor
Deploy::Deploy(Player* issuer, Territory* target, int armies)
: Order("Deploy"), issuer(issuer), target(target), armies(armies) {
}; // Parameterized constructor
Deploy::Deploy(const Deploy& other)
: Order(other), issuer(other.issuer), target(other.target), armies(other.armies) {
}; // Copy constructor
Deploy& Deploy::operator=(const Deploy& other) // Assignment operator
{
if (this != &other)
{
Order::operator=(other);
issuer = other.issuer;
target = other.target;
armies = other.armies;
}
return *this;
}
Deploy::~Deploy() {} // Destructor
bool Deploy::validate() const // Validates the Deploy order
{
if (!(issuer && target) || armies <= 0)
return false;
// must deploy on your own territory
return (target->getOwner() == issuer);
}
void Deploy::execute() // Executes the Deploy order
{
if (!validate()) {
setEffect("Deploy invalid (must own target / bad armies).");
setExecuted(false);
std::cout << *this << std::endl;
Notify(this);
return;
}
target->setArmies(target->getArmies() + armies);
setEffect("deployed " + std::to_string(armies) + " to " + target->getName());
setName("Deploy (executed)");
setExecuted(true);
std::cout << *this << std::endl;
Notify(this);
}
Order* Deploy::clone() const // Virtual constructor
{
return new Deploy(*this);
}
std::string Deploy::toString() const //Converts Deploy order details to string
{
return Order::toString() + ", Issuer: " + (issuer ? issuer->getName() : "None") +
", Target: " + (target ? target->getName() : "None") + ", Armies: " + to_string(armies);
}
std::ostream& operator<<(std::ostream& os, const Deploy& order)
{
return os << static_cast<const Order&>(order);
}
// Advance Class Implementation --------------------------------------------------------------------
Advance::Advance() : Order("Advance"), issuer(nullptr), source(nullptr), target(nullptr), armies(0) {} // Default constructor
Advance::Advance(Player* issuer, Territory* source, Territory* target, int armies)
: Order("Advance"), issuer(issuer), source(source), target(target), armies(armies) {
} // Parameterized constructor
Advance::Advance(const Advance& other)
: Order(other), issuer(other.issuer), source(other.source), target(other.target), armies(other.armies) {
} // Copy constructor
Advance& Advance::operator=(const Advance& other) // Assignment operator
{
if (this != &other)
{
Order::operator=(other);
issuer = other.issuer;
source = other.source;
target = other.target;
armies = other.armies;
}
return *this;
}
Advance::~Advance() {} // Destructor
bool Advance::validate() const // Validates the Advance order
{
if (!(issuer && source && target) || source == target || armies <= 0)
return false;
// must own the source territory
if (source->getOwner() != issuer)
return false;
// must have enough armies in source
if (source->getArmies() < armies)
return false;
// must be adjacent
if (!source->isAdjacentTo(target))
return false;
// If target is enemy and there is a truce, this attack is invalid
Player* defender = target->getOwner();
if (defender && defender != issuer) {
// Either direction counts, but we store symmetric truces anyway
if (issuer->hasNegotiatedWith(defender) || defender->hasNegotiatedWith(issuer)) {
return false; // attack invalid due to Negotiate
}
}
return true;
}
void Advance::execute() // Executes the Advance order
{
if (!validate()) {
setEffect("Advance invalid (ownership/armies/adjacency).");
setExecuted(false);
std::cout << *this << std::endl;
Notify(this);
return;
}
Player* defender = target->getOwner();
// Friendly move (same owner)
if (defender == issuer) {
source->setArmies(source->getArmies() - armies);
target->setArmies(target->getArmies() + armies);
setEffect("moved " + std::to_string(armies) + " from " + source->getName() + " to " + target->getName());
setName("Advance (executed)");
setExecuted(true);
std::cout << *this << std::endl;
Notify(this);
return;
}
// Hostile move — battle simulation
int atkSent = armies;
source->setArmies(source->getArmies() - atkSent);
int atk = atkSent;
int def = target->getArmies();
std::random_device rd; // Generate random numbers
std::mt19937 gen(rd()); // Mersenne Twister RNG
std::binomial_distribution<int> atkKillsDist(atk, 0.60); // Attacker kills 60% of defending armies
std::binomial_distribution<int> defKillsDist(def, 0.70); // Defender kills 70% of attacking armies
int atkKills = atkKillsDist(gen);
int defKills = defKillsDist(gen);
int defAfter = std::max(0, def - atkKills);
int atkAfter = std::max(0, atk - defKills);
if (defender != NULL)
{
if (defender->getPlayerStrategy()->getStrategyString() == "Neutral") {
defender->setPlayerStrategy(new AggressivePlayerStrategy(defender));
}
}
if (defAfter == 0 && atkAfter > 0) {
// conquer: survivors occupy, transfer ownership
target->setArmies(atkAfter);
if (defender != NULL)
{
target->getOwner()->removeTerritory(target);
}
target->setOwner(issuer);
issuer->addTerritory(target);
setEffect("attacked " + target->getName() + ": conquered with " + std::to_string(atkAfter) + " surviving.");
}
else {
// defender holds
target->setArmies(defAfter);
setEffect("attacked " + target->getName() + ": failed (def " + std::to_string(defAfter) + " left).");
}
setName("Advance (executed)");
setExecuted(true);
std::cout << *this << std::endl;
Notify(this);
}
Order* Advance::clone() const // Virtual constructor
{
return new Advance(*this);
}
std::string Advance::toString() const // Converts Advance order details to string
{
return Order::toString() + ", Issuer: " + (issuer ? issuer->getName() : "None") +
", Source: " + (source ? source->getName() : "None") +
", Target: " + (target ? target->getName() : "None") +
", Armies: " + to_string(armies);
}
std::ostream& operator<<(std::ostream& os, const Advance& order)
{
return os << static_cast<const Order&>(order);
}
Territory* Advance::getSource() const
{
return source;
}
Territory* Advance::getTarget() const
{
return target;
}
// Bomb Class Implementation -----------------------------------------------------------------------
Bomb::Bomb() : Order("Bomb"), issuer(nullptr), target(nullptr) {} // Default constructor
Bomb::Bomb(Player* issuer, Territory* target)
: Order("Bomb"), issuer(issuer), target(target) {
} // Parameterized constructor
Bomb::Bomb(const Bomb& other)
: Order(other), issuer(other.issuer), target(other.target) {
} // Copy constructor
Bomb& Bomb::operator=(const Bomb& other) // Assignment operator
{
if (this != &other)
{
Order::operator=(other);
issuer = other.issuer;
target = other.target;
}
return *this;
}
Bomb::~Bomb() {} // Destructor
bool Bomb::validate() const // Validates the Bomb order
{
if (!(issuer && target))
return false;
Player* tgtOwner = target->getOwner();
if (!tgtOwner || tgtOwner == issuer)
return false; // must target an enemy
// If there is a truce, bombing that player is not allowed
if (issuer->hasNegotiatedWith(tgtOwner) || tgtOwner->hasNegotiatedWith(issuer)) {
return false; // invalid due to Negotiate
}
// must be adjacent to at least one issuer-owned territory
bool adjacentToIssuer = false;
for (Territory* adj : target->getAdjacentTerritories()) {
if (adj && adj->getOwner() == issuer) {
adjacentToIssuer = true;
break;
}
}
if (!adjacentToIssuer)
return false;
return true;
}
void Bomb::execute() // Executes the Bomb order
{
if (!validate()) {
setEffect("Bomb invalid (must target adjacent enemy).");
setExecuted(false);
std::cout << *this << std::endl;
Notify(this);
return;
}
int cur = target->getArmies();
int removed = cur / 2; // remove half (floor)
target->setArmies(cur - removed);
if (target->getOwner()->getPlayerStrategy()->getStrategyString() == "Neutral") {
target->getOwner()->setPlayerStrategy(new AggressivePlayerStrategy(target->getOwner()));
}
setEffect("bombed " + target->getName() + " removing " + std::to_string(removed));
setName("Bomb (executed)");
setExecuted(true);
std::cout << *this << std::endl;
Notify(this);
}
Order* Bomb::clone() const // Virtual constructor
{
return new Bomb(*this);
}
std::string Bomb::toString() const // Converts Bomb order details to string
{
return Order::toString() + ", Issuer: " + (issuer ? issuer->getName() : "None") +
", Target: " + (target ? target->getName() : "None");
}
std::ostream& operator<<(std::ostream& os, const Bomb& order)
{
return os << static_cast<const Order&>(order);
}
// Blockade Class Implementation -------------------------------------------------------------------
Blockade::Blockade() : Order("Blockade"), issuer(nullptr), target(nullptr) {} // Default constructor
Blockade::Blockade(Player* issuer, Territory* target)
: Order("Blockade"), issuer(issuer), target(target) {
} // Parameterized constructor
Blockade::Blockade(const Blockade& other)
: Order(other), issuer(other.issuer), target(other.target) {
} // Copy constructor
Blockade& Blockade::operator=(const Blockade& other) // Assignment operator
{
if (this != &other)
{
Order::operator=(other);
issuer = other.issuer;
target = other.target;
}
return *this;
}
Blockade::~Blockade() {} // Destructor
bool Blockade::validate() const // Validates the Blockade order
{
if (!(issuer && target))
return false;
return (target->getOwner() == issuer);
}
void Blockade::execute() // Executes the Blockade order
{
if (!validate()) {
setEffect("Blockade invalid (must target own territory).");
setExecuted(false);
std::cout << *this << std::endl;
Notify(this);
return;
}
target->setArmies(target->getArmies() * 2);
target->setOwner(nullptr); // neutral player placeholder
issuer->removeTerritory(target);
setEffect("blockaded " + target->getName() + " (doubled, transferred to Neutral)");
setName("Blockade (executed)");
setExecuted(true);
std::cout << *this << std::endl;
Notify(this);
}
Order* Blockade::clone() const // Virtual constructor
{
return new Blockade(*this);
}
std::string Blockade::toString() const // Converts Blockade order details to string
{
return Order::toString() + ", Issuer: " + (issuer ? issuer->getName() : "None") +
", Target: " + (target ? target->getName() : "None");
}
std::ostream& operator<<(std::ostream& os, const Blockade& order)
{
return os << static_cast<const Order&>(order);
}
// Airlift Class Implementation --------------------------------------------------------------------
Airlift::Airlift() : Order("Airlift"), issuer(nullptr), source(nullptr), target(nullptr), armies(0) {} // Default constructor
Airlift::Airlift(Player* issuer, Territory* source, Territory* target, int armies)
: Order("Airlift"), issuer(issuer), source(source), target(target), armies(armies) {
} // Parameterized constructor
Airlift::Airlift(const Airlift& other)
: Order(other), issuer(other.issuer), source(other.source), target(other.target), armies(other.armies) {
} // Copy constructor
Airlift& Airlift::operator=(const Airlift& other) // Assignment operator
{
if (this != &other)
{
Order::operator=(other);
issuer = other.issuer;
source = other.source;
target = other.target;
armies = other.armies;
}
return *this;
}
Airlift::~Airlift() {} // Destructor
bool Airlift::validate() const // Validates the Airlift order
{
if (!(issuer && source && target) || source == target || armies <= 0)
return false;
// must own both territories
if (source->getOwner() != issuer || target->getOwner() != issuer)
return false;
if (source->getArmies() < armies)
return false;
return true;
}
void Airlift::execute()
{
if (!validate()) {
setEffect("Airlift invalid (ownership/armies).");
setExecuted(false);
std::cout << *this << std::endl;
Notify(this);
return;
}
source->setArmies(source->getArmies() - armies);
target->setArmies(target->getArmies() + armies);
setEffect("airlifted " + std::to_string(armies) + " from " + source->getName() + " to " + target->getName());
setName("Airlift (executed)");
setExecuted(true);
std::cout << *this << std::endl;
Notify(this);
}
Order* Airlift::clone() const // Virtual constructor
{
return new Airlift(*this);
}
std::string Airlift::toString() const // Converts Airlift order details to string
{
return Order::toString() + ", Issuer: " + (issuer ? issuer->getName() : "None") +
", Source: " + (source ? source->getName() : "None") +
", Target: " + (target ? target->getName() : "None") +
", Armies: " + to_string(armies);
}
std::ostream& operator<<(std::ostream& os, const Airlift& order)
{
return os << static_cast<const Order&>(order);
}
// Negotiate Class Implementation --------------------------------------------------------------------
Negotiate::Negotiate() : Order("Negotiate"), issuer(nullptr), targetPlayer(nullptr) {} // Default constructor
Negotiate::Negotiate(Player* issuer, Player* targetPlayer)
: Order("Negotiate"), issuer(issuer), targetPlayer(targetPlayer) {
} // Parameterized constructor
Negotiate::Negotiate(const Negotiate& other)
: Order(other), issuer(other.issuer), targetPlayer(other.targetPlayer) {
} // Copy constructor
Negotiate& Negotiate::operator=(const Negotiate& other) // Assignment operator
{
if (this != &other)
{
Order::operator=(other);
issuer = other.issuer;
targetPlayer = other.targetPlayer;
}
return *this;
}
Negotiate::~Negotiate() {} // Destructor
bool Negotiate::validate() const // Validates the Negotiate order
{
return issuer && targetPlayer && (issuer != targetPlayer);
}
void Negotiate::execute() // Executes the Negotiate order
{
if (!validate())
{
setEffect("Negotiate order execution failed: Invalid order.");
setExecuted(false);
cout << *this << endl;
Notify(this);
return;
}
// Register a mutual truce for this turn
issuer->addNegotiatedPlayer(targetPlayer);
targetPlayer->addNegotiatedPlayer(issuer);
setEffect("negotiated temporary peace with " + targetPlayer->getName());
setName("Negotiate (executed)");
setExecuted(true);
cout << *this << endl;
Notify(this);
}
Order* Negotiate::clone() const // Virtual constructor
{
return new Negotiate(*this);
}
std::string Negotiate::toString() const // Converts Negotiate order details to string
{
return Order::toString() + ", Issuer: " + (issuer ? issuer->getName() : "None") +
", Target Player: " + (targetPlayer ? targetPlayer->getName() : "None");
}
std::ostream& operator<<(std::ostream& os, const Negotiate& order)
{
return os << static_cast<const Order&>(order);
}
// OrdersList Class Implementation -----------------------------------------------------------------
OrdersList::OrdersList() : orders() {} // Default constructor
OrdersList::OrdersList(const OrdersList& other) : orders() // Copy constructor
{
deepCopy(other);
}
OrdersList& OrdersList::operator=(const OrdersList& other) // Assignment operator
{
if (this != &other)
{
clear();
deepCopy(other);
}
return *this;
}
OrdersList::~OrdersList() // Destructor
{
clear();
}
void OrdersList::add(const Order& order) // Adds an order to the list (by reference)
{
orders.push_back(order.clone());
Notify(this);
}
void OrdersList::add(Order* order) // Adds an order to the list (by pointer)
{
if (!order) throw std::invalid_argument("Cannot add null order");
orders.push_back(order->clone());
Notify(this);
}
void OrdersList::remove(int index) // Removes an order from the list by index
{
if(index < 0 || index >= orders.size())
{
throw std::out_of_range("Index out of range");
}
delete orders[index];
orders.erase(orders.begin() + index);
Notify(this);
}
void OrdersList::remove(Order* order) // Removes an order from the list by pointer
{
if (!order) return;
auto it = std::find(orders.begin(), orders.end(), order);
if (it != orders.end())
{
delete* it;
orders.erase(it);
Notify(this);
}
}
void OrdersList::move(int fromIndex, int toIndex) // Moves an order within the list
{
if (fromIndex < 0 || fromIndex >= orders.size() || toIndex < 0 || toIndex >= orders.size())
{
throw std::out_of_range("Index out of range");
}
if (fromIndex == toIndex) return; // No need to move if indices are the same
Order* tmp = orders[fromIndex];
orders.erase(orders.begin() + fromIndex);
orders.insert(orders.begin() + toIndex, tmp);
Notify(this);
}
int OrdersList::size() const // Returns the number of orders in the list
{
return orders.size();
}
Order& OrdersList::at(int index) // Returns the order at a specific index
{
if (index < 0 || index >= orders.size())
{
throw std::out_of_range("Index out of range");
}
return *orders[index];
}
void OrdersList::clear() // Clears all orders from the list
{
for(Order* order : orders)
{
delete order;
}
orders.clear();
Notify(this);
}
const Order& OrdersList::at(int index) const // Const version of at()
{
if (index < 0 || index >= orders.size())
{
throw std::out_of_range("Index out of range");
}
return *orders[index];
}
void OrdersList::deepCopy(const OrdersList& other) // Deep copies from another OrdersList
{
orders.reserve(other.orders.size());
for (Order* order : other.orders)
{
orders.push_back(order->clone());
}
}
bool OrdersList::operator==(const OrdersList& other) const
{
if (orders.size() != other.orders.size())
return false;
for (size_t i = 0; i < orders.size(); ++i)
{
const Order* a = orders[i];
const Order* b = other.orders[i];
// same dynamic type?
if (typeid(*a) != typeid(*b))
return false;
// same textual representation?
std::ostringstream osa, osb;
osa << *a;
osb << *b;
if (osa.str() != osb.str())
return false;
}
return true;
}
std::ostream& operator<<(std::ostream& os, const OrdersList& ordersList) // Stream insertion operator, essentially adds itself into the stream, similar to toString()
{
os << "OrdersList with " << ordersList.size() << " orders:\n";
for (int i = 0; i < ordersList.size(); ++i)
{
os << i + 1 << ". " << ordersList.at(i) << "\n";
}
return os;
}
std::string OrdersList::stringToLog() const {
return "OrdersList updated: now contains " + std::to_string(orders.size()) + " orders.";
}