-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.cc
655 lines (570 loc) · 22.4 KB
/
commands.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
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#include "commands.h"
using namespace std;
// methods
void followWhenInsufficientFunds(vector<shared_ptr<Player>> group,
shared_ptr<Player> curPlayer, shared_ptr<Board> b, shared_ptr<Player> propOwner,
bool payBank){
cout << "Please select one of the available commands to increase your funds, or declare bankruptcy." << endl;
cout << "Available commands [\"bankrupt\",\"mortage\",\"improve\"] with their according procedure" << endl;
string action;
cin >> action;
// call other command here
if (action == BANKRUPT)
{
if (!payBank){
curPlayer->setBankruptStatus(true);
followBankruptCommandWithPlayer(curPlayer, propOwner);
}
}
else if (action == TRADE)
{
followTradeCommand(group, curPlayer);
}
else if (action == MORTGAGE)
{
followMortgageCommand(curPlayer);
}
else if (action == IMPROVE)
{
followImproveCommand(group, curPlayer, b);
}
}
void followRollCommand(vector<shared_ptr<Player>> group,
shared_ptr<Player> curPlayer, bool testMode,
shared_ptr<Board> b)
{
// inner helper function
auto pointerOfPlayer = [group](char trackingPiece) {
int sizeGroup = group.size();
shared_ptr<Player> result;
for (int i = 0; i < sizeGroup; i++)
{
if (group[i]->getGamePiece() == trackingPiece)
{
return group[i];
}
}
cout << "why trackingPiece is not in this player group? hmmm..." << endl;
// this will return empty share_ptr of player
return result;
};
// we want to know if they are stepping on the ownable or unownable property to act accordingly
string steppingSquare = curPlayer->getSquareAtCurrPos();
if (isOwnableBlock(steppingSquare))
{ // they're on an ownable block
// check if this block is owned by someone
if (Transactions::isInOwnedList(steppingSquare))
{ // this property belongs to someone, pay rent
shared_ptr<Ownable> propPointer = Transactions::pointerOfProp(steppingSquare);
shared_ptr<Player> propOwner = pointerOfPlayer(propPointer->getOwner());
cout << "You are stepping on " << steppingSquare << ", which belongs to ";
if (propOwner == curPlayer)
{
cout << propOwner->getName() << ". That's you! No payment required." << endl;
}
else if (propOwner != curPlayer && !propPointer->getMortStatus())
{
// ^check if the property belongs to the current player and not mortgaged
cout << propOwner->getName() << ", let's pay fee" << endl;
if (isGym(propPointer->getName()))
{
cout << "You landed on a Gym. Please roll again to calculate the fee you owe." << endl;
auto gym = std::dynamic_pointer_cast<Gym>(propPointer);
int roll = 0;
bool rollOverload = false;
string command;
cin >> command;
while (command != ROLL)
{
cout << "Sorry, you have to roll first." << endl;
cin >> command;
}
if (testMode)
{
std::string die1;
std::string die2;
cin >> die1;
if (!cin.fail() && isNumber(die1))
{
cin >> die2;
if (!cin.fail() && isNumber(die2))
{
roll = std::stoi(die1) + std::stoi(die2);
rollOverload = true;
}
gym->setRoll(roll);
}
}
if (!rollOverload)
{
auto twoDices = make_unique<Dice>();
cout << "Rolling your dice ........" << endl;
cout << "and you get ";
cout << twoDices->getDie1() << " + ";
cout << twoDices->getDie2() << " = ";
cout << twoDices->diceSum() << "!" << endl;
gym->setRoll(twoDices->diceSum());
}
}
// pay rent action
while (!Transactions::payPlayer(curPlayer, propOwner, propPointer->amountToPay()))
{ //if not success paying
cout << "You don't have enough money to pay the rent. Please make other commands to gain more funds or \"bankrupt\" to declare bankruptcy" << endl;
string action;
cin >> action;
followWhenInsufficientFunds(group, curPlayer, b, propOwner, false);
}
if (!curPlayer->getBankruptStatus()){
cout << "fee pay successfully" << endl;
}
}
}
else
{ // buy or auction
cout << "You are stepping on " << steppingSquare << ", you can buy this property!" << endl;
cout << "type \"buy\" to buy or type anything to let the bank auction this property." << endl;
string input;
cin >> input;
if (input == BUY)
{
while (!Transactions::buyProperty(steppingSquare, curPlayer))
{
cout << "You don't have enough money to buy this property. Make command TRADE or MORTGAGE to gain more funds" << endl;
cout << "(type \"auction\" to stop BUYING action - the bank will auction this property)" << endl;
cin >> input;
if (input == AUCTION)
{
// run the auction command
followAuctionCommand(group, curPlayer, steppingSquare);
break;
}
if (input == TRADE)
{
followTradeCommand(group, curPlayer);
}
if (input == MORTGAGE)
{
followMortgageCommand(curPlayer);
}
if (input == IMPROVE)
{
followImproveCommand(group, curPlayer, b);
}
}
curPlayer->printOwnedProp();
}
else
{
// run the auction
followAuctionCommand(group, curPlayer, steppingSquare);
}
}
}
else
{ // they're on an unownable block
if (steppingSquare == "SLC"){
cout << "You have arrived at the SLC!" << endl;
SLC::determinePlayerPos(curPlayer);
b->movePlayer(curPlayer->getGamePiece(), curPlayer->getCurrPos());
b->drawBoard();
followRollCommand(group, curPlayer, testMode, b);
}
else if (steppingSquare == "DC Tims Line"){
cout << "You have arrived at DC Tims Line." << endl;
cout << "You get your coffee immediately." << endl;
}
else if (steppingSquare == "GO TO TIMS") {
cout << "After pulling an all-nighter, you need coffee." << endl;
cout << "Unfortunately, the line is a mile long." << endl;
curPlayer->moveToDCTims();
}
else if (steppingSquare == "NEEDLES HALL") {
cout << "You have arrived at Needles Hall." << endl;
int changeInFunds = MonetaryServices::needlesHall(curPlayer);
if (changeInFunds < 0){
// if player does not have enough money
while (!Transactions::payBank(curPlayer, changeInFunds)){
cout << "Insufficient funds!" << endl;
followWhenInsufficientFunds(group, curPlayer, b, nullptr, true);
}
if (!curPlayer->getBankruptStatus()){
cout << "Successfully paid!" << endl;
}
} else {
curPlayer->addFund(changeInFunds);
}
} else { // step on payment square
int payment = 0;
if (steppingSquare == "TUITION"){
payment = MonetaryServices::payTuition(curPlayer);
} else if (steppingSquare == "COOP FEE"){
int payment = MonetaryServices::payCoop();
cout << "You are assessed a $" << payment << " co-op fee." << endl;
} else if (steppingSquare == "Goose Nesting"){
cout << "You are attacked by a flock of geese, but nothing ";
cout << "else happens." << endl;
}
// if player does not have enough money
while (!Transactions::payBank(curPlayer, payment)){
cout << "Insufficient funds!" << endl;
// helper
followWhenInsufficientFunds(group, curPlayer, b, nullptr, true);
}
if (!curPlayer->getBankruptStatus()){
cout << "Successfully paid!" << endl;
}
}
}
}
// helper function
bool isNumber(string a)
{
int size = a.size();
string initial = "";
for (int i = 0; i < size; i++)
{
char each = a[i];
initial += each;
try
{
stoi(initial);
initial = "";
}
catch (invalid_argument err)
{
return false;
}
}
return true;
}
void followTradeCommand(vector<shared_ptr<Player>> group, shared_ptr<Player> curPlayer)
{
string receiver, offer, want;
int sizeGroup = group.size();
cout << "--> Please enter the name of the player you want to trade with. List of remaining players and the game piece" << endl;
for (int i = 0; i < sizeGroup; i++) {
if (group[i] == curPlayer) continue;
cout << " ~> name:" << group[i]->getName() << " - piece:" << group[i]->getGamePiece() << endl;
}
cin >> receiver;
shared_ptr<Player> pointerReceiver;
bool validateReceiver = false;
for (int i = 0; i < sizeGroup; i++)
{
if (group[i]->getName() == receiver)
{
pointerReceiver = group[i];
validateReceiver = true;
}
}
// validate this transaction
if (!validateReceiver)
{
cout << "The player name doesn't exist" << endl;
cout << "Abort trading!" << endl;
return;
}
cout << "--> You are now trading with " << pointerReceiver->getName() << " <--" << endl;
cout << endl;
cout << "--> Enter the name of item or the amount of money you want to offer" << endl;
cout << "--> here is the list of your properties" << " <--" << endl;
int sizeListY = curPlayer->getOwnedPropList().size();
for (int i = 0; i < sizeListY; i++) {
cout << "~> " << curPlayer->getOwnedPropList()[i]->getName();
cout << " - original cost:" << curPlayer->getOwnedPropList()[i]->getCostToBuy() << endl;
}
cin >> offer;
cout << "--> Enter the name of item or the amount of money you want to take" << endl;
cout << "--> here is the list of properties of " << pointerReceiver->getName() << " <--" << endl;
int sizeListR = pointerReceiver->getOwnedPropList().size();
for (int i = 0; i < sizeListR; i++) {
cout << "~> " << pointerReceiver->getOwnedPropList()[i]->getName();
cout << " - original cost:" << pointerReceiver->getOwnedPropList()[i]->getCostToBuy() << endl;
}
cin >> want;
if (isNumber(offer) && isNumber(want))
{
cout << "*** Please don't offer money for money ***" << endl;
return;
}
if ( !isNumber(offer))
{
if (!isOwnableBlock(offer))
{
cout << "*** Your offer item is not either a property or money ***" << endl;
cout << "--> Abort trading! <--" << endl;
return;
}
}
if (!isNumber(want))
{
if (!isOwnableBlock(want))
{
cout << "*** Your wanted item is not either a property or money ***" << endl;
cout << "--> Abort trading! <--" << endl;
return;
}
}
string permission;
cout << "========================= IMPORTANT =========================" << endl;
cout << "--> " << curPlayer->getName() << " wants to trade "<< offer << " with ";
cout << pointerReceiver->getName() << " for " << want << " <--" << endl;
cout << "Hi " << pointerReceiver->getName() <<"! please type YES to make the transaction, type otherwise to cancel" << endl;
cout << "=============================================================" << endl;
cin >> permission;
if (permission != "YES") {
cout << "--> This trading action has been cancel by " << pointerReceiver->getName() << " <--" << endl;
return;
}
if (isNumber(offer))
{
if (!Transactions::tradeMforP(curPlayer, pointerReceiver, stoi(offer), Transactions::pointerOfProp(want)))
{
cout << "--> Abort trading! <--" << endl;
}
return;
}
if (isNumber(want))
{
if (!Transactions::tradePforM(curPlayer, pointerReceiver, Transactions::pointerOfProp(offer), stoi(want)))
{
cout << "--> Abort trading! <--" << endl;
}
return;
}
if (!Transactions::tradePforP(curPlayer, pointerReceiver, Transactions::pointerOfProp(offer), Transactions::pointerOfProp(want)))
{
cout << "--> Abort trading! <--" << endl;
return;
}
}
void followImproveCommand(vector<shared_ptr<Player>> group, shared_ptr<Player> curPlayer,
std::shared_ptr<Board> b)
{
string property, action;
cout << "--> Hi " << curPlayer->getName() << " please choose your property to improve <--" << endl;
int sizeListY = curPlayer->getOwnedPropList().size();
if (sizeListY == 0) {
cout << "--> Unfortunately, you don't have any property to make improvement <--" << endl;
cout << "--> Abort Improvement action! <--" << endl;
return;
}
cout << "--> here is the list of your properties" << " <--" << endl;
for (int i = 0; i < sizeListY; i++) {
cout << "~> " << curPlayer->getOwnedPropList()[i]->getName() << endl;
cout << " - original cost:" << curPlayer->getOwnedPropList()[i]->getCostToBuy() << endl;
cout << " - buy improvement cost:" << costToImprProp(curPlayer->getOwnedPropList()[i]->getName()) << endl;
cout << " - sell improvement price:" << costToImprProp(curPlayer->getOwnedPropList()[i]->getName()) * 0.5 << endl;
}
cin >> property;
cout << "--> please type in \"buy\" or \"sell\" action" << endl;
cin >> action;
while (true) {
if (action == BUY || action == SELL || cin.fail()) break;
cout << "--> unregconize action for improvement command! <--" << endl;
cout << "--> please type in \"buy\" or \"sell\" action" << endl;
cin >> action;
}
shared_ptr<Ownable> pointerProperty;
if (!Transactions::isInOwnedList(property))
{
cout << "The player doesn't own this property or this property doesn't exist" << endl;
cout << "Abort buying/selling improvement!" << endl;
return;
}
else
{
pointerProperty = Transactions::pointerOfProp(property);
}
if (action == BUY)
{
if (!Transactions::improveProperty(pointerProperty, curPlayer))
{
cout << "Abort buying/selling improvement!" << endl;
}
else
{
b->addImpr(pointerProperty->getName());
b->drawBoard();
}
}
else if (action == SELL)
{
if (!Transactions::sellImprove(pointerProperty, curPlayer))
{
cout << "Abort buying/selling improvement!" << endl;
}
else
{
b->removeImpr(pointerProperty->getName());
b->drawBoard();
}
}
else
{
cout << "Unregconized action command" << endl;
cout << "Abort buying/selling improvement!" << endl;
}
}
void followMortgageCommand(shared_ptr<Player> curPlayer)
{
cout << "--> Hi " << curPlayer->getName() << " please choose your property to mortgage <--" << endl;
int sizeListY = curPlayer->getOwnedPropList().size();
if (sizeListY == 0) {
cout << "--> Unfortunately, you don't have any property to make mortgage <--" << endl;
cout << "--> Abort Mortgage action! <--" << endl;
return;
}
cout << "--> here is the list of your properties" << " <--" << endl;
for (int i = 0; i < sizeListY; i++) {
cout << "~> " << curPlayer->getOwnedPropList()[i]->getName() << endl;
cout << " - original cost:" << curPlayer->getOwnedPropList()[i]->getCostToBuy() << endl;
cout << " - mortgage price:" << curPlayer->getOwnedPropList()[i]->getCostToBuy() * 0.5 << endl;
}
string property;
cin >> property;
shared_ptr<Ownable> pointerProperty;
if (!Transactions::isInOwnedList(property))
{
cout << "The player doesn't own this property or this property doesn't exist" << endl;
cout << "Abort mortgage action!" << endl;
return;
}
else
{
pointerProperty = Transactions::pointerOfProp(property);
}
if (!Transactions::mortgageProperty(pointerProperty, curPlayer))
{
cout << "Abort mortgage action!" << endl;
}
}
void followUnmortgageCommand(shared_ptr<Player> curPlayer)
{
cout << "--> Hi " << curPlayer->getName() << " please choose your property to unmortgage <--" << endl;
int sizeListY = curPlayer->getOwnedPropList().size();
if (sizeListY == 0) {
cout << "--> Unfortunately, you don't have any property to make unmortgage <--" << endl;
cout << "--> Abort Unmortgage action! <--" << endl;
return;
}
cout << "--> here is the list of your properties" << " <--" << endl;
for (int i = 0; i < sizeListY; i++) {
cout << "~> " << curPlayer->getOwnedPropList()[i]->getName() << endl;
cout << " - original cost:" << curPlayer->getOwnedPropList()[i]->getCostToBuy() << endl;
cout << " - unmortgage cost:" << curPlayer->getOwnedPropList()[i]->getCostToBuy() * 0.6 << endl;
}
string property;
cin >> property;
shared_ptr<Ownable> pointerProperty;
if (!Transactions::isInOwnedList(property))
{
cout << "The player doesn't own this property or this property doesn't exist" << endl;
cout << "Abort unmortgage action!" << endl;
return;
}
else
{
pointerProperty = Transactions::pointerOfProp(property);
}
if (!Transactions::unmortgageProperty(pointerProperty, curPlayer))
{
cout << "Abort unmortgage action!" << endl;
}
}
void followAuctionCommand(std::vector<std::shared_ptr<Player>> group, std::shared_ptr<Player> curPlayer, std::string ownableItem)
{
// make new auction
cout << endl;
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
cout << "The BANK is now running auction on " << ownableItem << " property" << endl;
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
cout << "Each player is now asked for bidding decision" << endl << endl;
// setup before bidding
auto newAuction = make_shared<Auction>(group, curPlayer, ownableItem);
int curIndexPlayer = 0; // current bidder
int sizeGroup = group.size();
int numberOfBidder = sizeGroup;
vector<bool> trackingGiveUpBid;
for (int i = 0; i < sizeGroup; i++)
{
trackingGiveUpBid.push_back(false);
if (group[i] == curPlayer)
{
curIndexPlayer = i;
}
}
// we want a person who is next to the one calling auction bid first
curIndexPlayer = (curIndexPlayer + 1) % sizeGroup;
bool nextBidder = true;
string action; // can choose between RAISE/WITHDRAW
while (true)
{
string curPlayerName = group[curIndexPlayer]->getName();
if (numberOfBidder == 1) {
cout << "Congratulation! You won this bidding section " << curPlayerName << endl;
break;
}
if (trackingGiveUpBid[curIndexPlayer])
{
curIndexPlayer = (curIndexPlayer + 1) % sizeGroup;
continue;
}
if (nextBidder) {
cout << "----------> $$$ -------------> Property ----------------> $$$$$$" << endl;
cout << "The current bidding player is " << curPlayerName << endl;
cout << "----------------------------------------------------------------" << endl << endl;
nextBidder = false;
}
cout << "--> Please choose your action \"raise\" or \"withdraw\" for the bid " << curPlayerName << endl;
cin >> action;
if (cin.fail()) break;
if (action == RAISE)
{
std::string amount;
cout << "--> please enter the amount you want to bid" << endl;
cin >> amount;
while (!isNumber(amount))
{
cout << "--> The bidding amount have to be a number" << endl;
cout << "--> Please enter the amount you want to raise " << curPlayerName << endl;
cin >> amount;
}
if (!newAuction->placeBid(group[curIndexPlayer], stoi(amount))) {
cout << "--> Bid has not been placed, please try \"raise\" again or type \"withdraw\" to withdraw from auction " << curPlayerName << endl;
continue;
}
curIndexPlayer = (curIndexPlayer + 1) % sizeGroup;
nextBidder = true;
}
else if (action == WITHDRAW)
{
newAuction->withdrawBid(group[curIndexPlayer]);
trackingGiveUpBid[curIndexPlayer] = true;
curIndexPlayer = (curIndexPlayer + 1) % sizeGroup;
nextBidder = true;
numberOfBidder--;
}
else
{
cout << "unregconized action for bidding, the BANK will be prompted to ask you again" << endl << endl;
}
}
}
void followBankruptCommandWithPlayer(std::shared_ptr<Player> curPlayer, std::shared_ptr<Player> toPlayer)
{
curPlayer->setBankruptStatus(true);
// take all their funds
toPlayer->addFund(curPlayer->getFunds());
// take all their property and re-set the owner
vector<shared_ptr<Ownable>> propListToTransfer = curPlayer->getOwnedPropList();
int sizeList = propListToTransfer.size();
for (int i = 0; i < sizeList; i++)
{
toPlayer->addProp(propListToTransfer[i]);
propListToTransfer[i]->setOwner(toPlayer->getGamePiece());
}
}
void followBankruptCommandWithBank(std::shared_ptr<Player> curPlayer)
{
}