-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cc
292 lines (240 loc) · 7.49 KB
/
player.cc
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
#include "player.h"
using namespace std;
Player::Player(string name, char gamePiece, int funds)
: name{name}, gamePiece{gamePiece}, funds{funds}
{}
bool Player::checkIfInMonopolyBlock(std::string squareName) {
std::string monoBlockOfSquare = monoBlockOfProp(squareName);
int size = monopolyBlocks.size();
for (int i = 0; i < size; i++) {
if (monopolyBlocks[i] == monoBlockOfSquare) {
return true;
}
}
return false;
}
bool Player::ownThisProp(std::string name) {
int size = ownedProperties.size();
for (int i = 0; i < size; i++) {
if (ownedProperties[i]->getName() == name) {
return true;
}
}
return false;
}
bool Player::addFund(int amt) {
funds += amt;
return true;
}
bool Player::payFund(int amt) {
if (bankruptStatus) {
std::cout << "(testing) this player is bankrupted" << std::endl;
return false;
}
if (funds < amt) {
std::cout << "(testing) insufficient fund to pay, check to see if you can sell anything" << std::endl;
return false;
}
funds -= amt;
return true;
}
bool Player::addProp(std::shared_ptr<Ownable> prop) {
// check if property is already owned
if (this->ownThisProp(prop->getName())) {
std::cout << "(testing) this props is owned" << std::endl;
return false;
}
if (isGym(prop->getName())){
numGymOwned++;
} else if (isResidence(prop->getName())){
numResOwned++;
}
ownedProperties.push_back(prop);
return true;
}
bool Player::removeProp(std::shared_ptr<Ownable> prop) {
// check if property is owned
if (!this->ownThisProp(prop->getName())) {
std::cout << "(testing) this props is not owned" << std::endl;
return false;
}
for (unsigned int i = 0; i < ownedProperties.size(); i++) {
if (prop == ownedProperties[i]) {
ownedProperties.erase(ownedProperties.begin() + i);
break;
}
}
return true;
}
bool Player::mortageProp(std::shared_ptr<Ownable> prop) {
// mortgage the prop and add fund accordingly
int fundFromMortgage = costToMortProp(prop->getName());
funds += fundFromMortgage;
int sizeOwnProp = ownedProperties.size();
for (int i = 0; i < sizeOwnProp; i++) {
if (prop == ownedProperties[i]) {
ownedProperties[i]->setMortStatus(true);
break;
}
}
return true;
}
bool Player::unmortageProp(std::shared_ptr<Ownable> prop) {
// pay the fund and unmortgage props
int fundToPay = costToUnmortProp(prop->getName());
if (fundToPay > funds) {
std::cout << "(testing) you don't have enough money to unmortgage" << std::endl;
return false;
}
funds -= fundToPay;
int sizeOwnProp = ownedProperties.size();
for (int i = 0; i < sizeOwnProp; i++) {
if (prop == ownedProperties[i]) {
ownedProperties[i]->setMortStatus(false);
break;
}
}
return true;
}
char Player::getGamePiece() const {
return gamePiece;
}
string Player::getName() const {
return name;
}
string Player::getSquareAtCurrPos() {
return MAP_GAME[pos];
}
int Player::getCurrPos() const {
return pos;
}
int Player::getFunds() const {
return funds;
}
int Player::getAssets() const {
int result = funds;
// assets = funds + printed prices of all prop (both mort and unmort) +
// costs of all improvements
int sizeOwnProp = ownedProperties.size();
for (int i = 0; i < sizeOwnProp; i++){
std::string propName = ownedProperties[i]->getName();
result += costToBuyProp(propName);
int imprLevel = ownedProperties[i]->getImprLevel();
if (imprLevel > 0){
result += imprLevel * costToImprProp(propName);
}
}
return result;
}
void Player::movePlayer(int roll) {
pos += roll;
if (pos >= MAP_GAME_SIZE) {
std::cout << "***You receive OSAP SALARY for landing or passing it!***" << std::endl;
pos = pos % MAP_GAME_SIZE;
this->addFund(OSAP_SALARY);
}
}
void Player::moveToDCTims() {
pos = DC_TIMS_LINE;
// action on DC Tims Line (need to change later)
return;
}
void Player::setPos(int pos) {
this->pos = pos;
}
void Player::addTimsCup(){
timsCups++;
}
void Player::setTimsCups(int cups){
timsCups = cups;
}
int Player::getTimsCups(){
return timsCups;
}
void Player::setBankruptStatus(bool status) {
bankruptStatus = status;
}
bool Player::getBankruptStatus() {
return bankruptStatus;
}
std::vector<std::shared_ptr<Ownable>> Player::getOwnedPropList() {
return ownedProperties;
}
void Player::updateMonopolyBlock() {
// every falcuty need 3 building to finish a block except for Math faculty (they only need 2)
int sizeOwnedProp = ownedProperties.size();
std::map<std::string, int> trackingBuilding; // {"Sci1": 2, "Sci2": 3}
std::string eachBlock;
numGymOwned = 0;
numResOwned = 0;
// track any owned buildings, mort or unmort
// updates numGymOwned, numResOwned
for (int i = 0; i < sizeOwnedProp; i++) {
std::string propName = ownedProperties[i]->getName();
if (isGym(propName)){
numGymOwned++;
} else if (isResidence(propName)){
numResOwned++;
}
eachBlock = ownedProperties[i]->getMonoBlock();
trackingBuilding[eachBlock] += 1;
}
for (auto &block: trackingBuilding) {
if (block.second == 2 && block.first == "Math") {
monopolyBlocks.push_back(block.first);
} else if (block.second == 3) {
monopolyBlocks.push_back(block.first);
}
}
}
int Player::getNumGymOwned(){
return numGymOwned;
}
int Player::getNumResOwned(){
return numResOwned;
}
void Player::printOwnedProp(){
int sizeOwnedProp = ownedProperties.size();
cout << "Displaying all properites owned by " << name << endl;
for (int i = 0; i < sizeOwnedProp; i++) {
std::string propName = ownedProperties[i]->getName();
cout << propName << "\t";
cout << "Cost: $" << costToBuyProp(propName) << "\t";
cout << "Mortgaged: " ;
if (ownedProperties[i]->getMortStatus()){
cout << "Yes\t" << endl;
} else {
cout << "No\t";
if (isAcademic(ownedProperties[i]->getName())) {
cout << "Improvements: " << ownedProperties[i]->getImprLevel() << "\t";
cout << "Improvement Cost: " << costToImprProp(propName) << endl;
} else {
cout << endl;
}
}
}
}
void Player::displayAssets(){
cout << "Displaying assets of " << name << endl;
cout << "Funds: " << funds << endl;
printOwnedProp();
cout << "Tims Cups: " << timsCups << endl;
cout << "Total worth: " << getAssets() << endl;
}
void Player::loadUpdateAmountToPay(){
int sizeOwnedProp = ownedProperties.size();
for (int i = 0; i < sizeOwnedProp; i++){
std::string propName = ownedProperties[i]->getName();
if (isGym(propName)){
ownedProperties[i]->setPayLevel(numGymOwned - 1);
} else if (isResidence(propName)){
ownedProperties[i]->setPayLevel(numResOwned - 1);
} else {
bool blockOwned = checkIfInMonopolyBlock(propName);
if (blockOwned){
auto acad = std::dynamic_pointer_cast<Academic>(ownedProperties[i]);
acad->setBlockOwned(true);
}
}
}
}