-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
602 lines (567 loc) · 20.3 KB
/
Game.cpp
File metadata and controls
602 lines (567 loc) · 20.3 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
//
// Created by Jingchao Zhang on 2/26/2021.
//
#include <random>
#include <sstream>
#include <filesystem>
#include <algorithm>
#include <string>
#include <iomanip>
#include <stdexcept>
#include "Game.h"
#include "MapLoader.h"
#include "BidingFacility.h"
using namespace std;
const int Game::CARD_COSTS[] = {0, 1, 1, 2, 2, 3};
const int Game::CARD_COSTS_SIZE = 6;
Game::Game() {
COLORS = {"purple", "white", "green", "grey"};
}
Game::~Game() {
delete hand;
hand = nullptr;
delete deck;
deck = nullptr;
delete map;
map = nullptr;
for (auto & player : players) {
delete player;
player = nullptr;
}
}
bool Game::start() {
cout << "****************************" << endl;
cout << "Eight-Minute_Empire_Legends." << endl;
cout << "****************************" << endl;
cout << "Creating game components...\n" << endl;
coinSupply = 36;
for (const auto & color : COLORS) {
armies[color] = 18;
cities[color] = 3;
}
printComponents();
bool selectMapSucceed = selectMap();
if (!selectMapSucceed) {
return false;
}
selectNumberOfPlayers();
createPlayers();
cout << "Creating a deck of cards..." << endl;
createDeck();
cout << "A deck of cards are created! There are " << deck->getCards().size() << " cards in the deck.\n" << endl;
printComponents();
return true;
}
bool Game::startup() {
cout << "****************************" << endl;
cout << " Startup phase:" << endl;
cout << "****************************" << endl;
cout << "\nShuffling deck..." << endl;
deck->shuffleDeck();
cout << "There are " << deck->getCards().size() << " cards in the deck." << endl;
cout << "\nDrawing six cards..." << endl;
hand = new Hand(deck); // draw six cards
printSixCards();
cout << "There are " << deck->getCards().size() << " cards in the deck." << endl;
cout << "\nSelecting starting region..." << endl;
if (!selectStartingRegion()) {
return false;
}
cout << "\nPlacing armies and cities on the starting region..." << endl;
createArmiesAndCities();
map->printForce(numOfPlayer);
cout << "\nPlayers bidding..." << endl;
bid();
return true;
}
void Game::play() {
cout << "****************************" << endl;
cout << " Main Game starts!" << endl;
cout << "****************************" << endl;
bool gameEnd = players[0]->getCards().size() == 13;
while (!gameEnd) {
for (int i : order) {
cout << "It's player " << i << "'s turn:\n" << endl;
Player* currentPlayer = getPlayerById(i);
printSixCards();
currentPlayer->printMyAbilities();
Card* card = selectCard(currentPlayer);
if (card->getAnd() || card->getOr()) {
currentPlayer->AndOrAction(card);
} else {
currentPlayer->takeAction(card->getActions()[0]);
}
map->printForce(numOfPlayer);
cout << "Player " << currentPlayer->getId()
<< ", your turn is completed! Press Enter to pass to other players..." << endl;
cin.ignore(10, '\n');
cin.get();
cout << "****************************" << endl;
cout << "****************************" << endl;
}
// TODO 为测试改为3回合结束游戏,将来要改回13。
gameEnd = players[0]->getCards().size() == 3;
}
}
Card* Game::selectCard(Player* currentPlayer) {
int cardIndex;
while (true) {
cout << "Please select a card index (1-6) (You have " << currentPlayer->getCoins() << " coins):" << endl;
cout << ">> ";
cin >> cardIndex;
cardIndex--;
if (cardIndex < 0 || cardIndex >= CARD_COSTS_SIZE) {
cout << "ERROR! Please enter a number from range 1-6!" << endl;
cout << "Try again." << endl;
continue;
}
if (currentPlayer->getCoins() < CARD_COSTS[cardIndex]) {
cout << "ERROR! You don't have enough coin to buy this card!" << endl;
cout << "Try again." << endl;
continue;
}
break;
}
currentPlayer->PayCoin(CARD_COSTS[cardIndex]);
Card* card = hand->getHandCards()[cardIndex];
currentPlayer->exchange(card);
hand->exchange(cardIndex + 1, deck);
return card;
}
void Game::computeScore() {
computeMapScore();
computeAbilityScore();
computeElixirScore();
}
void Game::computeMapScore() {
// Territory
cout << "Computing Territory scores..." << endl;
for (auto & territory : map->getTerritories()) {
int playerId = territory->getControllingPlayerId();
if (playerId > 0) {
cout << "Player " << playerId << " is controlling territory " << territory->getId() << endl;
Player* player = getPlayerById(playerId);
player->addScore(1);
}
}
// Continent
cout << "\nComputing Continent scores..." << endl;
for(auto continent: map->getContinents()){
int playerId = continent->getControllingPlayerId();
if (playerId > 0) {
cout << "Player " << playerId << " is controlling continent " << continent->getId() << endl;
Player* player = getPlayerById(playerId);
player->addScore(1);
}
}
cout << endl;
}
void Game::computeAbilityScore() {
cout << "Computing Ability scores..." << endl;
for (auto & player : players) {
for (auto type : player->getCardTypeVp()) {
cout << "Player " << player->getId() << " gains 1 VP from each " << Card::cardTypeToString(type) << endl;
player->addScore(player->getNumberOfCardsOfEachType()[type]);
}
for (auto type : player->getCardSetVp()) {
if (type == noble && player->getNumberOfCardsOfEachType()[noble] == 3) {
cout << "Player " << player->getId() << " gains 4 VP from three noble cards." << endl;
player->addScore(4);
} else if (type == mountain && player->getNumberOfCardsOfEachType()[mountain] == 2) {
cout << "Player " << player->getId() << " gains 3 VP from two mountain cards." << endl;
player->addScore(3);
}
}
if (player->hasOneVpPer3Coins()) {
cout << "Player " << player->getId() << " gains " << player->getCoins() / 3 << " VP from coins left." << endl;
player->addScore(player->getCoins() / 3);
}
if (player->hasOneVpPerFlying()) {
int flyingVP = player->getAbilities()[AbilityType::flying];
cout << "Player " << player->getId() << " gains " << flyingVP << " VP from flying." << endl;
player->addScore(flyingVP);
}
}
}
void Game::computeElixirScore() {
vector<Player*> hasMostElixirs;
int highest = -1;
for (auto & player : players) {
int numberOfElixir = player->getAbilities()[AbilityType::elixir];
if (numberOfElixir > highest) {
highest = numberOfElixir;
hasMostElixirs.clear();
hasMostElixirs.emplace_back(player);
} else if (numberOfElixir == highest) {
hasMostElixirs.emplace_back(player);
}
}
if (hasMostElixirs.size() == 1) {
cout << "Player " << hasMostElixirs[0]->getId() << " gains 2 VP from Elixirs." << endl;
hasMostElixirs[0]->addScore(2);
} else {
for (auto & player : hasMostElixirs) {
cout << "Player " << player->getId() << " gains 1 VP from Elixirs." << endl;
player->addScore(1);
}
}
}
void Game::claimWinner() {
cout << endl;
for (auto & player : players) {
int armiesOnBoard = 0;
for (auto & territory : map->getTerritories()) {
armiesOnBoard += territory->getArmies()[player->getId()];
}
int numOfControlledRegions = 0;
for (auto & territory : map->getTerritories()) {
if (territory->getControllingPlayerId() == player->getId()) {
numOfControlledRegions++;
}
}
cout << "Player " << player->getId() << ": score: " << player->getScore() << ", coins: " << player->getCoins()
<< ", armies: " << armiesOnBoard << ", controlled regions: " << numOfControlledRegions << endl;
}
vector<Player*> playersWithHighestScore;
int highestScore = -1;
for (auto & player : players) {
if (player->getScore() > highestScore) {
highestScore = player->getScore();
playersWithHighestScore.clear();
playersWithHighestScore.emplace_back(player);
} else if (player->getScore() == highestScore) {
playersWithHighestScore.emplace_back(player);
}
}
if (playersWithHighestScore.size() == 1) {
displayWinner(playersWithHighestScore[0]);
return;
}
vector<Player*> playersWithMostCoins;
int mostCoins = -1;
for (auto & player : playersWithHighestScore) {
if (player->getCoins() > mostCoins) {
mostCoins = player->getCoins();
playersWithMostCoins.clear();
playersWithMostCoins.emplace_back(player);
} else if (player->getCoins() == mostCoins) {
playersWithMostCoins.emplace_back(player);
}
}
if (playersWithMostCoins.size() == 1) {
displayWinner(playersWithMostCoins[0]);
return;
}
vector<Player*> playersWithMostArmies;
int mostArmies = -1;
for (auto & player : playersWithMostCoins) {
int armiesOnBoard = 0;
for (auto & territory : map->getTerritories()) {
armiesOnBoard += territory->getArmies()[player->getId()];
}
if (armiesOnBoard > mostArmies) {
mostArmies = armiesOnBoard;
playersWithMostArmies.clear();
playersWithMostArmies.emplace_back(player);
} else if (armiesOnBoard == mostArmies) {
playersWithMostArmies.emplace_back(player);
}
}
if (playersWithMostArmies.size() == 1) {
displayWinner(playersWithMostArmies[0]);
return;
}
Player* playerWithMostControlledRegions;
int mostControlledRegions = -1;
for (auto & player : playersWithMostArmies) {
int numOfControlledRegions = 0;
for (auto & territory : map->getTerritories()) {
if (territory->getControllingPlayerId() == player->getId()) {
numOfControlledRegions++;
}
}
if (numOfControlledRegions > mostControlledRegions) {
mostControlledRegions = numOfControlledRegions;
playerWithMostControlledRegions = player;
}
}
displayWinner(playerWithMostControlledRegions);
}
void Game::displayWinner(Player *player) {
cout << "********************************************" << endl;
cout << " WINNER is player " << player->getId() << ": " << player->getFirstName() << " " << player->getLastName()
<< "!" << endl;
cout << " CONGRATULATIONS!!!" << endl;
cout << "********************************************" << endl;
}
bool Game::selectMap() {
string filePath;
string path = "../Maps/";
vector<string> mapFiles;
for (const auto & entry : std::filesystem::directory_iterator(path)){
mapFiles.push_back(entry.path().string().erase(entry.path().string().find(path), path.size()));
}
int filePathOption = -1;
//Map selection
while (true) {
try {
cout << "Please chose your game map"<< endl;
for(int i = 0; i < mapFiles.size(); i++) {
cout << i + 1 << ". " << mapFiles[i] << endl;
}
cout << mapFiles.size() + 1 << ". Exit" << endl;
cout << ">> ";
cin >> filePathOption;
if (filePathOption < 1 || filePathOption > (mapFiles.size() + 1)) {
throw out_of_range("Unknown option, please chose again!");
} else if (filePathOption == mapFiles.size() + 1) {
cout << "GOODBYE" << endl;
return false;
}
filePath = path + mapFiles[filePathOption - 1];
map = MapLoader::loadMap(filePath);
bool validateResult = map->validate();
if (!validateResult) {
throw logic_error("This is not a valid map! Please choose another one.");
}
cout << *map << endl;
cout << "The map is created successfully!\n" << endl;
break;
} catch (out_of_range& e) {
cout << e.what() << endl;
} catch (const std::string e) {
cout << e << endl;
} catch (logic_error& e) {
cout << e.what() << endl;
}
}
return true;
}
void Game::selectNumberOfPlayers() {
int numberOfPlayers;
while (true) {
cout << "Please select the number of players: (2, 3, 4)" << endl;
cout << ">> ";
cin >> numberOfPlayers;
if (numberOfPlayers < 2 || numberOfPlayers > 4) {
cout << "Error! Please select a valid number." << endl;
} else {
break;
}
}
this->numOfPlayer = numberOfPlayers;
}
void Game::createPlayers() {
int coins;
if (numOfPlayer == 4) {
coins = 9;
} else if (numOfPlayer == 3) {
coins = 11;
} else if (numOfPlayer == 2) {
coins = 14;
}
string name;
string firstName;
string lastName;
string color;
int bidding;
vector<string> remainingColors = COLORS;
for (int i = 0; i < numOfPlayer; ++i) {
cout << "Please enter the full name of player " << i + 1 << ": (separate by space)" << endl;
cout << ">> ";
cin >> firstName;
cin >> lastName;
while (true) {
cout << "Please enter the color of player " << i + 1 << ": (";
for (int j = 0; j < remainingColors.size(); ++j) {
cout << remainingColors[j];
if (j != remainingColors.size() - 1) {
cout << ", ";
} else {
cout << ")\n>> ";
}
}
cin >> color;
auto itr = find(remainingColors.begin(), remainingColors.end(), color);
if (itr != remainingColors.end()) {
remainingColors.erase(itr);
break;
} else {
cout << "ERROR! Please type a correct color name." << endl;
}
}
cout << "Please enter the bidding of player " << i + 1 << ": (0-" << coins << ")" << endl;
cout << "(The coins will be deducted only if you win the bidding later.)" << endl;
cout << ">> ";
cin >> bidding;
while (bidding < 0 || bidding > coins) {
cout << "ERROR! Please enter the bidding of player " << i + 1 << ": (0-" << coins << ")" << endl;
cout << ">> ";
cin >> bidding;
}
coinSupply -= coins;
cities[color] -= 3;
armies[color] -= 18;
players.emplace_back(new Player(i + 1, firstName, lastName, color, bidding, coins,
map->getTerritories()));
cout << *players[i];
cout << "Player " << i + 1 << " is created successfully!\n" << endl;
}
for (auto & player : players) {
player->setPlayers(players);
player->setTerritoryAdjacencyList(map->getTerritoryAdjacencyList());
}
}
void Game::createDeck() {
deck = new Deck(numOfPlayer);
}
void Game::createArmiesAndCities() {
Territory* startingTerritory = map->getTerritoryById(startRegionId);
for (auto player : players) {
player->PlaceNewArmies(4, startingTerritory);
}
if (numOfPlayer == 2) {
vector<string> remainingColors = COLORS;
for (auto & player : players) {
auto itr = find(remainingColors.begin(), remainingColors.end(), player->getColor());
remainingColors.erase(itr);
}
string nonPlayerColor = remainingColors[0];
int numOfTerritories = map->getTerritories().size();
int territoryId = -1;
for (int i = 0; i < 10; i++) {
cout << "Player " << players[i % 2]->getId() << ", please select a territory from 1-" << numOfTerritories
<< endl;
cin >> territoryId;
if (territoryId < 1 || territoryId > numOfTerritories) {
cout << "Invalid territory ID! Please try again." << endl;
i--;
cin.clear();
cin.ignore();
} else {
//Assume non-player army has playerId of 0
map->getTerritoryById(territoryId)->placeNewArmiesOfPlayer(0, 1);
armies[nonPlayerColor]--;
}
}
}
for (auto player : players) {
cout << *player;
}
}
void Game::printSixCards() {
cout << "The six cards:" << endl;
cout << "Index:";
for (int i = 1; i <= 6; ++i) {
cout << setw(17) << to_string(i) + " ";
}
cout << "\nCost: ";
for (int i = 0; i < CARD_COSTS_SIZE; i++) {
cout << setw(17) << to_string(CARD_COSTS[i]) + " ";
}
cout << endl;
cout << "Card: ";
for (auto & card : hand->getHandCards()) {
cout << setw(17) << card->getName();
}
cout << "\n" << endl;
for (int i = 0; i < hand->getHandCards().size(); ++i) {
cout << i + 1 << ". [cost: " << CARD_COSTS[i] << "] " << *hand->getHandCards()[i] << endl;
}
}
bool Game::criteriaA(int regionId) {
vector<int> neighbors = map->getTerritoryNeighborsById(regionId);
for (int neighbor : neighbors) {
if (map->getDistance(regionId, neighbor) == 3) {
return true;
}
}
return false;
}
bool Game::criteriaB(int regionId) {
vector<int> neighbors = map->getTerritoryNeighborsById(regionId);
int waterConnectionCount = 0;
for (int neighbor : neighbors) {
if (map->getDistance(regionId, neighbor) == 1 && criteriaA(neighbor)) {
return true;
}
if (map->getDistance(regionId, neighbor) == 3) {
waterConnectionCount++;
}
}
if (waterConnectionCount >= 2) {
return true;
}
return false;
}
bool Game::selectStartingRegion() {
vector<Territory*> tempRegions = map->getTerritories();
shuffle(tempRegions.begin(), tempRegions.end(), std::mt19937(std::random_device()()));
cout << "Shuffling region IDs..." << endl;
for (auto & tempRegion : tempRegions) {
cout << tempRegion->getId() << " ";
}
for (auto & tempRegion : tempRegions) {
int regionId = tempRegion->getId();
if (criteriaA(regionId) && criteriaB(regionId)) {
startRegionId = regionId;
tempRegion->setIsStartingRegion(true);
cout << "\n\nStart region is " << startRegionId << "!\n" << endl;
return true;
}
}
cout << "There is no qualified region! Exit." << endl;
return false;
}
void Game::bid() {
int winnerID = BidingFacility::bid(players);
cout << "The bidding winner is player " << winnerID << "!\n" << endl;
Player* winner = getPlayerById(winnerID);
winner->PayCoin(winner->getBiding());
order.emplace_back(winnerID);
vector<int> otherPlayerIDs;
for (auto & player : players) {
if (player->getId() != winnerID) {
otherPlayerIDs.emplace_back(player->getId());
}
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(otherPlayerIDs.begin(), otherPlayerIDs.end(), g);
for (auto & otherPlayerID : otherPlayerIDs) {
order.emplace_back(otherPlayerID);
}
cout << "The order of players' move is:" << endl;
for (int i : order) {
cout << i << " ";
}
cout << "\n\n";
}
Player* Game::getPlayerById(int id) {
for (auto & player : players) {
if (player->getId() == id) {
return player;
}
}
cout << "ERROR! No player has this id.";
return nullptr;
}
void Game::printComponents() {
cout << "Game components..." << endl;
cout << "Coins: " << coinSupply << endl;
cout << "Armies: ";
for (int i = 0; i < COLORS.size(); ++i) {
cout << armies[COLORS[i]] << " " << COLORS[i];
if (i != COLORS.size() - 1) {
cout << ", ";
}
}
cout << "\nCities: ";
for (int i = 0; i < COLORS.size(); ++i) {
cout << cities[COLORS[i]] << " " << COLORS[i];
if (i != COLORS.size() - 1) {
cout << ", ";
}
}
cout << "\n\n";
}