-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerStrategies.cpp
More file actions
1046 lines (859 loc) · 25.8 KB
/
PlayerStrategies.cpp
File metadata and controls
1046 lines (859 loc) · 25.8 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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "PlayerStrategies.h"
#include "Map.h"
#include "Orders.h"
#include "Cards.h"
#include "Player.h"
#include <set>
//PlayerStrategy class implementation
PlayerStrategy::PlayerStrategy(Player* player) //Parameterized Constructor
{
this->player = player;
}
PlayerStrategy::PlayerStrategy(const PlayerStrategy& playerStrategy) //Copy Constructor
{
player = new Player(*playerStrategy.player);
}
PlayerStrategy& PlayerStrategy::operator = (const PlayerStrategy& playerStrategy) //Assignment Operator Overloading
{
if (this != &playerStrategy)
{
delete player;
player = new Player(*playerStrategy.player);
}
return *this;
}
ostream& operator << (ostream& output, const PlayerStrategy& playerStrategy) //Stream Insertion Operator Overloading
{
output << "Strategy Type: " << playerStrategy.getStrategyString() << "\n";
return output;
}
void PlayerStrategy::setPlayer(Player* player)
{
this->player = player;
}
Player* PlayerStrategy::getPlayer()
{
return player;
}
string PlayerStrategy::getStrategyString() const
{
return "Abstract";
}
//HumanPlayerStrategy class implementation
HumanPlayerStrategy::HumanPlayerStrategy(Player* player) : PlayerStrategy(player) {} //Parameterized Constructor
HumanPlayerStrategy::HumanPlayerStrategy(const HumanPlayerStrategy& playerStrategy) : PlayerStrategy(playerStrategy) {} //Copy Constructor
HumanPlayerStrategy& HumanPlayerStrategy::operator = (const HumanPlayerStrategy& playerStrategy) //Assignment Operator Overloading
{
if (this != &playerStrategy)
{
PlayerStrategy::operator=(playerStrategy);
}
return *this;
}
ostream& operator << (ostream& output, const HumanPlayerStrategy& playerStrategy) //Stream Insertion Operator Overloading
{
output << "Strategy Type: Human\n";
return output;
}
HumanPlayerStrategy* HumanPlayerStrategy::clone() //Clone function
{
return new HumanPlayerStrategy(*this);
}
bool HumanPlayerStrategy::issueOrder(Deck* deck)
{
// Basic menu for human interaction
cout << "=== Human Player (" << player->getName() << ") - Issue Order ===\n";
cout << "Reinforcement armies available: " << player->getArmies() << "\n";
cout << "Choose an action:\n";
cout << " 1) Deploy armies\n";
cout << " 2) Advance armies\n";
cout << " 3) Play a card\n";
cout << " 4) Done issuing orders\n";
cout << "Enter choice: ";
int choice;
cin >> choice;
// Clear input errors if any
if (!cin) {
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return false;
}
// Helper lambdas to show territories/cards
auto showTerritories = [this]() {
cout << "\nYour territories:\n";
int idx = 0;
for (Territory* t : player->getTerritories()) {
cout << " [" << idx << "] "
<< t->getName()
<< " (armies: " << t->getArmies() << ")\n";
++idx;
}
};
auto getTerritoryByIndex = [this](int index) -> Territory* {
if (index < 0) return nullptr;
int i = 0;
for (Territory* t : player->getTerritories()) {
if (i == index) return t;
++i;
}
return nullptr;
};
switch (choice) {
case 1: { // Deploy
if (player->getArmies() <= 0) {
cout << "You have no reinforcement armies to deploy.\n";
return false;
}
showTerritories();
cout << "Enter index of territory to deploy to: ";
int idx;
cin >> idx;
Territory* target = getTerritoryByIndex(idx);
if (!target) {
cout << "Invalid territory index.\n";
return false;
}
cout << "Enter number of armies to deploy (max "
<< player->getArmies() << "): ";
int num;
cin >> num;
if (num <= 0 || num > player->getArmies()) {
cout << "Invalid army amount.\n";
return false;
}
cout << "\n";
player->issueOrder(new Deploy(player, target, num));
player->setArmies(player->getArmies() - num);
cout << "Issued Deploy(" << num << " -> " << target->getName() << ")\n\n";
return true;
}
case 2: { // Advance
showTerritories();
cout << "Enter index of source territory: ";
int srcIdx;
cin >> srcIdx;
Territory* src = getTerritoryByIndex(srcIdx);
if (!src) {
cout << "Invalid source index.\n";
return false;
}
if (src->getArmies() <= 1) {
cout << "Source territory must have more than 1 army to advance.\n";
return false;
}
cout << "\nAdjacent territories from " << src->getName() << ":\n";
int idx = 0;
std::vector<Territory*> adjList;
for (Territory* adj : src->getAdjacentTerritories()) {
cout << " [" << idx << "] "
<< adj->getName()
<< " (owner: "
<< (adj->getOwner() ? adj->getOwner()->getName() : "None")
<< ", armies: " << adj->getArmies() << ")\n";
adjList.push_back(adj);
++idx;
}
if (adjList.empty()) {
cout << "No adjacent territories to advance to.\n";
return false;
}
cout << "Enter index of destination territory: ";
int dstIdx;
cin >> dstIdx;
if (dstIdx < 0 || dstIdx >= static_cast<int>(adjList.size())) {
cout << "Invalid destination index.\n";
return false;
}
Territory* dst = adjList[dstIdx];
cout << "Enter number of armies to advance (max "
<< (src->getArmies() - 1) << "): ";
int num;
cin >> num;
if (num <= 0 || num >= src->getArmies()) {
cout << "Invalid army amount.\n";
return false;
}
cout << "\n";
player->issueOrder(new Advance(player, src, dst, num));
cout << "Issued Advance(" << num << " from "
<< src->getName() << " to " << dst->getName() << ")\n\n";
return true;
}
case 3: { // Play card
Hand* hand = player->getHand();
auto& cards = hand->getCards();
if (cards.empty()) {
cout << "You have no cards to play.\n";
return false;
}
cout << "\nYour cards:\n";
int idx = 0;
for (const auto& uptr : cards) {
Card* c = uptr.get();
cout << " [" << idx << "] " << c->getTypeAsString() << "\n";
++idx;
}
cout << "Enter index of card to play: ";
int cardIdx;
cin >> cardIdx;
if (cardIdx < 0 || cardIdx >= static_cast<int>(cards.size())) {
cout << "Invalid card index.\n";
return false;
}
Card* card = cards[cardIdx].get();
std::string type = card->getTypeAsString();
CardPlayContext context; // default context
if (type == "Bomb") {
// choose enemy territory to bomb
cout << "\nChoose enemy territory to bomb:\n";
std::vector<Territory*> enemyTerr;
int i = 0;
list<Territory*> attackList = toAttack();
for (Territory* t : attackList) { // if you have such helper; otherwise build like toAttack()
cout << " [" << i << "] " << t->getName()
<< " (owner: " << t->getOwner()->getName()
<< ", armies: " << t->getArmies() << ")\n";
enemyTerr.push_back(t);
++i;
}
if (enemyTerr.empty()) {
cout << "No enemy territories in range to bomb.\n";
return false;
}
int tIdx;
cin >> tIdx;
if (tIdx < 0 || tIdx >= static_cast<int>(enemyTerr.size())) {
cout << "Invalid target index.\n";
return false;
}
context.target = enemyTerr[tIdx];
}
// ---- REINFORCEMENT ----
else if (type == "Reinforcement") {
cout << "\nChoose a territory to reinforce:\n";
showTerritories();
cout << "Enter index of territory to reinforce: ";
int tIdx;
cin >> tIdx;
Territory* target = getTerritoryByIndex(tIdx);
if (!target) {
cout << "Invalid territory index.\n";
return false;
}
context.target = target;
// Use same value style as your Benevolent strategy
context.armies = 3; // or 5, depending on your card rules
}
// ---- AIRLIFT ----
else if (type == "Airlift") {
cout << "\nChoose source and target territories for Airlift:\n";
showTerritories();
cout << "Enter index of source territory: ";
int srcIdx;
cin >> srcIdx;
Territory* src = getTerritoryByIndex(srcIdx);
if (!src) {
cout << "Invalid source index.\n";
return false;
}
if (src->getArmies() <= 1) {
cout << "Source territory must have more than 1 army to airlift.\n";
return false;
}
cout << "Enter index of target territory: ";
int dstIdx;
cin >> dstIdx;
Territory* dst = getTerritoryByIndex(dstIdx);
if (!dst) {
cout << "Invalid target index.\n";
return false;
}
cout << "Enter number of armies to airlift (max "
<< (src->getArmies() - 1) << "): ";
int num;
cin >> num;
if (num <= 0 || num >= src->getArmies()) {
cout << "Invalid army amount.\n";
return false;
}
context.source = src;
context.target = dst;
context.armies = num;
}
// ---- BLOCKADE ----
else if (type == "Blockade") {
cout << "\nChoose a territory to blockade:\n";
showTerritories();
cout << "Enter index of territory to blockade: ";
int tIdx;
cin >> tIdx;
Territory* target = getTerritoryByIndex(tIdx);
if (!target) {
cout << "Invalid territory index.\n";
return false;
}
context.target = target;
}
// ---- NEGOTIATE ----
else if (type == "Diplomacy") {
// Collect adjacent enemy players
std::set<Player*> enemyPlayersSet;
for (Territory* myTerr : player->getTerritories()) {
for (Territory* adj : myTerr->getAdjacentTerritories()) {
if (adj->getOwner() != nullptr &&
adj->getOwner() != player) {
enemyPlayersSet.insert(adj->getOwner());
}
}
}
if (enemyPlayersSet.empty()) {
cout << "No adjacent enemy players to negotiate with.\n";
return false;
}
std::vector<Player*> enemyPlayers(enemyPlayersSet.begin(), enemyPlayersSet.end());
cout << "\nChoose a player to negotiate with:\n";
for (size_t i = 0; i < enemyPlayers.size(); ++i) {
cout << " [" << i << "] " << enemyPlayers[i]->getName() << "\n";
}
cout << "Enter index of player: ";
int pIdx;
cin >> pIdx;
if (pIdx < 0 || pIdx >= static_cast<int>(enemyPlayers.size())) {
cout << "Invalid player index.\n";
return false;
}
context.targetPlayer = enemyPlayers[pIdx];
}
cout << "\n";
card->play(player, deck, hand, context);
cout << "Played card: " << type << "\n\n";
return true;
}
case 4:
default:
cout << "Done issuing orders for this turn.\n\n";
return false;
}
}
list<Territory*> HumanPlayerStrategy::toDefend()
{
// For a human, just return all owned territories; the player decides in issueOrder.
list<Territory*> defendList;
for (Territory* t : player->getTerritories()) {
defendList.push_back(t);
}
return defendList;
}
list<Territory*> HumanPlayerStrategy::toAttack()
{
// All adjacent enemy territories to any of the human player's territories.
list<Territory*> attackList;
std::set<Territory*> seen;
for (Territory* t : player->getTerritories()) {
for (Territory* adj : t->getAdjacentTerritories()) {
if (adj->getOwner() != player && adj->getOwner() != nullptr) {
if (!seen.count(adj)) {
seen.insert(adj);
attackList.push_back(adj);
}
}
}
}
return attackList;
}
string HumanPlayerStrategy::getStrategyString() const
{
return "Human";
}
//AggressivePlayerStrategy class implementation
AggressivePlayerStrategy::AggressivePlayerStrategy(Player* player) : PlayerStrategy(player) {} //Parameterized Constructor
AggressivePlayerStrategy::AggressivePlayerStrategy(const AggressivePlayerStrategy& playerStrategy) : PlayerStrategy(playerStrategy) {} //Copy Constructor
AggressivePlayerStrategy& AggressivePlayerStrategy::operator = (const AggressivePlayerStrategy& playerStrategy) //Assignment Operator Overloading
{
if (this != &playerStrategy)
{
PlayerStrategy::operator=(playerStrategy);
}
return *this;
}
ostream& operator << (ostream& output, const AggressivePlayerStrategy& playerStrategy) //Stream Insertion Operator Overloading
{
output << "Strategy Type: Aggressive\n";
return output;
}
AggressivePlayerStrategy* AggressivePlayerStrategy::clone() //Clone function
{
return new AggressivePlayerStrategy(*this);
}
bool AggressivePlayerStrategy::issueOrder(Deck* deck)
{
if (player->getTerritories().empty())
{
return false;
}
bool orderIssued = false;
Territory* strongest = toDefend().front(); //Finds the strongest territory owned
if (player->getArmies() > 0) //Checks if there are armies to deploy
{
//Deploys all armies to the strongest territory
player->issueOrder(new Deploy(player, strongest, player->getArmies()));
player->setArmies(0);
orderIssued = true;
}
//Advances all possible armies from adjacent territories to the strongest territory
for (Territory* adjacent : strongest->getAdjacentTerritories())
{
if (adjacent->getOwner() == player && adjacent->getArmies() > 1)
{
player->issueOrder(new Advance(player, adjacent, strongest, (adjacent->getArmies() - 1)));
orderIssued = true;
}
}
list<Territory*> attackList = toAttack(); //Finds all possible territories to attack from the strongest territory
if (!attackList.empty()) //Checks if there are any territories to attack
{
Territory* strongestEnemy = nullptr;
for (Territory* enemy : attackList) //Finds the strongest enemy territory
{
if (strongestEnemy == nullptr || strongestEnemy->getArmies() < enemy->getArmies())
{
strongestEnemy = enemy;
}
}
//Plays all possible Bomb cards on the strongest enemy territory
for (const auto& card : player->getHand()->getCards())
{
if (card->getTypeAsString() == "Bomb")
{
CardPlayContext& context = CardPlayContext();
context.target = strongestEnemy;
card->play(player, deck, player->getHand(), context);
orderIssued = true;
}
}
//Calculates armies to attack with
int armyAmount = strongest->getArmies() / attackList.size();
int armyRemainder = strongest->getArmies() % attackList.size();
//Issues Advance orders to all territories in the attack list
while (!attackList.empty() && strongest->getArmies() > 1 && armyAmount > 0)
{
if (attackList.size() == 1)
{
armyAmount += (armyRemainder - 1);
}
player->issueOrder(new Advance(player, strongest, attackList.front(), armyAmount));
attackList.pop_front();
orderIssued = true;
}
}
return orderIssued;
}
list<Territory*> AggressivePlayerStrategy::toDefend()
{
if (player->getTerritories().empty())
{
return {};
}
bool enemyAdjacent = false;
Territory* strongest = nullptr;
//Iterates through all territories owned by the player
for (Territory* territory : player->getTerritories())
{
if (strongest == nullptr)
{
strongest = territory;
}
bool hasEnemyNeighbours = false;
//Check if territory has any enemy neighbours
for (Territory* adjacent : territory->getAdjacentTerritories())
{
if (adjacent->getOwner() != player)
{
hasEnemyNeighbours = true;
break;
}
}
//Select the strongest territory with enemy neighbours
if (!enemyAdjacent && hasEnemyNeighbours)
{
strongest = territory;
enemyAdjacent = true;
}
else if (strongest->getArmies() < territory->getArmies() && ((enemyAdjacent && hasEnemyNeighbours) || (!enemyAdjacent && !hasEnemyNeighbours)))
{
strongest = territory;
}
}
return {strongest};
}
list<Territory*> AggressivePlayerStrategy::toAttack()
{
if (player->getTerritories().empty())
{
return {};
}
list<Territory*> attackList;
Territory* attackFrom = toDefend().front();
//Iterates through all adjacent territories to find possible attack targets
for (Territory* adjacent : attackFrom->getAdjacentTerritories())
{
//Adds to attack list if the adjacent territory is not owned by the player
if (adjacent->getOwner() != player)
{
attackList.push_back(adjacent);
}
}
return attackList;
}
string AggressivePlayerStrategy::getStrategyString() const
{
return "Aggressive";
}
//BenevolentPlayerStrategy class implementation
BenevolentPlayerStrategy::BenevolentPlayerStrategy(Player* player) : PlayerStrategy(player) {} //Parameterized Constructor
BenevolentPlayerStrategy::BenevolentPlayerStrategy(const BenevolentPlayerStrategy& playerStrategy) : PlayerStrategy(playerStrategy) {} //Copy Constructor
BenevolentPlayerStrategy& BenevolentPlayerStrategy::operator = (const BenevolentPlayerStrategy& playerStrategy) //Assignment Operator Overloading
{
if (this != &playerStrategy)
{
PlayerStrategy::operator=(playerStrategy);
}
return *this;
}
ostream& operator << (ostream& output, const BenevolentPlayerStrategy& playerStrategy) //Stream Insertion Operator Overloading
{
output << "Strategy Type: Benevolent\n";
return output;
}
BenevolentPlayerStrategy* BenevolentPlayerStrategy::clone() //Clone function
{
return new BenevolentPlayerStrategy(*this);
}
bool BenevolentPlayerStrategy::issueOrder(Deck* deck)
{
if (player->getTerritories().empty())
{
return false;
}
bool orderIssued = false;
if (player->getArmies() > 0) //Checks if there are armies to deploy
{
//Calculates armies to deploy to each territory in the defend list
list<Territory*> defendList = toDefend();
int armyAmount = player->getArmies() / defendList.size();
int armyRemainder = player->getArmies() % defendList.size();
//Issues Deploy orders to all territories in the defend list
while (!defendList.empty())
{
if (defendList.size() == 1)
{
armyAmount += armyRemainder;
}
player->issueOrder(new Deploy(player, defendList.front(), armyAmount));
defendList.pop_front();
orderIssued = true;
}
player->setArmies(0);
}
Territory* strongest = nullptr;
//Finds the strongest territory owned
for (Territory* territory : player->getTerritories())
{
if (strongest == nullptr || strongest->getArmies() < territory->getArmies())
{
strongest = territory;
}
}
list<Territory*> defendList = toDefend();
//Plays all possible Reinforcement, Airlift, Blockade and Negotiate cards
for (const auto& card : player->getHand()->getCards())
{
if (!defendList.empty() && strongest == defendList.front())
{
defendList.pop_front();
}
if (card->getTypeAsString() == "Reinforcement")
{
//Plays Reinforcement card on the weakest territory
if (defendList.empty())
{
defendList = toDefend();
}
CardPlayContext& context = CardPlayContext();
context.target = defendList.front();
context.armies = 3;
card->play(player, deck, player->getHand(), context);
defendList.pop_front();
orderIssued = true;
}
else if (card->getTypeAsString() == "Airlift")
{
//Plays Airlift card to move armies to the weakest territory
if (defendList.empty())
{
defendList = toDefend();
}
if (strongest != nullptr)
{
CardPlayContext& context = CardPlayContext();
context.source = strongest;
context.target = defendList.front();
context.armies = strongest->getArmies() / 2;
card->play(player, deck, player->getHand(), context);
defendList.pop_front();
orderIssued = true;
}
}
else if (card->getTypeAsString() == "Blockade")
{
//Plays Blockade card on the weakest territory
if (defendList.empty())
{
defendList = toDefend();
}
CardPlayContext& context = CardPlayContext();
context.target = defendList.front();
card->play(player, deck, player->getHand(), context);
defendList.pop_front();
orderIssued = true;
}
else if (card->getTypeAsString() == "Negotiate")
{
//Plays Negotiate card with an adjacent enemy player
CardPlayContext& context = CardPlayContext();
context.targetPlayer = player;
bool targetPlayerFound = false;
//Finds an adjacent enemy player
for (Territory* territory : player->getTerritories())
{
for (Territory* adjacentTerritory : territory->getAdjacentTerritories())
{
if (adjacentTerritory->getOwner() != player && adjacentTerritory->getOwner() != nullptr)
{
context.targetPlayer = adjacentTerritory->getOwner();
targetPlayerFound = true;
break;
}
}
if (targetPlayerFound)
{
break;
}
}
if (targetPlayerFound)
{
card->play(player, deck, player->getHand(), context);
defendList.pop_front();
orderIssued = true;
}
}
}
list<Territory*> attackList = toAttack();
int armyAmount = player->getArmies() / (attackList.size() + 1);
//Issues Advance orders to all territories neighbouring the strongest territory
while (!attackList.empty())
{
if (armyAmount == 0)
{
break;
}
player->issueOrder(new Advance(player, strongest, attackList.front(), armyAmount));
attackList.pop_front();
orderIssued = true;
}
return orderIssued;
}
list<Territory*> BenevolentPlayerStrategy::toDefend()
{
if (player->getTerritories().empty())
{
return {};
}
Territory* weakest = nullptr;
//Finds the weakest territory owned
for (Territory* territory : player->getTerritories())
{
if (weakest == nullptr || weakest->getArmies() > territory->getArmies())
{
weakest = territory;
}
}
list<Territory*> defendList;
//Finds all territories with the same army count as the weakest territory
for (Territory* territory : player->getTerritories())
{
if (weakest->getArmies() == territory->getArmies())
{
defendList.push_back(territory);
}
}
return defendList;
}
list<Territory*> BenevolentPlayerStrategy::toAttack()
{
if (player->getTerritories().empty())
{
return {};
}
Territory* strongest = nullptr;
//Finds the strongest territory owned
for (Territory* territory : player->getTerritories())
{
if (strongest == nullptr || strongest->getArmies() < territory->getArmies())
{
strongest = territory;
}
}
Territory* weakestNeighbour = nullptr;
//Finds the weakest neighbouring territory
for (Territory* territory : strongest->getAdjacentTerritories())
{
if (territory->getOwner() != player)
{
continue;
}
if (weakestNeighbour == nullptr || weakestNeighbour->getArmies() > territory->getArmies())
{
weakestNeighbour = territory;
}
}
list<Territory*> advanceList;
//Finds all neighbouring territories with the same army count as the weakest neighbouring territory
if (weakestNeighbour != nullptr)
{
for (Territory* territory : strongest->getAdjacentTerritories())
{
if (territory->getOwner() != player)
{
continue;
}
if (weakestNeighbour->getArmies() == territory->getArmies())
{
advanceList.push_back(territory);
}
}
}
return advanceList;
}
string BenevolentPlayerStrategy::getStrategyString() const
{
return "Benevolent";
}
//NeutralPlayerStrategy class implementation
NeutralPlayerStrategy::NeutralPlayerStrategy(Player* player) : PlayerStrategy(player) {} //Parameterized Constructor
NeutralPlayerStrategy::NeutralPlayerStrategy(const NeutralPlayerStrategy& playerStrategy) : PlayerStrategy(playerStrategy) {} //Copy Constructor
NeutralPlayerStrategy& NeutralPlayerStrategy::operator = (const NeutralPlayerStrategy& playerStrategy) //Assignment Operator Overloading
{
if (this != &playerStrategy)
{
PlayerStrategy::operator=(playerStrategy);
}
return *this;
}
ostream& operator << (ostream& output, const NeutralPlayerStrategy& playerStrategy) //Stream Insertion Operator Overloading
{
output << "Strategy Type: Neutral\n";
return output;
}
NeutralPlayerStrategy* NeutralPlayerStrategy::clone() //Clone function
{
return new NeutralPlayerStrategy(*this);
}
bool NeutralPlayerStrategy::issueOrder(Deck* deck)
{
//Issues no orders
return false;
}
list<Territory*> NeutralPlayerStrategy::toDefend()
{
//Defends no territories
return {};
}
list<Territory*> NeutralPlayerStrategy::toAttack()
{
//Attacks no territories
return {};
}
string NeutralPlayerStrategy::getStrategyString() const
{
return "Neutral";
}
//CheaterPlayerStrategy class implementation
CheaterPlayerStrategy::CheaterPlayerStrategy(Player* player) : PlayerStrategy(player) {} //Parameterized Constructor
CheaterPlayerStrategy::CheaterPlayerStrategy(const CheaterPlayerStrategy& playerStrategy) : PlayerStrategy(playerStrategy) {} //Copy Constructor
CheaterPlayerStrategy& CheaterPlayerStrategy::operator = (const CheaterPlayerStrategy& playerStrategy) //Assignment Operator Overloading
{
if (this != &playerStrategy)
{
PlayerStrategy::operator=(playerStrategy);
}
return *this;
}
ostream& operator << (ostream& output, const CheaterPlayerStrategy& playerStrategy) //Stream Insertion Operator Overloading
{
output << "Strategy Type: Cheater";
return output;
}
CheaterPlayerStrategy* CheaterPlayerStrategy::clone() //Clone function
{
return new CheaterPlayerStrategy(*this);
}
bool CheaterPlayerStrategy::issueOrder(Deck* deck)
{
std::set<Territory*> toConquer;
// Collect all adjacent enemy territories
for (Territory* myTerr : player->getTerritories()) {
for(Territory* adjTerr : myTerr->getAdjacentTerritories()) {
if (adjTerr->getOwner() != player && !player->hasNegotiatedWith(adjTerr->getOwner()) && !adjTerr->getOwner()->hasNegotiatedWith(player)) {
toConquer.insert(adjTerr);
}
}
}
if (toConquer.empty()) {
return false; // No territories to conquer
}
// Perform the conquest
for(Territory* enemyTerr : toConquer) {
Player* previousOwner = enemyTerr->getOwner();
if (previousOwner != nullptr) {
if (previousOwner->getPlayerStrategy()->getStrategyString() == "Neutral") {
previousOwner->setPlayerStrategy(new AggressivePlayerStrategy(previousOwner));
}