-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
309 lines (282 loc) · 11.3 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
/*
* Email: [email protected]
* ID: ****6281
* @SamuraiPolix - Samuel Lazareanu
*/
#include "Player.hpp"
#include "Types.hpp"
#include <iostream>
#include "Board.hpp"
#include "Catan.hpp"
#include "DevelopmentCard.hpp"
#include "Tile.hpp"
using std::vector, std::string, std::map, std::cout, std::endl;
namespace ariel{
// static vars
Color Player::colors[3] = {Color::Red, Color::Green, Color::Blue};
int Player::playerCounter = 0;
// functions
Player::Player(string name) {
this->name = name;
// resources = {};
settlements = {};
roads = {};
tiles = {};
currTurn = false;
developmentCards = {};
victoryPoints = 0;
knights = 0;
hasLargestArmy = false;
color = colors[playerCounter++];
for (int i = 0; i < 5; i++){
resources[i] = 0;
}
}
int Player::placeSettlement(size_t index, Board* board){
// make sure its the player's turn
if (currTurn == false){
throw std::invalid_argument("Player tried to place a settlement when it's not his turn");
}
// make sure player has enough resources, if its not the first 2 settlements
if (numOfSettlements() >= 2 && !availableResourcesBuildable(BuildableTypes::Settlement)){
throw valid_resources("Player doesn't have enough resources (1 Wood, 1 Brick, 1 Sheep, 1 Wheat) to place a settlement\n");
}
// place settlement
int status = board->placeSettlement(*this, index);
if(status == 0){
settlements.push_back(index);
if (numOfSettlements() > 2){ // player pays for settlements after the first 2
resources[ResourceType::Wood-1]--;
resources[ResourceType::Brick-1]--;
resources[ResourceType::Sheep-1]--;
resources[ResourceType::Wheat-1]--;
}
}
addVictoryPoints(1); // add 1 victory point for placing a settlement
return status;
}
bool Player::availableResourcesBuildable(BuildableTypes type){
if (type == BuildableTypes::Settlement){
return resources[ResourceType::Wood-1] >= 1 && resources[ResourceType::Brick-1] >= 1 && resources[ResourceType::Sheep-1] >= 1 && resources[ResourceType::Wheat-1] >= 1;
}
else if (type == BuildableTypes::Road){
return resources[ResourceType::Wood-1] >= 1 && resources[ResourceType::Brick-1] >= 1;
}
else if (type == BuildableTypes::City){
return resources[ResourceType::Wheat-1] >= 2 && resources[ResourceType::Ore-1] >= 3;
}
throw std::invalid_argument("Invalid buildable type\n");
}
bool Player::availableResourcesDevelopmentCard(){
return resources[ResourceType::Ore-1] >= 1 && resources[ResourceType::Wheat-1] >= 1 && resources[ResourceType::Sheep-1] >= 1;
}
string Player::getName() const{
return "\033[" + std::to_string(color) + "m" + name + "\033[0m";
}
string Player::getSimpleName() const{
return name;
}
int Player::numOfSettlements(){
return settlements.size();
}
int Player::numOfRoads(){
return this->roads.size();
}
int Player::placeRoad(size_t vertex1, size_t vertex2, Board* board){
cout << "Player " << getName() << " is trying to place a road between " << vertex1 << " and " << vertex2 << endl;
// make sure its the player's turn
if (currTurn == false){
throw std::invalid_argument("Player tried to place a road when it's not his turn");
}
// make sure player has enough resources, if its not the first 2 settlements
if (numOfRoads() >= 2 && !availableResourcesBuildable(BuildableTypes::Road)){
throw valid_resources("Player doesn't have enough resources (1 Wood, 1 Brick) to place a road\n");
}
// place road
int status = board->placeRoad(*this, vertex1, vertex2);
if(status == 0){
roads.push_back(std::make_tuple(vertex1, vertex2));
if (numOfRoads() > 2){ // player pays for settlements after the first 2
resources[ResourceType::Wood-1]--;
resources[ResourceType::Brick-1]--;
}
}
return status;
}
size_t Player::buyDevelopmentCard(Catan& catan){
if (currTurn == false){
throw std::invalid_argument("Player tried to buy a development card when it's not his turn");
}
// check if player has enough resources
if (!availableResourcesDevelopmentCard()){
throw valid_resources("Player doesn't have enough resources (1 Ore, 1 Wheat, 1 Sheep) to buy a development card\n");
}
// remove resources
resources[ResourceType::Ore-1]--;
resources[ResourceType::Wheat-1]--;
resources[ResourceType::Sheep-1]--;
// add development card
return catan.addDevelopmentCard(*this);
}
void Player::printPoints(){
cout << this->getName() << " has " << getVictoryPoints() << " victory points" << endl;
}
void Player::trade(Player& player, ResourceType resourceSent, ResourceType resourceReceived, const size_t& amountSent, const size_t& amountReceived){
// make sure its the player's turn
if (currTurn == false){
throw std::invalid_argument("Player tried to trade when it's not his turn");
}
// make sure player has enough resources
if (resources[resourceSent-1] < amountSent){
throw valid_resources("Player doesn't have enough resources to trade\n");
}
// make sure other player has enough resources
if (player.getResourceAmount(resourceReceived) < amountReceived){
throw valid_resources("Other player doesn't have enough resources to trade\n");
}
// remove resources
resources[resourceSent-1] -= amountSent;
player.removeResource(resourceReceived, amountReceived);
// add resources
resources[resourceReceived-1] += amountReceived;
player.addResource(resourceSent, amountSent);
}
void Player::trade(Player& player, vector<tuple<ResourceType, size_t>>& resourcesSent, vector<tuple<ResourceType, size_t>>& resourcesReceived){
// make sure its the player's turn
if (currTurn == false){
throw std::invalid_argument("Player tried to trade when it's not his turn");
}
// make sure curr player has enough resources
for (size_t i = 0; i < resourcesSent.size(); i++){
if (resources[std::get<0>(resourcesSent[i])-1] < std::get<1>(resourcesSent[i])){
throw valid_resources("Player doesn't have enough resources to trade\n");
}
}
// make sure other player has enough resources
for (size_t i = 0; i < resourcesReceived.size(); i++){
if (player.getResourceAmount(std::get<0>(resourcesReceived[i])) < std::get<1>(resourcesReceived[i])){
throw valid_resources("Other player doesn't have enough resources to trade\n");
}
}
// remove resources from curr player, add to other player
for (size_t i = 0; i < resourcesSent.size(); i++){
resources[std::get<0>(resourcesSent[i])-1] -= std::get<1>(resourcesSent[i]);
player.addResource(std::get<0>(resourcesSent[i]), std::get<1>(resourcesSent[i]));
}
// add resources to curr player, remove from other player
for (size_t i = 0; i < resourcesReceived.size(); i++){
resources[std::get<0>(resourcesReceived[i])-1] += std::get<1>(resourcesReceived[i]);
player.removeResource(std::get<0>(resourcesReceived[i]), std::get<1>(resourcesReceived[i]));
}
}
void Player::rollDice(Catan& catan){
// make sure its the player's turn
if (currTurn == false){
throw std::invalid_argument("Player tried to roll the dice when it's not his turn");
}
int dice = rand() % 6 + 1 + rand() % 6 + 1;
cout << "Dice rolled: " << dice << endl;
if (dice == 7){
// Each player with more than 7 resource cards must return half of their resource cards to the bank.
catan.rolledSeven();
}
else{
catan.giveResources(dice);
}
}
size_t Player::numOfResources(){
size_t sum = 0;
for (size_t i = 0; i < 5; i++){
sum += resources[i];
}
return sum;
}
void Player::endTurn(Catan& catan){
currTurn = false;
catan.nextPlayer();
}
void Player::startTurn(){
currTurn = true;
}
size_t Player::getResourceAmount(ResourceType resource){
return resources[resource-1];
}
size_t Player::addDevelopmentCard(DevelopmentCard& card){
developmentCards.push_back(&card);
if (card.getType() == CardType::VictoryPoint){
victoryPoints++;
}
return developmentCards.size()-1;
}
int Player::useDevelopmentCard(size_t indexInHand){
developmentCards[indexInHand]->use();
// remove the card from the player
developmentCards.erase(developmentCards.begin() + (int)indexInHand);
return 0;
}
size_t Player::printDevelopmentCards(){
for (size_t i = 0; i < developmentCards.size(); i++){
cout << i << ". " << *developmentCards[i] << endl;
}
return developmentCards.size();
}
void Player::addTile(Tile* tile){
// cout<< tile << endl;
tiles.push_back(tile);
}
// int Player::addTilesByIndex(size_t index){
// for (size_t i = 0; i < tiles.size(); i++){
// if (tiles[i].hasIndexVertex(index) == index){
// return -1;
// }
// }
// tiles.push_back(Tile(index));
// return 0;
// }
void Player::addResource(ResourceType resource, size_t amount){
resources[resource-1] += amount;
}
void Player::removeResource(ResourceType resource, size_t amount){
if (resources[resource-1] < amount){
throw std::invalid_argument("Player doesn't have enough resources to remove\n");
}
resources[resource-1] -= amount;
}
int Player::getVictoryPoints(){
return victoryPoints + (hasLargestArmy ? 2 : 0);
}
int Player::addVictoryPoints(size_t points){
victoryPoints += points;
return victoryPoints;
}
void Player::updateLargestArmy(){
// iterate over all cards and check if player has exactly 3 knights
int count = 0;
for (size_t i = 0; i < developmentCards.size(); i++){
if (developmentCards[i]->getType() == CardType::Knight){
count++;
}
}
if (count >= 3){
hasLargestArmy = true;
}
else {
hasLargestArmy = false;
}
}
vector<Tile*>& Player::getTiles(){
return tiles;
}
Color Player::getColor(){
return color;
}
ostream& operator<<(ostream& os, const Player& player){
os << player.getName() << ": " << endl;
os << "- Victory Points: " << player.victoryPoints << endl;
os << "- Resources: " << endl;
for (size_t i = 0; i < 5; i++){
cout << " - " << getResourceName((ResourceType)(i+1)) << ": " << player.resources[i] << endl;
}
return os;
}
}