-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenevolentPlayerStrategy.cpp
133 lines (118 loc) · 4.79 KB
/
BenevolentPlayerStrategy.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
//
// Created by Ryan on 2021-12-01.
//
#include "BenevolentPlayerStrategy.h"
/**
* Default constructor
* @param player link
*/
BenevolentPlayerStrategy::BenevolentPlayerStrategy(Player* player) {
this->p = player;
p->setName("Benevolent");
}
/**
* Issue order inherited from PlayerStrategy
*
* @param vPlayersInPlay all the other players in the game
*/
void BenevolentPlayerStrategy::issueOrder(vector<Player *> &vPlayersInPlay) {
int numArmiesDeployed = 0;
int toDeploy;
int weakest;
int weakestUnits;
int currentUnits;
int difference;
vector<Territory *> ownedTerritories = p->getVTerritory();
vector<int> territoryUnits;
for(int i = 0; i < ownedTerritories.size(); i++)
territoryUnits.push_back(ownedTerritories.at(i)->unitsGarrisoned);
// Reinforce whenever possible
if (p->getHand()->isCardOwned("Reinforcement")) {
int reinforcementCardReward = 5;
this->p->setReinforcements(this->p->getReinforcements() + reinforcementCardReward);
p->getHand()->removeCard(p->getHand()->getCardIndex("Reinforcement"));
}
// Use reinforcement pool
while (p->getReinforcements() != numArmiesDeployed){
weakest = -1;
for(int i = 0; i < ownedTerritories.size(); i++){
Territory * cur = ownedTerritories.at(i);
currentUnits = territoryUnits.at(i);
if(weakest == -1) {
weakest = 0;
weakestUnits = currentUnits;
toDeploy = p->getReinforcements() - numArmiesDeployed;
continue;
}
difference = abs(weakestUnits - currentUnits);
if(weakestUnits > currentUnits){
toDeploy = difference;
weakest = i;
weakestUnits = currentUnits;
}
else if(difference < toDeploy)
toDeploy = difference;
}
if(toDeploy == 0)
toDeploy = 1;
territoryUnits.at(weakest) = territoryUnits.at(weakest) + toDeploy;
numArmiesDeployed += toDeploy;
auto *newDeploy = new Deploy(toDeploy, *ownedTerritories.at(weakest));
newDeploy->setPlayerLink(*p);
this->p->getOrdersList()->addDeploy(newDeploy);
}
// todo: Fortifies weakest territories
vector<Territory*> defendingTerritories = toDefend();
for(Territory * border : defendingTerritories){
for(Territory * potentialAdj : ownedTerritories){
if(potentialAdj->IsAdjacent(*border)){
//if(!p->isTerritoryInList(defendingTerritories, potentialAdj->id)){
auto *newAdvance = new Advance(floor(potentialAdj->unitsGarrisoned / 2), *potentialAdj, *border);
newAdvance->setPlayerLink(*p);
this->p->getOrdersList()->addAdvance(newAdvance);
//}
}
}
}
}
/**
* Displays all adjacent territories to owned territories
*
* @return vector of all adjacent territories
*/
vector<Territory*> BenevolentPlayerStrategy::toAttack() {
vector<Territory *> territoriesToAttack;
//loop through player owned territories
for (int i = 0; i < p->getTerritorySize(); i++) {
//loop through the adjacent territories of the owned territories
for (int j = 0; j < p->getVTerritory().at(i)->adjacentTerritories.size(); j++) {
//check if that territory is already owned, if its now owned then add to list
if (this->p->isOwnedTerritory(p->getVTerritory().at(i)->adjacentTerritories.at(j)->id) == nullptr &&
!(p->isTerritoryInList(territoriesToAttack, p->getVTerritory().at(i)->adjacentTerritories.at(j)->id))) {
territoriesToAttack.push_back(p->getVTerritory().at(i)->adjacentTerritories.at(j));
}
}
}
return territoriesToAttack;
}
/**
* Displays territories owned that are adjacent to other unowned territories
*
* @return vector of all adjacent territories
*/
vector<Territory*> BenevolentPlayerStrategy::toDefend() {
vector<Territory *> territoriesToDefend;
//loop through player owned territories
for (int i = 0; i < p->getTerritorySize(); i++) {
//loop through the adjacent territories of the owned territories
for (int j = 0; j < p->getVTerritory().at(i)->adjacentTerritories.size(); j++) {
//check if that territory is already owned, if the adjacent territory is not owned
if (this->p->isOwnedTerritory(p->getVTerritory().at(i)->adjacentTerritories.at(j)->id) == nullptr &&
!(p->isTerritoryInList(territoriesToDefend, p->getVTerritory().at(i)->id))) {
territoriesToDefend.push_back(
p->getVTerritory().at(i)); //push the territory we own into list of territories to defend
}
}
}
return territoriesToDefend;
}