-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlayerStrategies.cpp
788 lines (645 loc) · 22.9 KB
/
PlayerStrategies.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
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
//
// Created by Safwan Ahmed on 2021-11-28.
//
#include "PlayerStrategies.h"
#include <iostream>
#include <string>
#include "Player.h"
using std::cout;
using std::cin;
using std::string;
using std::endl;
PlayerStrategies::PlayerStrategies(Player * p) {
this->p=p;
}
void PlayerStrategies::SetStrategy(Player * p) {
this->p=p;
}
PlayerStrategies::~PlayerStrategies() {
delete p;
}
vector<Territory*> HumanPlayer::toAttack(){
// This boolean will return if the decision was to attack or no the enemy player
bool toAttack = this->Decision();
int i=0;
// Getting all the enemy Neighboors Territories
if(toAttack) {
this->p->FillNeighboors();
vector<Territory *> array = this->p->Neighboors;
for(Territory* t:this->p->territoriesList){
for(Territory* t2:array){
if(t2->number==t->number){
array.erase(array.begin()+i);
}else{
i++;
}
}
i=0;
}
//Ronaldo7
//comparisons will be done n times
for (int i = 0; i < array.size(); i++) {
//compare elemet to the next element, and swap if condition is true
for (int j = 0; j < array.size() - 1; j++) {
//Order By least amount of armies to attack
//Check swap Value
if (array[j]->numberOfArmies > array[j + 1]->numberOfArmies)
swap((array[j]), array[j + 1]);
}
}
return array;
}
else
{
// We return an empty vector so it cannot attack
vector<Territory *> array;
return array;
}
}
/**
* There is no need to change that method of human
* @return a list of Territories to Defend
*/
vector<Territory*> HumanPlayer::toDefend(){
vector<Territory *> array = this->p->territoriesList;
//comparisons will be done n times
for (int i = 0; i < array.size(); i++)
{
//compare element to the next element, and swap if condition is true
for(int j = 0; j < array.size() - 1; j++)
{
//Order By least amount of armies
//Check swap Value
if (array[j]->numberOfArmies > array[j+1]->numberOfArmies)
swap((array[j]), array[j+1]);
}
}
return array;
}
// There is no need to change issue order for Human player, decision making of attack will take care of IT
void HumanPlayer::issueOrder() {
bool CanAdvance = false;
bool SpecialOrder = false;
this->p->FillNeighboors();
//Returns list of enemy territories that are neighbors that we can attack
toAttack();
//Returns list of territories that belong to the player that we can defend
toDefend();
bool deployExist=false;
bool advanceExist=false;
for(auto o:this->p->orders->get_order_list_new()){
if(o->get_type()=="Deploy"){
deployExist=true;
}
if(o->get_type()=="Advance"){
advanceExist=true;
}
}
//Check for deploy
if(!deployExist) {
Deploy *d = new Deploy;
d->p= this->p;
d->ListOfPlayers=this->p->Game->players;
d->m=this->p->Game->map;
d->deck=this->p->Game->gameDeck;
d->Attach(p->Game->o);
this->p->orders->Add(d);
deployExist=true;
}
//Check for Advance
if(!advanceExist) {
bool AdvanceAttack = true;
if (this->toAttack().size() == 0)
AdvanceAttack = false;
bool AdvanceDeploy = true;
if (this->toDefend().size() == 0)
AdvanceDeploy = false;
if (AdvanceAttack || AdvanceDeploy) {
Advance *d = new Advance;
d->p= p;
d->ListOfPlayers=this->p->Game->players;
d->deck=this->p->Game->gameDeck;
d->Attach(p->Game->o);
d->m=this->p->Game->map;
this->p->orders->Add(d);
CanAdvance = true;
}
}
//Verify for the Execution of a Special Order
vector<Card *> Hand = this->p->getHand()->GetCollection();
//Monitor if we have already pushed a Card
//Play will remove the Card from the player Hand
string CardType;
if(Hand.size() != 0) {
for (Card *c: Hand) {
CardType = c->GetType();
if (CardType == "Bomb") {
//Plays the Card
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Airlift") {
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Diplomacy") {
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Blockade") {
c->play(CardType,this->p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Reinforcement") {
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
}
}
else {
cout << "We have Used all the cards inside the player's hand" << endl;
SpecialOrder = true;
}
if(this->p->_name=="Neutral"&&(p->territoriesList.size()==0||advanceExist)&&p->hand->GetCollection().size()==0){
this->p->FinishedIssueOrder = true;
}
if(advanceExist && SpecialOrder&&deployExist){
this->p->FinishedIssueOrder = true;
return;
}
}
HumanPlayer::HumanPlayer(Player * p): PlayerStrategies(p) {
name="HumanPlayer";
this->p = p;
}
// This method will take the correct decision for the player
bool HumanPlayer::Decision() {
bool completed = false;
bool ToAttack =false;
while(!completed) {
cout<<endl;
cout << "If given the option are we going to attack or no this turn choose yes or no ?" << endl;
string decision = "";
// cin >> decision;
decision="yes";
if (decision == "yes") {
// Code to allow an attack
completed = true;
ToAttack = true;
return ToAttack;
}
if (decision == "no") {
// Code to stop the attack
p->humanPlay=true;
completed = true;
ToAttack = false;
return ToAttack;
} else {
cout << "Please enter a correct option " << endl;
}
}
}
//BenevolentPlayer Player
/*"----------------------------------------------------------------------------------------"*/
BenevolentPlayer::BenevolentPlayer(Player * p): PlayerStrategies(p) {
name="BenevolentPlayer";
this->p=p;
}
/**
* The Benevolent Player simply never attacks
* @return
*/
vector<Territory*> BenevolentPlayer::toAttack(){
// We just return an empty array because this player simply never Attacks , sufficient for it not to attack
vector<Territory *> array;
return array;
}
/**
* There is no need to change that method of Benevolent Player
* @return a list of Territories to Defend
*/
vector<Territory*> BenevolentPlayer::toDefend(){
vector<Territory *> array = this->p->territoriesList;
//comparisons will be done n times
for (int i = 0; i < array.size(); i++)
{
//compare elemet to the next element, and swap if condition is true
for(int j = 0; j < array.size() - 1; j++)
{
//Order By most amount of armies
//Check swap Value
if (array[j]->numberOfArmies < array[j+1]->numberOfArmies)
swap((array[j]), array[j+1]);
}
}
return array;
}
// There is no need to change issue order for Human player, decision making of attack will take care of IT
void BenevolentPlayer::issueOrder() {
bool CanAdvance = false;
bool SpecialOrder = false;
this->p->FillNeighboors();
//Returns list of enemy territories that are neighbors that we can attack
toAttack();
//Returns list of territories that belong to the player that we can defend
toDefend();
bool deployExist=false;
bool advanceExist=false;
for(auto o:this->p->orders->get_order_list_new()){
if(o->get_type()=="Deploy"){
deployExist=true;
}
if(o->get_type()=="Advance"){
advanceExist=true;
}
}
//Check for dpeloy
if(!deployExist) {
Deploy *d = new Deploy;
d->p= this->p;
d->ListOfPlayers=this->p->Game->players;
d->m=this->p->Game->map;
d->deck=this->p->Game->gameDeck;
d->Attach(p->Game->o);
this->p->orders->Add(d);
deployExist=true;
}
//Check for Advance
if(!advanceExist) {
bool AdvanceAttack = true;
if (this->toAttack().size() == 0)
AdvanceAttack = false;
bool AdvanceDeploy = true;
if (this->toDefend().size() == 0)
AdvanceDeploy = false;
if (AdvanceAttack || AdvanceDeploy) {
Advance *d = new Advance;
d->p= p;
d->ListOfPlayers=this->p->Game->players;
d->deck=this->p->Game->gameDeck;
d->Attach(p->Game->o);
d->m=this->p->Game->map;
this->p->orders->Add(d);
CanAdvance = true;
}
}
//Verify for the Execution of a Special Order
vector<Card *> Hand = this->p->getHand()->GetCollection();
//Monitor if we have already pushed a Card
//Play will remove the Card from the player Hand
string CardType;
if(Hand.size() != 0) {
for (Card *c: Hand) {
CardType = c->GetType();
if (CardType == "Bomb") {
//Plays the Card
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Airlift") {
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Diplomacy") {
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Blockade") {
c->play(CardType,this->p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Reinforcement") {
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
}
}
else {
cout << "We have Used all the cards inside the player's hand" << endl;
SpecialOrder = true;
}
if(this->p->_name=="Neutral"&&(p->territoriesList.size()==0||advanceExist)&&p->hand->GetCollection().size()==0){
this->p->FinishedIssueOrder = true;
}
if(advanceExist && SpecialOrder&&deployExist){
this->p->FinishedIssueOrder = true;
return;
}
}
/**
* ----------------------------------------------------------------------------------------------------------------------
* The aggressive player will always deploy on their strongest country,
* attack with their strongest country, and if it can, move their armies to their strongest country.
* Maybe modify the Advance method for this type of Player or use boolean
*/
AggressivePlayer::AggressivePlayer(Player * p): PlayerStrategies(p) {
name="AggressivePlayer";
// To tell to the player pointer that it is an agressive player
this->p = p;
p->AgressivePlayer = true;
}
vector<Territory*> AggressivePlayer::toAttack(){
// Getting all the enemy Neighboors Territories
vector<Territory *> array;
// Gets the Neighboors of the strongest Territory so we could Attack
if(this->p->toDefend().size()!=0) {
int i=0;
vector<Territory *> array = this->p->toDefend()[0]->edges;
for(Territory* t:this->p->territoriesList){
for(Territory* t2:array){
if(t->number==t2->number){
array.erase(array.begin()+i);
//Ronaldo
}else {
i++;
//another analysis of our index use being weird
}
}
i=0;
}
//comparisons will be done n times
for (int i = 0; i < array.size(); i++) {
//compare elemet to the next element, and swap if condition is true
for (int j = 0; j < array.size() - 1; j++) {
//Order By least amount of armies to attack
//Check swap Value
if (array[j]->numberOfArmies > array[j + 1]->numberOfArmies)
swap((array[j]), array[j + 1]);
}
}
if(array.size()==0&&this->p->Game->players.size()==2){
this->p->Game->numberOftimesHecannotattack++;
if( this->p->Game->numberOftimesHecannotattack>=25){
this->p->Game->draw=true;
}
}
return array;
}
return array;
}
/**
* There is no need to change that method of human
* @return a list of Territories to Defend
*/
vector<Territory*> AggressivePlayer::toDefend(){
// Verify if it does return indeed the strongest Territory
vector<Territory *> array = this->p->territoriesList;
//comparisons will be done n times
for (int i = 0; i < array.size(); i++)
{
//compare elemet to the next element, and swap if condition is true
for(int j = 0; j < array.size() - 1; j++)
{
//Order By most amount of armies
//Check swap Value
if (array[j]->numberOfArmies < array[j+1]->numberOfArmies)
swap((array[j]), array[j+1]);
}
}
return array;
}
// There is no need to change issue order for Human player, decision making of attack will take care of IT
void AggressivePlayer::issueOrder() {
bool CanAdvance = false;
bool SpecialOrder = false;
this->p->FillNeighboors();
//Returns list of enemy territories that are neighbors that we can attack
toAttack();
//Returns list of territories that belong to the player that we can defend
toDefend();
bool deployExist=false;
bool advanceExist=false;
for(auto o:this->p->orders->get_order_list_new()){
if(o->get_type()=="Deploy"){
deployExist=true;
}
if(o->get_type()=="Advance"){
advanceExist=true;
}
}
// We have to push a secial type of Deploy Order and overwite its execute method
if(!deployExist) {
Deploy *d = new Deploy;
d->p= this->p;
d->ListOfPlayers=this->p->Game->players;
d->m=this->p->Game->map;
d->deck=this->p->Game->gameDeck;
d->Attach(p->Game->o);
this->p->orders->Add(d);
deployExist=true;
}
//Check for Advance
if(!advanceExist) {
bool AdvanceAttack = true;
if (this->toAttack().size() == 0)
AdvanceAttack = false;
bool AdvanceDeploy = true;
if (this->toDefend().size() == 0)
AdvanceDeploy = false;
if (AdvanceAttack || AdvanceDeploy) {
Advance *d = new Advance;
d->p= p;
d->ListOfPlayers=this->p->Game->players;
d->deck=this->p->Game->gameDeck;
d->Attach(p->Game->o);
d->m=this->p->Game->map;
this->p->orders->Add(d);
CanAdvance = true;
}
}
//Verify for the Execution of a Special Order
vector<Card *> Hand = this->p->getHand()->GetCollection();
//Monitor if we have already pushed a Card
//Play will remove the Card from the player Hand
string CardType;
if(Hand.size() != 0) {
for (Card *c: Hand) {
CardType = c->GetType();
if (CardType == "Bomb") {
//Plays the Card
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Airlift") {
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Diplomacy") {
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Blockade") {
c->play(CardType,this->p);
// This breaks out of the loop so we can push one Card at a time
break;
}
if (CardType == "Reinforcement") {
c->play(CardType,p);
// This breaks out of the loop so we can push one Card at a time
break;
}
}
}
else {
cout << "We have Used all the cards inside the player's hand" << endl;
SpecialOrder = true;
}
if(this->p->_name=="Neutral"&&(p->territoriesList.size()==0||advanceExist)&&p->hand->GetCollection().size()==0){
this->p->FinishedIssueOrder = true;
}
if(advanceExist && SpecialOrder&&deployExist){
this->p->FinishedIssueOrder = true;
return;
}
}
/**
* ---------------------------------------------------------------------------------------------------------------------
* This where we start the Neutral player
*/
NeutralPlayer::NeutralPlayer(Player * p): PlayerStrategies(p) {
name="NeutralPlayer";
// To tell to the player pointer that it is an Neutral player
this->p =p;
p->NeutralPlayer = true;
}
vector<Territory*> NeutralPlayer::toAttack(){
// Neutral Player returns an empty array
vector<Territory *> array;
return array;
}
/**
* There is no need to change that method of human
* @return a list of Territories to Defend
*/
vector<Territory*> NeutralPlayer::toDefend(){
// Verify if it does return indeed the strongest Territory
vector<Territory *> array = this->p->territoriesList;
//comparisons will be done n times
for (int i = 0; i < array.size(); i++)
{
//compare elemet to the next element, and swap if condition is true
for(int j = 0; j < array.size() - 1; j++)
{
//Order By most amount of armies
//Check swap Value
if (array[j]->numberOfArmies > array[j+1]->numberOfArmies)
swap((array[j]), array[j+1]);
}
}
return array;
}
// There is no need to change issue order for Human player, he will issue no order
void NeutralPlayer::issueOrder() {
this->p->FinishedIssueOrder = true;
p->FinishedIssueOrder= true;
}
// Using = operator to return an Aggressive Player form a neutral Player
AggressivePlayer &AggressivePlayer::operator=(const Player &p) {
AggressivePlayer * a = new AggressivePlayer(new Player(p));
return *a;
}
/**
* -----------------------------------------------------------------------------------------------------------
* This is for the cheater player
*/
CheaterPlayer::CheaterPlayer(Player *p) : PlayerStrategies(p){
name="CheaterPlayer";
this->p = p;
//To specify that it is a cheater player
this->p->CheaterPlayer = true;
}
vector<Territory*> CheaterPlayer::toAttack(){
// This boolean will return if the decision was to attack or no the enemy player
// Getting all the enemy Neighboors Territories
vector<Territory *> array = this->p->Neighboors;
//comparisons will be done n times
for (int i = 0; i < array.size(); i++) {
//compare elemet to the next element, and swap if condition is true
for (int j = 0; j < array.size() - 1; j++) {
//Order By least amount of armies to attack
//Check swap Value
if (array[j]->numberOfArmies > array[j + 1]->numberOfArmies)
swap((array[j]), array[j + 1]);
}
}
return array;
}
/**
* There is no need to change that method of human
* @return a list of Territories to Defend
*/
vector<Territory*> CheaterPlayer::toDefend(){
vector<Territory *> array = this->p->territoriesList;
//comparisons will be done n times
for (int i = 0; i < array.size(); i++)
{
//compare elemet to the next element, and swap if condition is true
for(int j = 0; j < array.size() - 1; j++)
{
//Order By least amount of armies
//Check swap Value
if (array[j]->numberOfArmies > array[j+1]->numberOfArmies)
swap((array[j]), array[j+1]);
}
}
return array;
}
// There is no need to change issue order for Human player, decision making of attack will take care of IT
void CheaterPlayer::issueOrder() {
bool advanceExist = true;
bool SpecialOrder = true;
bool deployExist = true;
// List of all Neighboor Territorie that we can attack
vector<Territory *> TerritoriesToAttack =this->toAttack();
// the Cheater Player is like the Corona virus it spreads everywhere
vector<Territory *> CheaterTerritories =p->territoriesList;
// All the players in the game
vector<Player * > PlayersList = this->p->Game->players;
int index = 0;
//Looping through all the players in the game
for(Player * p : this->p->Game->players){
// All the territories of the Player we are currently looping on
vector<Territory *> PlayerT = p->territoriesList;
// Looping through the list territories in all Players
for(Territory * t1 :p->territoriesList ){
//Making sure that the Cheater player does not compare to itself
if(p->CheaterPlayer == false) {
//Looping the the list of territories that we can attack
for (Territory *t2: this->toAttack()) {
// Verifying if territory of enemy player is in that list
if (t1->number == t2->number) {
//Pushing back the territory to the list of Cheater Player
// erasing the territory of the enemy list
if(index<p->territoriesList.size()){
this->p->territoriesList.push_back(t1);
p->territoriesList.erase(p->territoriesList.begin() + index);
index--;
//lets analyse
if(p->NeutralPlayer== true){
p->SetStrategy(new AggressivePlayer(p));
}
}else{
}
}
}
index++;
}
}
index =0;
}
this->p->FinishedIssueOrder = true;
return;
}