-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
554 lines (453 loc) · 19.8 KB
/
Player.cpp
File metadata and controls
554 lines (453 loc) · 19.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
#include "Player.h"
#include <iostream>
#include <vector>
#include <string>
//Default constructor - initialize the player
Player::Player()
{
this->isAttacked = false;
}
//Destructor
Player::~Player() {
for (Territory* td : territoriesDefend)
delete td;
for (Territory* ta : territoriesAttack)
delete ta;
for (Territory* to : territoriesOwned)
delete to;
delete myOrders;
delete myHand;
};
//Constructor that initializes the player's collection of territories (owned, defend, attack) and the player name
Player::Player(vector<Territory*> territoriesOwned, vector<Territory*> territoriesDefend, vector<Territory*> territoriesAttack, string name, bool deployOrdersIssued)
: territoriesOwned(territoriesOwned), territoriesDefend(territoriesDefend), territoriesAttack(territoriesAttack), deployOrdersIssued(false), isHumanStrategy(false)
{
this->myHand = new Hand();
this->name = name;
this->territoriesOwned = territoriesOwned;
this->territoriesDefend = territoriesDefend;
this->territoriesAttack = territoriesAttack;
this->deployOrdersIssued = deployOrdersIssued;
this->isHumanStrategy = isHumanStrategy;
this->isAttacked = false;
}
//Constructor that initializes the player's name
Player::Player(string name)
: name(name)
{
this->name = name;
this->isHumanStrategy = false;
this->isAttacked = false;
}
string Player:: getName(){
return name;
}
void Player:: setName(string name){
this->name = name;
}
int Player:: getArmy(){
return army;
}
void Player:: setArmy(int army){
this->army = army;
}
bool Player:: getFlag(){
return deployOrdersIssued;
}
void Player::setFlag(bool deployOrdersIssued){
this->deployOrdersIssued = deployOrdersIssued;
}
bool Player:: getIsAttacked(){
return isAttacked;
}
void Player:: setIsAttacked(bool attacked){
this->isAttacked = attacked;
}
void Player::removeCardOfTypeFromHand(Player* player, CardType cardType) {
// Find the first card in the hand that matches the specified CardType
auto it = find_if(player->myHand->handCards.begin(), player->myHand->handCards.end(),
[&](Card* card) { return *card->getCardType() == cardType; });
// If a card of the specified type was found, remove it from the hand
if (it != player->myHand->handCards.end()) {
cout<<"card found in players hand"<<endl;
player->removeCardFromHand(*it);
}
}
void Player::removeCardFromHand(Card* cardToRemove) {
auto& handCards = myHand->handCards; // reference to vector of hand cards
handCards.erase(remove(handCards.begin(), handCards.end(), cardToRemove), handCards.end()); // remove card from vector
cout<<"card deleted from players hand"<<endl;
delete cardToRemove; // delete card from heap
// auto element = find(player->myHand->handCards.begin(), player->myHand->handCards.end(), this); // Finds element
// int elementIndex = distance(player->myHand->handCards.begin(), element); // Gets element index
// mainDeck->deckCards.push_back(move(this)); // Moves card pointer back to deck
// player->myHand->handCards.erase(element); // Erases card pointer from hand
}
//Copy Constructor passing parameter p
Player::Player(const Player& p)
{
this->myHand = p.myHand;
this->name = p.name;
this->territoriesOwned = p.territoriesOwned;
this->territoriesDefend = p.territoriesDefend;
this->territoriesAttack = p.territoriesAttack;
this->isHumanStrategy = p.isHumanStrategy;
}
//Implementation of the friend insertion operator for class Player
ostream& operator<<(ostream& output, Player& p) {
output << p.getName();
return output;
}
//Implementation of the owned() method
void Player::owned(Territory* t) {
vector<Territory*> territoriesOwned;
this->territoriesOwned.push_back(t);
}
//Implementation of the toDefend() method
vector<Territory*> Player::toDefend(vector<Territory*> t) {
vector<Territory*> territoriesDefend;
for (int i = 0; i < t.size(); i++){
if (t[i]->getOwner() == nullptr || t[i]->getOwner()->getName() == this->getName()){
territoriesDefend.push_back(t[i]);
}
}
return territoriesDefend;
}
// returns a list of territories that can be attecked by the player i.e. adjacent territories
vector<Territory*> Player::toAttack() {
vector<Territory*> territoriesAttack;
for (int j = 0; j < territoriesOwned.size(); j++)
{
vector<Territory*> adjacentTerritories = territoriesOwned[j]->getAdjacentTerritories();
for (int i = 0; i < adjacentTerritories.size(); i++)
{
if(!(adjacentTerritories[i]->getOwner()->getName() == this->getName()))
{
bool foundDuplicate = false;
for(int k = 0; k < territoriesAttack.size(); k++)
{
if(adjacentTerritories[i] == territoriesAttack[k])
{
foundDuplicate = true;
break;
}
}
if(!foundDuplicate)
{
territoriesAttack.push_back(adjacentTerritories[i]);
}
}
}
}
return territoriesAttack;
}
//Implementation of getPlayerTerritories() method
vector <Territory*> Player:: getPlayerTerritories(){
return territoriesOwned;
}
void Player::clearPlayerTerritories() {
territoriesOwned.clear();
}
Territory* Player::getStrongestCountry(Player* player) {
Territory* strongestTerritory = player->getPlayerTerritories()[0];
int highestArmyCount = 0;
for (int i = 0; i < player->getPlayerTerritories().size(); i++) {
if (player->getPlayerTerritories()[i]->getArmy() >= highestArmyCount) {
highestArmyCount = player->getPlayerTerritories()[i]->getArmy();
strongestTerritory = player->getPlayerTerritories()[i];
}
}
if (strongestTerritory->getArmy() == 0) {
cout<<"\nAll territories have 0 armies"<<endl;
strongestTerritory = nullptr;
return strongestTerritory;
}
return strongestTerritory;
}
Territory* Player::getWeakestCountry(Player* player) {
Territory* weakestTerritory = getPlayerTerritories()[0];
int lowestArmyCount = getPlayerTerritories()[0]->getArmy();
for (int i = 1; i < player->getPlayerTerritories().size(); i++) {
if (player->getPlayerTerritories()[i]->getArmy() <= lowestArmyCount) {
lowestArmyCount = player->getPlayerTerritories()[i]->getArmy();
weakestTerritory = player->getPlayerTerritories()[i];
}
}
return weakestTerritory;
}
void Player:: setStrategy(PlayerStrategy* ps){
this->ps = ps;
}
PlayerStrategy* Player:: getStrategy(){
return ps;
}
void Player::addTerritory(Territory* newTerritory) {
territoriesOwned.push_back(newTerritory);
}
void Player::removeTerritory(int index) {
if (index >= 0) {
territoriesOwned.erase(territoriesOwned.begin() + index);
}
}
// create an OrderList object
OrdersList* ol = new OrdersList();
//static int issue = 0;
void Player::issueOrder(vector<Player*> player, int index)
{
bool endLoop = true;
bool temp = true;
// cout<<player[index]->myOrders->vectorOfOrders.size() <<endl;
bool addingOrders = true;
int choice = 0;
int numberOfArmies = player[index]->getArmy();
bool deployOrdersIssued = player[index]->getFlag();
// cout << "ARMY SIZE " << player[index]->getArmy() << endl;
// cout << "number of armies" << numberOfArmies<< endl;
vector<Territory*> tToAttack = player[index]->toAttack(); //get list of territories to be attacked
vector<Territory*> tToDefend = player[index]->toDefend(player[index]->getPlayerTerritories()); //get list of territories to be defended
while (endLoop) { // (4) a player can play cards to issue orders;
if (numberOfArmies!=0 && temp){ // (2) a player will only issue deploy orders and no other kind of orders if they still have armies in their reinforcement pool;
Territory* selectedTarget = getValidTarget(tToDefend);
int reinforcementAmount = 0;
while (true) {
// cout << "\nPlayer " << player[index]->getName() << "'s turn: " << endl;
cout << "How many reinforcements do you wish to send ( 1 - " << numberOfArmies << " )?";
if (!(cin >> reinforcementAmount)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input: Please try again!" << endl;
} else if (reinforcementAmount < 1 || reinforcementAmount > numberOfArmies)
cout << "Invalid Input: Please try again!" << endl;
else
break;
} // end of while loop
numberOfArmies = numberOfArmies - reinforcementAmount;
Deploy* deploy = new Deploy(selectedTarget, reinforcementAmount);
selectedTarget->setArmy(reinforcementAmount);
cout<<"Created Deploy Order"<<endl;
deploy->orderEffect = "order deployed";
player[index]->myOrders->add(deploy);// add deploy order to orderslist
temp = false;
player[index]->setFlag(deployOrdersIssued);
}
cout << player[index]->getName() << ": \n "<< player[index]->myHand << endl;
cout << "\n-- Orders --\n";
cout << "1. Advance\n";
cout << "2. Bomb\n";
cout << "3. Blockade\n";
cout << "4. Airlift\n";
cout << "5. Negotiate\n";
cout << "6. End Ordering Phase\n";
// Prompt the user for input
cout << "Enter a choice (1 - 6): ";
// Read input from the user
cin >> choice;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\nInvalid input: Please enter an integer (1-6)" << endl;
}
switch (choice) {
// Advance
// (3) a player can issue advance orders to either defend or attack, based on the toAttack() and toDefend() lists;
case 1: // Advance
player[index]->setFlag(false);
if (player[index]->myHand->containsCardType("Reinforcement")) {
Territory* src = getValidTerritory(tToDefend, "Please select the territory you would like to Advance from:", 1);
// int wow;
// cin >> wow;
// cout << tToDefend[wow] << endl;
vector<Territory*> choiceAdjacentTerritories = src->getAdjacentTerritories();
Territory* tgt = getValidTerritory(choiceAdjacentTerritories, "Please select the territory you would like to Advance to:", 1);
int units = src->getArmy();
cout << "Enter the amount of units you wish to Advance (territory " << src->getName()<< " currently has "<< units << " armies (units)):";
int unitUser = 0;
cin >> unitUser;
while (unitUser<1 || unitUser > units){ // get valid unit amount
cout << "invalid units amount. Please enter a value higher than 0 and lower than " << units<< endl;
cin >> unitUser;
}
Advance* advance = new Advance(tgt, src, unitUser);
player[index]->myOrders->vectorOfOrders.push_back(advance); // add airlift order to orderslist
} else {
cout << "Invalid Request: You do not have this card!";
continue;
}
break;
// Bomb
case 2:
if (player[index]->myHand->containsCardType("Bomb")) {
cout << "Enter the integer of the territory you wish to Bomb: "<<endl;
// validate input
Bomb* bomb = new Bomb(getValidTarget(tToAttack));
player[index]->myOrders->add(bomb); // add bomb order to orderslist
}
else {
cout << "Invalid Request: You do not have this card! Try again"<<endl;
continue;
}
player[index]->setFlag(false);
break;
// Blockade
case 3:
if (player[index]->myHand->containsCardType("Blockade")) {
// validate input
Blockade* blockade = new Blockade(getValidTerritory(tToDefend, "\nPlease select the territory you would like to Blockade:",0));
player[index]->myOrders->add(blockade); // add blockade order to orderslist
} else {
cout << "Invalid Request: You do not have this card!"<<endl;
continue;
}
player[index]->setFlag(false);
break;
// Airlift
case 4:
if (player[index]->myHand->containsCardType("Airlift")) {
Territory* source = getValidTerritory(tToDefend, "Please select the territory you would like to Airlift from:", 4);
Territory* target = getValidTerritory(tToDefend, "Please select the territory you would like to Airlift to:", 0);
int maxUnitAmount = source->getArmy();
cout << "Enter the amount of units you wish to Airlift (territory " << source->getName()<< " currently has "<< maxUnitAmount << " armies (units)):";
int unitAmount = 0;
cin >> unitAmount;
cout<< "max unit amount is " << maxUnitAmount << " and the entered unit amount is " << unitAmount<<endl;
if (maxUnitAmount == 0){
cout << "There are no armies to Airlift" << endl;
}
else {
while (unitAmount<=0 || unitAmount > maxUnitAmount){ // get valid unit amount
cout << "invalid units amount. Please enter a value higher than 0 and lower than " << maxUnitAmount<< endl;
cin >> unitAmount;
}
}
Airlift* airlift = new Airlift(source, target, unitAmount);
player[index]->myOrders->add(airlift); // add airlift order to orderslist
} else {
cout << "Invalid Request: You do not have this card!";
continue;
}
player[index]->setFlag(false);
break;
// Negotiate
case 5:
if (player[index]->myHand->containsCardType("Diplomacy")) {
Negotiate* negotiate = new Negotiate(getValidPlayer(player, index));
player[index]->myOrders->add(negotiate); //add new negotiate order to the orderslist
} else {
cout << "Invalid Request: You do not have this card!";
continue;
}
player[index]->setFlag(false);
break;
//Exit
case 6:
addingOrders = false;
player[index]->setFlag(false);
endLoop = false;
break;
}
}
}
// checks if the user is trying to attack a valid territory i.e. if the entered integer corresponds to a territory id present in the vector returned by toAttack()
Territory* Player::getValidTarget(vector<Territory*>& tToAttack) {
cout << "\nThis is the list of available territories to you:" << endl; // display available territories to attack
for (auto t : tToAttack) {
cout << t->getName() << " (" << t->getTerritoryID() << ")\n";
}
cout << "\nPlease select a territory from this list:" << endl;
int territoryId;
bool validId = false;
Territory* target = nullptr;
while (!validId) {
if (!(cin >> territoryId) || cin.peek() != '\n') {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter an integer.\n";
} else { // get valid territory
for (auto t : tToAttack) {
if (t->getTerritoryID() == territoryId) {
validId = true;
target = t;
break;
}
}
if (!validId) {
cout << "Invalid territory ID. Please enter a valid ID.\n";
}
}
}
return target; //return a valid territory to attack
}
Territory* Player::getValidTerritory(vector<Territory*>& territory, string descriptiveMsg, int orderCase) {
cout << "\nThis is the list of available territories:" << endl; //display the territories that can be defended
for (auto t : territory) {
cout << t->getName() << " (" << t->getTerritoryID() << ")\n";
}
cout << descriptiveMsg << endl;
int territoryId;
bool validId = false;
Territory* target = nullptr;
while (!validId) {
if (!(cin >> territoryId) || cin.peek() != '\n') {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter an integer.\n";
} else {
for (auto t : territory) { // get valid territory
if (t->getTerritoryID() == territoryId) {
if(orderCase != 4){
validId = true;
target = t;
break;
}
else{
if(t->getArmy()!=0){
validId = true;
target = t;
break;
}
cout << "There are no armies to Airlift!" << endl;
}
}
}
if (!validId) {
cout << "Invalid territory ID. Please enter a valid ID.\n";
}
}
}
return target; //return a valid territory to be defended
}
//get valid player from this game
Player* Player::getValidPlayer(vector<Player*>players, int index){
cout << "Here are all the players currently in the game: " << endl;
for (auto player : players) {
if (player != this) {
cout << player->getName() << endl; //get valid players name
}
}
string playerName;
bool validName = false;
Player* validPlayer = nullptr;
while (!validName) {
cout << "Please enter the name of the player you wish to select: ";
if (!(cin >> playerName)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid player name." << endl;
} else {
transform(playerName.begin(), playerName.end(), playerName.begin(), ::tolower);
for (auto p : players) {
string name = p->getName(); //find the players name
transform(name.begin(), name.end(), name.begin(), ::tolower);
if (name == playerName && p != this) {
validName = true;
validPlayer = p;
break;
}
}
if (!validName) {
cout << "Invalid player name. Please enter a valid name." << endl;
}
}
}
return validPlayer;
}