-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
154 lines (135 loc) · 5.75 KB
/
Game.cpp
File metadata and controls
154 lines (135 loc) · 5.75 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
#include "Game.hpp"
#include "Deck.hpp"
#include "Utility.hpp"
#include <ctime>
#include <list>
#include <algorithm>
#include <climits>
using namespace std;
Game::Game(vector<string> names) {
// We already know how many players there will be, so reserve it
players_.reserve(names.size());
// Construct players_ with the names given
std::copy( names.begin(), names.end(), back_inserter(players_) );
// Tell each player who's on their sides
// Start with the edge cases
players_[0]
.setLeftRightPlayers(&players_[1],
&players_[players_.size() - 1]);
players_[players_.size() - 1]
.setLeftRightPlayers(&players_[0],
&players_[players_.size() - 2]);
for (size_t i = 1; i < players_.size() - 1; ++i) {
players_[i].setLeftRightPlayers(&players_[i+1], &players_[i-1]);
}
deck_ = Deck::getDeck(players_.size());
vector<Wonder> allWonders = AllWonders::getAllWonders();
random_shuffle(allWonders.begin(), allWonders.end());
for(size_t i=0; i<players_.size(); ++i) {
players_[i].setGame(this);
players_[i].giveWonder(allWonders[i]);
players_[i].chooseWonderSide();
}
}
void Game::discard(const Card& c) {
discard_.push_back(c);
}
vector<Card>& Game::getDiscard() {
return discard_;
}
void Game::play() {
for (int age = 0; age < 3; ++age) {
// Make the hands for this age
random_shuffle(deck_[age].begin(), deck_[age].end());
vector< list<Card> > hands;
hands.reserve(players_.size());
for (auto it = deck_[age].begin(); it != deck_[age].end(); it += 7) {
hands.push_back( list<Card>(&it[0], &it[7]) );
}
// Take turns
for (int turn = 0; turn < 6; ++turn) {
for (size_t playerNo = 0; playerNo < players_.size(); ++playerNo) {
// Give the player a hand, which is shifted each turn
// Add size of players*turn so that it's not negative before modulus
size_t handNo = (players_.size()*turn + playerNo + ((age % 2 != 0? 1 : -1) * turn)) % players_.size();
players_[playerNo].giveHand(&hands[handNo]);
}
for (size_t i=0; i<Player::turnRounds; ++i) {
for (size_t playerNo = 0; playerNo < players_.size(); ++playerNo) {
players_[playerNo].takeTurn(i);
}
}
}
//for (size_t playerNo = 0; playerNo < players_.size(); ++playerNo) {
// players_[playerNo].revealAction();
//}
for (size_t playerNo = 0; playerNo < players_.size(); ++playerNo) {
players_[playerNo].postAge();
}
// Military time
for (Player& p : players_) {
const vector<Produce> thisProduce = p.getProduce();
const vector< vector<Produce> > sideProduce{
p.getLeftPlayer() ->getProduce(),
p.getRightPlayer()->getProduce()};
size_t thisMilitary = std::count(thisProduce.begin(), thisProduce.end(), Produce::SHIELD);
for (const vector<Produce>& s : sideProduce) {
int militaryDiff = thisMilitary - std::count(s.begin(), s.end(), Produce::SHIELD);
if (militaryDiff > 0) {
// We won!
p.militaryWin(2 * age + 1);
} else if (militaryDiff < 0) {
p.militaryLoss();
}
}
}
}
// Count up the points and determine a winner
int winningPoints = INT_MIN;
vector<const Player*> winners;
for (const Player& p : players_) {
const vector<Produce> finalProduce = p.getProduce();
size_t gears = std::count(finalProduce.begin(), finalProduce.end(), Produce::GEAR),
tablets = std::count(finalProduce.begin(), finalProduce.end(), Produce::TABLET),
compasses = std::count(finalProduce.begin(), finalProduce.end(), Produce::COMPASS);
size_t sciencePts = 0;
for (size_t i=std::count(finalProduce.begin(), finalProduce.end(), Produce::ANY_SCIENCE); i>0; --i) {
// TODO: This isn't right... it only works for 1 ANY_SCIENCE
++gears;
size_t pts = std::min({gears, tablets, compasses})*7 + gears*gears + tablets*tablets + compasses*compasses;
if(pts > sciencePts) sciencePts = pts;
--gears;
++tablets;
pts = std::min({gears, tablets, compasses})*7 + gears*gears + tablets*tablets + compasses*compasses;
if(pts > sciencePts) sciencePts = pts;
--tablets;
++compasses;
pts = std::min({gears, tablets, compasses})*7 + gears*gears + tablets*tablets + compasses*compasses;
if(pts > sciencePts) sciencePts = pts;
--compasses;
}
int points = std::count(finalProduce.begin(), finalProduce.end(), Produce::VP)
+ p.getMilitaryPts()
+ p.getCoins() / 3
+ sciencePts;
cout << p << " got " << points << " points!" << endl;
if (points == winningPoints) {
winners.push_back(&p);
} else if (points > winningPoints) {
winners.clear();
winners.push_back(&p);
winningPoints = points;
}
//int points = filter([](const Produce& r){ return r == Produce::VP; }, finalProduce).size();
}
if (winners.size() > 1) {
cout << "Players ";
for (size_t i=0; i < winners.size() - 2; ++i) {
cout << *winners[i] << " and ";
}
cout << *winners[winners.size() - 1];
cout << " tied!";
} else {
cout << *winners[0] << " won!" << endl;
}
}