-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.cc
420 lines (376 loc) · 12.9 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
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
#include "player.h"
using namespace std;
Player::Player(char piece, std::string name, int money, int position, int timCups): piece{piece}, name{name}, bankrupt{false},
money{money}, position{position}, timCups{timCups}, numOfResidences{0}, numOfGyms{0}, inJail{false}, jailTurns{-1}{ }
char Player::getPiece(){
return piece;
}
string Player::getName(){
return name;
}
bool Player::isBankrupt(){
return bankrupt;
}
int Player::getMoney(){
return money;
}
int Player::getPosition(){
return position;
}
void Player::setPosition(int pos){
position = pos;
}
int Player::getTimCups(){
return timCups;
}
void Player::addTimCup(){
timCups++;
}
void Player::useTimCup(){
timCups--;
}
int Player::getGymNum(){
return numOfGyms;
}
void Player::addGym(){
numOfGyms++;
}
int Player::getResNum(){
return numOfResidences;
}
void Player::addRes(){
numOfResidences++;
}
std::vector<std::shared_ptr<Square>> Player::getSquares(){
return squaresOwned;
}
bool Player::isInJail(){
return inJail;
}
void Player::setJail(bool jail){
inJail = jail;
}
int Player::getJailTurns(){
return jailTurns;
}
void Player::setJailTurns(int jt){
jailTurns = jt;
}
vector<int> Player::getJailLastRoll(){
return jailLastRoll;
}
void Player::setJailLastRoll(vector<int> roll){
jailLastRoll.clear();
jailLastRoll.push_back(roll[0]);
jailLastRoll.push_back(roll[1]);
}
int Player::getWorth(){
int property_worth = 0;
int size = squaresOwned.size();
for (int i=0; i<size; i++){
property_worth+=squaresOwned[i]->getValue();
}
return property_worth+money;
}
void Player::printPlayerAssets(){
cout << "Name: " << name << endl;
cout << "Piece: " << piece << endl;
cout << "Position: " << position << endl;
if (inJail){
cout << "Jail: " << (2-jailTurns) << " turns left" << endl;
}
else{
cout << "Jail: Not in Jail" << endl;
}
if (bankrupt){
cout << "Money: Is Bankrupt" << endl;
}
else{
cout << "Money: $" << money << endl;
}
if (timCups>0){
cout << "TimCups: " << timCups << endl;
}
int size=squaresOwned.size();
if (size>0){
cout << "Properties: " << squaresOwned[0]->getName();
}
for (int i=1; i<size; i++){
cout << ", " << squaresOwned[i]->getName();
}
if (size>0){
cout << endl;
}
}
void Player::move(int pos){
position+=pos;
}
void Player::addMoney(int amount){
if (amount<=0){
cout << "You are trying to add an amount less than 0. This is an invalid operation." << endl;
}
else if (bankrupt){
cout << "Player is bankrupt, and thus money can't be added." << endl;
}
else{
money+=amount;
cout << name << " now has a balance of " << money << " after completeing the transaction." << endl;
}
return;
}
void Player::subtractMoney(int amount, vector < shared_ptr < Player >> otherPlayers) {
if (bankrupt) {
cout << "Player is bankrupt, and thus money can't be subtracted." << endl;
return;
} else if (money >= amount) {
money -= amount;
cout << name << " now has a balance of " << money << " after completing the transaction." << endl;
} else {
cout << "Your do not have enough money for this transaction. These are your assets: " << endl;
printPlayerAssets();
cout << "The following commands are available to you: " << endl;
cout << " bankrupt" << endl;
cout << " mortgage <property>" << endl;
cout << " improve <property> sell" << endl;
cout << " trade <player_name> <property> <property>" << endl;
cout << " trade <player_name> <property> <money>" << endl;
cout << " all" << endl;
string line = "";
cout << "Enter a valid command: " << endl;
while (money < amount) {
getline(cin, line);
stringstream ss(line);
string temp;
vector < string > command;
while (ss >> temp) {
command.push_back(temp);
}
if (command.size() < 1) {
continue;
}
if (command[0] == "bankrupt") {
cout << "You have chosen to declare bankruptcy. You will now be removed from the game." << endl;
bankrupt = true;
return;
} else if (command[0] == "mortgage" && command.size()>1) {
int size = squaresOwned.size();
for (int i = 0; i < size; i++) {
if (squaresOwned[i] -> getName() == command[1]) {
if (!(squaresOwned[i] -> isOwnable())) {
cout << squaresOwned[i]->getName() << "cannot be mortgaged." << endl;
} else {
squaresOwned[i] -> mortgage(this);
}
break;
}
}
} else if (command[0] == "improve" && command.size()>=3 && command[2]=="sell") {
int size = squaresOwned.size();
for (int i = 0; i < size; i++) {
if (squaresOwned[i] -> getName() == command[1]) {
if (!(squaresOwned[i] -> isImprovable()) || squaresOwned[i] -> getImprovementLevel() < 1) {
cout << squaresOwned[i]->getName() << "has no improvements." << endl;
} else {
squaresOwned[i] -> improveSell(this);
}
break;
}
}
} else if (command[0] == "trade" && command.size()>=4) {
int index=-1;
int size = otherPlayers.size();
for (int i=0; i<size; i++){
if (otherPlayers[i]->getName()==command[1]){
index = i;
break;
}
}
if (index==-1){
cout << "Player: " << command[1] <<" doesn't exist" << endl;
continue;
}
Player* other = otherPlayers[index].get();
int value=-1;
try{
value = stoi(command[3]);
}
catch(exception &err){}
int value2=-1;
try{
value2 = stoi(command[2]);
}
catch(exception &err){}
if (value>-1 && value2>-1){
cout << "You can't trade money for money" << endl;
continue;
}
if (value>-1){
int numPropOwned = squaresOwned.size();
int square_index=-1;
string accept="";
for (int i=0; i<numPropOwned; i++){
if (squaresOwned[i]->getName()==command[2]){
square_index=i;
break;
}
}
if (square_index==-1){
cout << "You don't own "<< command[2] <<"." << endl;
continue;
}
if (squaresOwned[square_index]->isImprovable()){
string mono_block = squaresOwned[square_index]->getMonopolyBlock();
bool failed=false;
for (int i=0; i<numPropOwned; i++){
if (squaresOwned[i]->getMonopolyBlock()==mono_block && squaresOwned[i]->getImprovementLevel()>0){
cout << squaresOwned[i]->getName() << " has improvements." << endl;
cout << "Cannot trade when the property, or properties in the same block, have improvements" << endl;
failed = true;
break;
}
}
if (failed){
continue;
}
}
if (other->getMoney()<value){
cout << command[1] << " doesn't have enough money" << endl;
cout<<"The other player doesn't have enough money"<<endl;
continue;
}
cout << "Does " << command[1] << " accept the trade (type accept to accept)" << endl;
getline(cin,accept);
if (accept!="accept"){
cout << command[1] << " has not accepted." << endl;
continue;
}
transferProperty(other, squaresOwned[square_index], otherPlayers);
other->subtractMoney(value, otherPlayers);
addMoney(value);
}
else{
int numPropOwned = squaresOwned.size();
int square_index=-1;
string accept="";
for (int i=0; i<numPropOwned; i++){
if (squaresOwned[i]->getName()==command[2]){
square_index=i;
break;
}
}
if (square_index==-1){
cout << "You don't own "<< command[2] <<"." << endl;
continue;
}
if (squaresOwned[square_index]->isImprovable()){
string mono_block = squaresOwned[square_index]->getMonopolyBlock();
bool failed=false;
for (int i=0; i<numPropOwned; i++){
if (squaresOwned[i]->getMonopolyBlock()==mono_block && squaresOwned[i]->getImprovementLevel()>0){
cout << squaresOwned[i]->getName() << " has improvements." << endl;
cout << "Cannot trade when the property, or properties in the same block, have improvements" << endl;
failed=true;
break;
}
}
if(failed){
continue;
}
}
vector<shared_ptr<Square>> otherSquares = other->getSquares();
int numPropOwnedOther = otherSquares.size();
int square_index_other=-1;
for (int i=0; i<numPropOwnedOther; i++){
if (otherSquares[i]->getName()==command[3]){
square_index_other=i;
break;
}
}
if (square_index_other==-1){
cout<<command[1] <<" doesn't own " << command[3] << "." <<endl;
continue;
}
if (otherSquares[square_index_other]->isImprovable()){
string mono_block = otherSquares[square_index_other]->getMonopolyBlock();
bool failed=false;
for (int i=0; i<numPropOwnedOther; i++){
if (otherSquares[i]->getMonopolyBlock()==mono_block && otherSquares[i]->getImprovementLevel()>0){
cout << otherSquares[i]->getName() << " has improvements." << endl;
cout << "Cannot trade when the property, or properties in the same block, have improvements" << endl;
failed=true;
break;
}
}
if(failed){
continue;
}
}
cout << "Does " << command[1] << " accept the trade (type accept to accept)" << endl;
getline(cin,accept);
if (accept!="accept"){
cout << command[1] << " has not accepted." << endl;
continue;
}
transferProperty(other, squaresOwned[square_index], otherPlayers);
other->transferProperty(this,otherSquares[square_index_other], otherPlayers);
}
}
else if (command[0] == "all") {
int numPlayers = otherPlayers.size();
for (int i = 0; i < numPlayers; i++) {
otherPlayers[i] -> printPlayerAssets();
cout << endl;
}
}
cout << "Enter a valid command: " << endl;
}
money -= amount;
cout << name << " now has a balance of " << money << " after raising enough money and completing the transaction." << endl;
}
}
void Player::addSquare(std::shared_ptr<Square> square){
cout << square->getName() << " given to " << name << "." << endl;
squaresOwned.insert(squaresOwned.end(), square);
}
void Player::transferMoney(Player* to, int amount, std::vector<shared_ptr<Player>> otherPlayers){
try{
subtractMoney(amount, otherPlayers);
to->addMoney(amount);
cout << amount << " transferred from " << name << " to " << to->getName() << "." << endl;
}
catch(const invalid_argument& ia){
cerr << ia.what() << endl;
}
}
void Player::transferProperty(Player* to, std::shared_ptr<Square> square, vector<shared_ptr<Player>> players){
int found=-1;
int size = squaresOwned.size();
for (int i=0; i<size; i++){
if (squaresOwned[i]==square){
found = i;
break;
}
}
if (found==-1){
cout << square->getName() << " not found. " << endl;
return;
}
if (square->isMortgaged()){
cout << square->getName() << " was mortgaged, so the receipient paid 10 percent upfront." << endl;
to->subtractMoney(square->getCost()*0.1, players);
}
if (square->isOwnable() && !square->isImprovable()){
if (square->isGym()){
numOfGyms--;
to->addGym();
}
else{
numOfResidences--;
to->addRes();
}
}
squaresOwned.erase(squaresOwned.begin()+found);
to->addSquare(square);
square->setOwner(to);
cout << square->getName() << " transferred from " << name << " to " << to->getName() << "." << endl;
}