-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlayer.cpp
317 lines (285 loc) · 8.86 KB
/
Player.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
#include "Player.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#ifdef _DEBUG
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
using namespace std;
// default constructor definition
Player::Player()
{
name = "";
capturedTerritory = DBG_NEW bool(false);
negotiatedFriends = NULL;
playerHand = NULL;
playerOlist = NULL;
territoriesToDefend = NULL;
territoriesToAttack = NULL;
surroundingterr = NULL;
}
// parametrized constructor definition
Player::Player(string playerName, PlayerStrategy *ps)
{
// player's name is set to passed argument
name = playerName;
negotiatedFriends = DBG_NEW vector<string*>();
// each player owns a hand of cards
playerHand = DBG_NEW Hand();
capturedTerritory = DBG_NEW bool(false);
// minimal number of armies for any player is 3
const int MINARMIES = 3;
playerstrategy = ps;
//initialize player orderlist
playerOlist = DBG_NEW Orderlist();
territoriesToDefend = DBG_NEW vector<Territory*>;
territoriesToAttack = DBG_NEW vector<Territory*>;
surroundingterr = DBG_NEW vector<Territory*>;
}
// destructor definition
Player::~Player()
{
// avoid memory leaks
if (playerHand !=NULL)delete playerHand;
if (playerOlist != NULL)delete playerOlist;
if (capturedTerritory != NULL)delete capturedTerritory;
capturedTerritory = NULL;
if (negotiatedFriends!=NULL)
for (int i=0; i<negotiatedFriends->size(); i++){
delete negotiatedFriends->at(i);
negotiatedFriends->at(i) = NULL;
}
delete negotiatedFriends;
negotiatedFriends = NULL;
territoriesToDefend->clear();
delete territoriesToDefend;
territoriesToAttack->clear();
delete territoriesToAttack;
surroundingterr->clear();
delete surroundingterr;
delete playerstrategy;
}
// copy constructor definition
Player::Player(const Player &player)
{
name = player.name;
reinforcementPool = player.reinforcementPool;
capturedTerritory = DBG_NEW bool();
*capturedTerritory = *player.capturedTerritory;
negotiatedFriends = DBG_NEW vector<string*>;
for (int i = 0; i < player.negotiatedFriends->size(); i++) {
negotiatedFriends->push_back(DBG_NEW string(*player.negotiatedFriends->at(i)));
}
territoriesToDefend = DBG_NEW vector<Territory*>;
for (int i = 0; i < player.territoriesToDefend->size(); i++) {
territoriesToDefend->push_back(DBG_NEW Territory(*player.territoriesToDefend->at(i)));
}
territoriesToAttack = DBG_NEW vector<Territory*>;
for (int i = 0; i < player.territoriesToAttack->size(); i++) {
territoriesToAttack->push_back(DBG_NEW Territory(*player.territoriesToAttack->at(i)));
}
surroundingterr = DBG_NEW vector<Territory*>;
for (int i = 0; i < player.surroundingterr->size(); i++) {
surroundingterr->push_back(DBG_NEW Territory(*player.surroundingterr->at(i)));
}
playerHand = DBG_NEW Hand(*player.playerHand);
playerOlist = DBG_NEW Orderlist(*player.playerOlist);
playerstrategy = DBG_NEW PlayerStrategy(*player.playerstrategy);
}
// assignment operator
Player& Player::operator = (const Player& o)
{
return *this;
}
// stream insertion operator
ostream &operator << (ostream &output, const Player &o)
{
output << o.name;
return output;
}
void Player::addReinforcements(int r)
{
reinforcementPool += r;
}
int Player::getCurrentReinforcements()
{
return reinforcementPool;
}
void Player::setCurrentReinforcements(int i){
reinforcementPool = i;
}
//clears the players hand
void Player::clearhand() {
delete playerHand;
playerHand = new Hand();
}
//stream insertion operator overload for printing a vector list of territory references
string Player::toString(vector<Territory*>* t)
{
string list = "Territories: \n";
if(t->size() > 0)
{
for(int i=0; i<t->size(); i++)
{
list.append(t->at(i)->getterritory_name() + "\n");
}
}
return list;
}
// method to set the name of player
void Player::setName(string playerName)
{
name = playerName;
}
// method to return name of player
string Player::getName()
{
return name;
}
// definition of method to get number of
// territories owned by the player
int Player::getNumTerrOwned()
{
return territoriesToDefend->size();
}
// definition of method to get hand
// owned by player
Hand* Player::getHand()
{
return playerHand;
}
// definition of method to get
// player's orderlist
Orderlist* Player::getPlayerlist()
{
return playerOlist;
}
void Player::clear() {
*capturedTerritory = false;
for (int i = 0; i < negotiatedFriends->size(); i++) {
delete negotiatedFriends->at(i);
}
negotiatedFriends->clear();
}
vector<Territory*>* Player::gettoDefend(Map& m)
{
updatetoDefend( m);
return territoriesToDefend;
}
//new method updates the todefend list with all new territories
void Player::updatetoDefend(Map &m){
territoriesToDefend->clear();
delete territoriesToDefend;
vector<Territory*>* terr2def = DBG_NEW vector<Territory*>;
vector<Territory*>* terrall = m.getTerritories();
for (int i =0; i < terrall->size(); i++){
if (terrall->at(i)->getterritory_owner()->getName().compare(name)==0)terr2def->push_back(terrall->at(i));
}
territoriesToDefend = terr2def;
terr2def = NULL;
}
// definition of method toDefend
// returning a list of territories to defend
vector<Territory*>* Player::toDefend(Map &m)
{
return playerstrategy->toDefend(&m, this);
}
// definition of method toAttack
// returning a list of territories to attack
vector<Territory*>* Player::toAttack(Map &m,Territory &t)
{
return playerstrategy->toAttack(&m, this, &t);
}
vector<Territory*>* Player::toAttack(Map &m){
return playerstrategy->toAttack(&m, this);
}
// definition of issueOrder which creates a specific Order
// object and adds it to the player's list of orders
void Player::issueOrder(Map *m, vector<Player*>* pl, Deck* deckpointer)
{
playerstrategy->issueorder(m,pl,this,deckpointer);
}
void Player::addOrder(Order* o) {
playerOlist->add(o);
}
//returns the Boolean value fi the players has captured a territory
bool Player::getcaptureTerritory() {return *capturedTerritory;}
//setter method for boolean value of capture territory
void Player::setcaptureTerritory(bool b) {*capturedTerritory = b;}
//adds a friend to the negotiated list
void Player::addnegotiateFriends(string s) {negotiatedFriends->push_back(DBG_NEW string(s));}
//clears the list (used at the end of each round)
void Player::clearnegotiateFriends() {
for (int i=0; i<negotiatedFriends->size(); i++){
delete negotiatedFriends->at(i);
negotiatedFriends->at(i) = NULL;
}
negotiatedFriends->clear();}
//Check if a player is in the list
bool Player::isNegotiatedFriend(string s) {
for (int i =0 ;i< negotiatedFriends->size(); i++){
if (negotiatedFriends->at(i)->compare(s)==0)return true;
}
return false;
}
//For a given territory on a map returns all surrounding territories
vector<Territory*>* Player::surroundingterritories(Map& m, Territory &l) {
vector<Territory*>* terr = DBG_NEW vector<Territory*>;
for (int i=0; i<m.getTerritories()->size(); i++){
if(m.isAdjacent(m.getTerritories()->at(i),l)) terr->push_back(m.getTerritories()->at(i));
}
return terr;
}
vector<Territory*>* Player::friendlyAdjacentTerritories(Map &m,Territory &t)
{
vector<Territory*>* surroundingTerr = surroundingterritories(m, t);
vector<Territory*>* friendlyAdjacentTerr = DBG_NEW vector<Territory*>();
if(surroundingTerr != NULL)
{
for(Territory* t: *surroundingTerr)
{
if(this->getName().compare(t->getterritory_owner()->getName()) == 0)
{
friendlyAdjacentTerr->push_back(t);
}
}
}
delete surroundingTerr;
return friendlyAdjacentTerr;
}
vector<Order*>* Player::getOrderList(){
return(playerOlist->retirevelist());
}
vector<Territory*>* Player::allnonFriendlies(Map &m){
vector<Territory*>* result = DBG_NEW vector<Territory*>;
vector<Territory*>* terr = m.getTerritories();
for (int i=0; i<terr->size(); i++){
if (terr->at(i)->getterritory_owner()->getName().compare(name)!=0) result->push_back(terr->at(i));
}
return result;
}
int Player::getContinentBonus(Map* m)
{
int bonus = 0;
for (auto continent : *m->getContinents()){
if (continent->ownedByOnePlayer(this, m))
{
bonus += continent->getBonus();
}
}
return bonus;
}
vector<Territory*>* Player::getterritoriesToAttack() {
return territoriesToAttack;
}
vector<Territory*>* Player::getSurroundingterr() {
return surroundingterr;
}
PlayerStrategy* Player::getstartegy() {
return playerstrategy;
}