-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
679 lines (615 loc) · 22 KB
/
Player.cpp
File metadata and controls
679 lines (615 loc) · 22 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
#include "Player.h"
#include "MapLoader.h"
#include <sstream>
#include <algorithm>
Player::Player() {
id = 0;
firstName = "Alice";
lastName = "Lee";
color = "white";
bidding = 0;
coins = 12;
score = 0;
remainingCity = 3;
remainingCubes = 18;
initializeDefaultValues();
}
Player::Player(int id, const string& firstName, const string& lastName, const string& color, int bidding, int coins,
vector<Territory *> territories) {
this->id = id;
this->firstName = firstName;
this->lastName = lastName;
this->color = color;
this->bidding = bidding;
this->coins = coins;
score = 0;
remainingCity = 3;
remainingCubes = 18;
this->territories = territories;
initializeDefaultValues();
}
Player::Player(const Player& player) {
id = player.id;
firstName = player.firstName;
lastName = player.lastName;
color = player.color;
bidding = player.bidding;
coins = player.coins;
score = player.score;
remainingCity = player.remainingCity;
remainingCubes = player.remainingCubes;
territories = player.territories;
cards = player.cards;
abilities = player.abilities;
cardTypeVp = player.cardTypeVp;
cardSetVp = player.cardSetVp;
oneVpPer3Coins = player.oneVpPer3Coins;
oneVpPerFlying = player.oneVpPerFlying;
players = player.players;
numberOfCardsOfEachType = player.numberOfCardsOfEachType;
oneVpPer3Coins = player.oneVpPer3Coins;
oneVpPerFlying = player.oneVpPerFlying;
}
void Player::initializeDefaultValues() {
abilities[moving] = 0;
abilities[army] = 0;
abilities[flying] = 0;
abilities[elixir] = 0;
abilities[immuneAttack] = 0;
numberOfCardsOfEachType[night] = 0;
numberOfCardsOfEachType[cursed] = 0;
numberOfCardsOfEachType[arcane] = 0;
numberOfCardsOfEachType[ancient] = 0;
numberOfCardsOfEachType[dire] = 0;
numberOfCardsOfEachType[forest] = 0;
numberOfCardsOfEachType[noble] = 0;
numberOfCardsOfEachType[mountain] = 0;
numberOfCardsOfEachType[emptyKind] = 0;
oneVpPer3Coins = false;
oneVpPerFlying = false;
}
Player::~Player() {
for (auto & territory : territories) {
territory = nullptr;
}
for (auto & player : players) {
player = nullptr;
}
for (auto & card : cards) {
card = nullptr;
}
}
void Player::PayCoin(int costOfCard) {
if (coins < costOfCard) {
cout << "ERROR! You don't have enough coin to buy this card!" << endl;
return;
}
coins -= costOfCard;
cout << "Player " << id << " paid " << costOfCard << " coins. Coins left: " << coins << endl;
}
void Player::PlaceNewArmies(int numberOfNewArmies, Territory* territory) {
territory->placeNewArmiesOfPlayer(id, numberOfNewArmies);
remainingCubes -= numberOfNewArmies;
cout << "Player " << id << " place " << numberOfNewArmies << " new armies in territory " << territory->getId()
<< endl;
}
int Player::MoveArmies(int numberOfArmies, Territory *from, Territory *to, int movingPoints) {
int remainingPoints;
if (from->getContinentId() == to->getContinentId()) {
remainingPoints = MoveOverLand(numberOfArmies, movingPoints);
} else {
remainingPoints = MoveOverWater(numberOfArmies, movingPoints);
}
if (remainingPoints == movingPoints) {
return remainingPoints;
}
from->removeArmiesOfPlayer(id, numberOfArmies);
to->placeNewArmiesOfPlayer(id, numberOfArmies);
cout << numberOfArmies << " armies were moved from " << from->getId() << " to " << to->getId() << endl;
return remainingPoints;
}
int Player::MoveOverLand(int numberOfArmies, int movingPoints) {
if (numberOfArmies > movingPoints) {
cout << "ERROR! You don't have enough moving points. Please try again." << endl;
return movingPoints;
}
return movingPoints - numberOfArmies;
}
int Player::MoveOverWater(int numberOfArmies, int movingPoints) {
int cost = 3;
if (abilities[flying] == 1) {
cout << "\nFlying level 1! The cost to move over water is 2 per army." << endl;
cost = 2;
} else if (abilities[flying] >= 2) {
cout << "\nFlying level 2! The cost to move over water is 1 per army." << endl;
cost = 1;
}
if (numberOfArmies * cost > movingPoints) {
cout << "ERROR! You don't have enough moving points. Try again." << endl;
return movingPoints;
}
return movingPoints - numberOfArmies * cost;
}
int Player::BuildCity(Territory* territory, int buildPoints) {
if (remainingCity <= 0) {
cout << "You have no city in hand!" << endl;
return buildPoints;
}
remainingCity--;
territory->buildCity(id);
return buildPoints - 1;
}
int Player::DestroyArmy(int numberOfArmies, Player* otherPlayer, Territory* territory, int destroyPoints) {
int currTroops = territory->getArmiesOfPlayer(otherPlayer->getId());
if (currTroops < numberOfArmies) {
cout << "There is not enough troop to destroy!" << endl;
return destroyPoints;
}
otherPlayer->remainingCubes += numberOfArmies;
territory->removeArmiesOfPlayer(otherPlayer->getId(), numberOfArmies);
cout << numberOfArmies << " armies are destroyed on territory " << territory->getId() << endl;
return destroyPoints - numberOfArmies;
}
bool Player::AndOrAction(Card *card) {
if (!card->getAnd() && !card->getOr()) {
cout << "ERROR! This is neither an And card nor an Or card." << endl;
return false;
}
if (card->getAnd() && card->getOr()) {
cout << "ERROR! A card cannot be both And card and Or card." << endl;
return false;
}
if (card->getAnd()) {
bool result1 = takeAction(card->getActions()[0]);
bool result2 = takeAction(card->getActions()[1]);
return result1 && result2;
}
// It must be an "Or card" now.
int option;
cout << "You may choose one action below: " << endl;
cout << "1. " << card->getActions()[0] << endl;
cout << "2. " << card->getActions()[1] << endl;
cout << ">>";
cin >> option;
while (option < 1 || option > 2) {
cout << "Invalid number! Please try again." << endl;
cout << ">>";
cin >> option;
}
return takeAction(card->getActions()[option - 1]);
}
bool Player::takeAction(Action action) {
if (action.actionType == ActionType::placeArmy && abilities[AbilityType::army] > 0) {
cout << "\nUsing ability! you can place extra " << abilities[AbilityType::army] << " armies!\n" << endl;
action.amount += abilities[AbilityType::army];
}
if (action.actionType == ActionType::moveArmy && abilities[AbilityType::moving] > 0) {
cout << "\nUsing ability! you can move extra " << abilities[AbilityType::moving] << " armies!\n" << endl;
action.amount += abilities[AbilityType::moving];
}
while (action.amount > 0) {
int option;
cout << "--------------------------------------" << endl;
cout << "You can " << action << ". (You are player " << id << ")" << endl;
cout << "--------------------------------------" << endl;
cout << "Please choose one option:" << endl;
cout << "1. Take action." << endl;
cout << "2. Done." << endl;
cout << ">>";
cin >> option;
if (option == 2) {
break;
}
int remainingPoints = action.amount;
if (action.actionType == placeArmy) {
remainingPoints = placeNewArmiesPrompt(action.amount);
} else if (action.actionType == moveArmy) {
remainingPoints = moveArmiesPrompt(action.amount);
} else if (action.actionType == buildCity) {
remainingPoints = buildCityPrompt(action.amount);
} else if (action.actionType == destroyArmy) {
remainingPoints = destroyArmyPrompt(action.amount);
}
if (remainingPoints == action.amount) {
continue;
}
action.amount = remainingPoints;
}
return true;
}
int Player::placeNewArmiesPrompt(int movingPoints) {
cout << "Game rule: You may place new armies only on the starting region or on a region where you have a city."
<< endl;
printTerritoriesForNewArmies();
int territoryId, numberOfArmies;
cout << "Please choose a territory ID to place the new armies: " << endl;
cout << ">>";
cin >> territoryId;
// You may place new armies only on the starting region or on a region where you have a city.
Territory* territory = getTerritoryById(territoryId);
if (!territory->getIsStartingRegion() && territory->getCities()[id] == 0) {
cout << "ERROR! You may place new armies only on the starting region or on a region where"
" you have a city." << endl;
return movingPoints;
}
cout << "Please enter the number of armies to place: " << endl;
cout << ">>";
cin >> numberOfArmies;
if (numberOfArmies > movingPoints) {
cout << "ERROR! You can place " << movingPoints << " new armies at most." << endl;
} else if (numberOfArmies < 1) {
cout << "ERROR! Please select a valid number." << endl;
} else if (remainingCubes < numberOfArmies) {
cout << "ERROR! You don't have enough cubes in hand!" << endl;
} else {
PlaceNewArmies(numberOfArmies, territory);
movingPoints -= numberOfArmies;
}
return movingPoints;
}
int Player::moveArmiesPrompt(int movingPoints) {
printMyTerritoriesWithArmies();
printNeighborsOfTerritoriesWithArmies();
vector<int> myTerritoriesWithArmies;
for (auto & territory : territories) {
if (territory->getArmies()[id] > 0) {
myTerritoriesWithArmies.emplace_back(territory->getId());
}
}
vector<int> territoryIdList;
for (auto & territory : territories) {
territoryIdList.emplace_back(territory->getId());
}
int fromTerritoryId, toTerritoryId, numberOfArmies;
cout << "Please choose a territory ID as starting point: " << endl;
cout << ">>";
cin >> fromTerritoryId;
while (find(myTerritoriesWithArmies.begin(), myTerritoriesWithArmies.end(), fromTerritoryId) ==
myTerritoriesWithArmies.end()) {
cout << "ERROR! You don't have army at this territory. Please try again!" << endl;
cout << ">>";
cin >> fromTerritoryId;
}
cout << "Please choose a territory ID as target: " << endl;
cout << ">>";
cin >> toTerritoryId;
while (find(territoryIdList.begin(), territoryIdList.end(), toTerritoryId) == territoryIdList.end()) {
cout << "ERROR! There is no territory with this ID. Please try again." << endl;
cout << ">>";
cin >> toTerritoryId;
}
Territory* fromTerritory = getTerritoryById(fromTerritoryId);
Territory* toTerritory = getTerritoryById(toTerritoryId);
vector<int> neighbors = territoryAdjacencyList[fromTerritoryId];
while (find(neighbors.begin(), neighbors.end(), toTerritoryId) == neighbors.end()) {
cout << "ERROR! These two territories are not connected. Please try again." << endl;
cout << "Please choose a territory ID as starting point: " << endl;
cout << ">>";
cin >> fromTerritoryId;
cout << "Please choose a territory ID as target: " << endl;
cout << ">>";
cin >> toTerritoryId;
fromTerritory = getTerritoryById(fromTerritoryId);
toTerritory = getTerritoryById(toTerritoryId);
}
cout << "Please choose the number of armies you want to move: " << endl;
cout << ">>";
cin >> numberOfArmies;
while (numberOfArmies > fromTerritory->getArmiesOfPlayer(id)) {
cout << "ERROR! You can move at most " << fromTerritory->getArmiesOfPlayer(id) << " armies. Please try again."
<< endl;
cout << ">>";
cin >> numberOfArmies;
}
int remainingMovingPoints = MoveArmies(numberOfArmies, fromTerritory, toTerritory, movingPoints);
return remainingMovingPoints;
}
int Player::destroyArmyPrompt(int destroyPoints) {
int playerId, territoryId;
cout << "Please choose a player ID to destroy army: " << endl;
cout << ">>";
cin >> playerId;
if (getPlayerById(playerId)->abilities[immuneAttack] > 0) {
cout << "This player is immune to attack!" << endl;
return destroyPoints;
}
cout << "This player has armies in the following regions:" << endl;
for (auto & territory : territories) {
int numOfArmies = territory->getArmiesOfPlayer(playerId);
if (numOfArmies > 0) {
cout << "TerritoryId " << territory->getId() << ": " << numOfArmies << " armies; ";
}
}
cout << endl;
cout << "Please choose a territory ID to destroy the army: " << endl;
cout << ">>";
cin >> territoryId;
Player* otherPlayer = getPlayerById(playerId);
Territory* territory = getTerritoryById(territoryId);
int remainingMovingPoints = DestroyArmy(1, otherPlayer, territory, destroyPoints);
return remainingMovingPoints;
}
int Player::buildCityPrompt(int buildPoints) {
cout << "Game rule: You may place a city anywhere on the board where you have an army." << endl;
printMyTerritoriesWithArmies();
vector<int> myTerritoriesWithArmies;
for (auto & territory : territories) {
if (territory->getArmies()[id] > 0) {
myTerritoriesWithArmies.emplace_back(territory->getId());
}
}
int territoryId;
cout << "Please choose a territory ID to build a city: " << endl;
cout << ">>";
cin >> territoryId;
while (find(myTerritoriesWithArmies.begin(), myTerritoriesWithArmies.end(), territoryId) == myTerritoriesWithArmies.end()) {
cout << "ERROR! You may place a city where you have an army. Please try again!" << endl;
cout << ">>";
cin >> territoryId;
}
Territory* territory = getTerritoryById(territoryId);
return BuildCity(territory, buildPoints);
}
void Player::exchange(Card *card) {
cards.emplace_back(card);
numberOfCardsOfEachType[card->getType()]++;
for (int i = 0; i < card->getAbilities().size(); ++i) {
cout << "You gain a new ability: " << card->getAbilities()[i] << "\n\n";
// Add this ability to the player's abilities attribute
AbilityType abilityType = card->getAbilities()[i].abilityType;
if (abilityType == gainCoins) {
coins += card->getAbilities()[i].amount;
} else if (abilityType == VP) {
if (card->getAbilities()[i].vpType == cardType) {
cardTypeVp.emplace_back(card->getAbilities()[i].cardTypeForVP);
} else if (card->getAbilities()[i].vpType == cardSet) {
cardSetVp.emplace_back(card->getAbilities()[i].cardTypeForVP);
} else if (card->getAbilities()[i].vpType == coinsLeft) {
oneVpPer3Coins = true;
} else if (card->getAbilities()[i].vpType == vpPerFlying) {
oneVpPerFlying = true;
}
} else {
abilities[abilityType] += card->getAbilities()[i].amount;
}
}
}
Territory *Player::getTerritoryById(int territoryId) {
for (auto & territory : territories) {
if (territory->getId() == territoryId) {
return territory;
}
}
cout << "ERROR! There is no territory with this ID." << endl;
return nullptr;
}
Player* Player::getPlayerById(int playerId) {
for (auto & player : players) {
if (player->getId() == playerId) {
return player;
}
}
cout << "ERROR! There is no player with this ID." << endl;
return nullptr;
}
map<AbilityType, int> Player::getAbilities() const {
return abilities;
}
void Player::addScore(int newScore) {
score += newScore;
}
void Player::printMyAbilities() {
cout << "\nMy abilities: ";
for (const auto &[k, v] : abilities) {
switch (k) {
case AbilityType::moving:
cout << v << " moving, ";
break;
case AbilityType::army:
cout << v << " armies, ";
break;
case AbilityType::flying:
cout << v << " flying, ";
break;
case AbilityType::elixir:
cout << v << " elixir, ";
break;
case AbilityType::immuneAttack:
cout << v << " immune attack, ";
break;
}
}
cout << "\n\n";
}
void Player::printMyTerritoriesWithArmies() {
cout << "My Territories With Armies:" << endl;
for (auto & territory : territories) {
if (territory->getArmies()[id] > 0) {
cout << "Territory" << territory->getId() << " (" << territory->getArmies()[id] << " armies), ";
}
}
cout << endl;
}
void Player::printNeighborsOfTerritoriesWithArmies() {
cout << "Neighbors of these territories: " << endl;
for (auto & territory : territories) {
int numOfArmies = territory->getArmies()[id];
if (numOfArmies > 0) {
cout << "Territory" << territory->getId() << " (" << numOfArmies << " armies) -> ";
vector<int> neighbors = territoryAdjacencyList[territory->getId()];
for (int neighbor : neighbors) {
int distance = 1;
if (territory->getContinentId() != getTerritoryById(neighbor)->getContinentId()) {
distance = 3;
}
cout << "Territory" << neighbor << " (distance " << distance << "), ";
}
cout << endl;
}
}
}
void Player::printTerritoriesForNewArmies() {
cout << "You may place new armies in these territories: ";
for (auto & territory : territories) {
if (territory->getIsStartingRegion() || territory->getCities()[id] > 0) {
cout << territory->getId() << ", ";
}
}
cout << endl;
}
Player &Player::operator=(const Player &player) {
firstName = player.firstName;
bidding = player.bidding;
coins = player.coins;
score = player.score;
remainingCity = player.remainingCity;
remainingCubes = player.remainingCubes;
territories = player.territories;
cards = player.cards;
return *this;
}
string Player::toString() const {
stringstream ss;
ss << "Player{ firstName=" << firstName << "; ";
ss << "bidding=" << bidding << "; ";
ss << "coinSupply=" << coins << "; ";
ss << "score=" << score << "; ";
ss << "remainingCity=" << remainingCity << "; ";
ss << "remainingCubes=" << remainingCubes << "; ";
ss << "territories=[";
for (int i = 0; i < territories.size(); i++) {
ss << *territories[i];
if (i < territories.size() - 1) {
ss << ", ";
}
}
ss << "]; cards=[";
for (int i = 0; i < cards.size(); i++) {
ss << *cards[i];
if (i < cards.size() - 1) {
ss << ", ";
}
}
ss << "]}";
return ss.str();
}
ostream &operator<<(ostream &out, const Player &player) {
out << "Player " << player.id << ": " << player.firstName << " " << player.lastName
<< " (" << player.color << ")" << endl;
out << "------------------------------------------" << endl;
out << "bidding: " << player.bidding << "; ";
out << "coins: " << player.coins << "; ";
out << "score: " << player.score << "; \n";
out << "remainingCity: " << player.remainingCity << "; ";
out << "remainingCubes: " << player.remainingCubes << "; \n";
out << "territories=[";
vector<int> hasForceInTerritory;
for (int i = 0; i < player.territories.size(); i++) {
if (player.territories[i]->getArmies()[player.getId()] > 0 ||
player.territories[i]->getCities()[player.getId()] > 0) {
hasForceInTerritory.emplace_back(player.territories[i]->getId());
}
}
for (int i = 0; i < hasForceInTerritory.size(); i++) {
out << hasForceInTerritory[i];
if (i < hasForceInTerritory.size() - 1) {
out << ", ";
}
}
out << "]\ncards=[";
for (int i = 0; i < player.cards.size(); i++) {
out << player.cards[i]->getName();
if (i < player.cards.size() - 1) {
out << ", ";
}
}
out << "]\n\n";
return out;
}
int Player::getId() const {
return id;
}
void Player::setId(int newId) {
id = newId;
}
string Player::getFirstName() const {
return firstName;
}
void Player::setFirstName(string &newFirstName) {
firstName = newFirstName;
}
string Player::getLastName() const {
return lastName;
}
void Player::setLastName(string &newLastName) {
lastName = newLastName;
}
string Player::getColor() const {
return color;
}
void Player::setColor(string &newColor) {
color = newColor;
}
int Player::getBiding() const {
return bidding;
}
void Player::setBidding(int newBiding) {
bidding = newBiding;
}
int Player::getCoins() const {
return coins;
}
void Player::setCoins(int newCoins) {
coins = newCoins;
}
int Player::getScore() const {
return score;
}
void Player::setScore(int newScore) {
score = newScore;
}
int Player::getRemainingCity() const {
return remainingCity;
}
void Player::setRemainingCity(int city) {
this->remainingCity = city;
}
int Player::getRemainingCubes() const {
return remainingCubes;
}
void Player::setRemainingCubes(int cubes) {
this->remainingCubes = cubes;
}
vector<Territory *> Player::getTerritories() const {
return territories;
}
void Player::setTerritories(vector<Territory *> &territories) {
this->territories = territories;
}
vector<Card *> Player::getCards() const {
return cards;
}
void Player::setCards(vector<Card *> &cards) {
this->cards = cards;
}
void Player::setPlayers(vector<Player*> newPlayers) {
this->players = newPlayers;
}
void Player::setTerritoryAdjacencyList(map<int, vector<int>> territoryAdjacencyList) {
this->territoryAdjacencyList = territoryAdjacencyList;
}
vector<CardType> Player::getCardTypeVp() {
return cardTypeVp;
}
vector<CardType> Player::getCardSetVp() {
return cardSetVp;
}
unordered_map<CardType, int> Player::getNumberOfCardsOfEachType() {
return numberOfCardsOfEachType;
}
bool Player::hasOneVpPer3Coins() {
return oneVpPer3Coins;
}
bool Player::hasOneVpPerFlying() {
return oneVpPerFlying;
}