-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonopoly.cpp
More file actions
1412 lines (1349 loc) · 65.3 KB
/
monopoly.cpp
File metadata and controls
1412 lines (1349 loc) · 65.3 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
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <chrono>
#include <iostream>
#include <limits>
#include <ostream>
#include <random>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
// Color groups for properties
enum ColorGroup {
NONE,
// colors
BROWN,
BLUE,
PINK,
ORANGE,
RED,
YELLOW,
GREEN,
DARK_BLUE,
GRAY,
PURPLE
};
// Symbols for players
const std::vector<std::string> availableSmybols = {
u8"♠",u8"♣",u8"♥",u8"♦",u8"●",u8"○",u8"■",u8"□",
u8"▲",u8"▼",u8"◆",u8"◇",u8"★",u8"☆",u8"✪",u8"✦",u8"✧",u8"✚",u8"✖",
u8"♜",u8"♞",u8"♝",u8"♛",u8"♚"
};
// Map display names to fully expanded ColorGroup
const std::vector<std::pair<std::string, ColorGroup>> availableColors = {
{"Default", NONE},
// colors
{"Brown", BROWN},
{"Blue", BLUE},
{"Pink", PINK},
{"Orange", ORANGE},
{"Red", RED},
{"Yellow", YELLOW},
{"Green", GREEN},
{"Dark Blue", DARK_BLUE},
{"Gray", GRAY},
{"Purple", PURPLE}
};
// ANSI escape codes for each color
const std::vector<std::pair<std::string, ColorGroup>> colorCodes = {
{"\033[0m", NONE},// None - Reset
// colors
{"\033[38;2;102;51;0m", BROWN},
{"\033[38;2;0;204;255m", BLUE},
{"\033[38;2;255;0;255m", PINK},
{"\033[38;2;255;102;0m", ORANGE},
{"\033[38;2;255;0;0m", RED},
{"\033[38;2;255;255;0m", YELLOW},
{"\033[38;2;0;204;0m", GREEN},
{"\033[38;2;77;77;255m", DARK_BLUE},
{"\033[38;2;77;77;77m", GRAY},
{"\033[38;2;102;0;255m", PURPLE}
};
void clearInputBuffer() {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
void clearTerminal() {
fputs("\x1b[1;1H\x1b[2J\x1b[3J", stdout);
fflush(stdout);
}
//Global Variables
const std::string RESET_COLOR = "\033[0m";
#include "player.hpp"
#include "tile.hpp"
#include "card.hpp"
#include "FileExport.hpp"
#include "FileImport.hpp"
std::vector<tile> gameBoard;
std::vector<player> players;
std::vector<card> communityCards;
std::vector<card> chanceCards;
std::random_device rd;
std::mt19937 gen(rd());
int currentPlayerTurn = 0;
int freeParkingFunds = 0;
int communityCardCounter = 0;
int chanceCardCounter = 0;
void movePlayer(int s, player &p, bool &ok, std::string message);
void bankruptcy(player &p, int targetID, int amount);
void displayGameBoard(){
clearTerminal();
//Map players to positions
//10 = Free
//40 = Jail
std::array<std::vector<std::string>, 41> positionMap;
for (const player& p : players) {
if (p.jailed) {
positionMap[40].push_back(p.symbol);
continue;
}
positionMap[p.currentPosition].push_back(p.symbol);
}
//Raw Gameboard String
std::string board = u8R"(┌────────────┬────────────┬────────────┬────────────┬────────────┬────────────┬────────────┬────────────┬────────────┬────────────┬────────────┐
│ 20 FP │ 21 KENTUCKY│ 22 CHANCE │ 23 INDIANA │ 24 ILLINOIS│ 25 B&O RR │ 26 ATLANTIC│ 27 VENTNOR │ 28 WATER │ 29 MARVIN │ 30 GOTOJAIL│
│ │ [O21] │ │ [O23] │ [O24] │ [O25] │ [O26] │ [O27] │ [O28] │ [O29] │ │
│ [20] │ [21] │ [22] │ [23] │ [24] │ [25] │ [26] │ [27] │ [28] │ [29] │ [30] │
│ │ │ │ │ │ │ │ │ │ │ │
├────────────┼────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴────────────┼────────────┤
│ 19 NEWYORK │ │ 31 PACIFIC │
│ [O19] │ │ [O31] │
│ [19] │ │ [31] │
│ │ │ │
├────────────┤ ├────────────┤
│ 18 TENNES │ │ 32 N CAROL │
│ [O18] │ │ [O32] │
│ [18] │ │ [32] │
│ │ │ │
├────────────┤ ├────────────┤
│ 17 CC │ │ 33 CC │
│ │ │ │
│ [17] │ │ [33] │
│ │ │ │
├────────────┤ ├────────────┤
│ 16 STJAMES │ │ 34 PENN AV │
│ [O16] │ │ [O34] │
│ [16] │ │ [34] │
│ │ │ │
├────────────┤ ├────────────┤
│ 15 PA RR │ │ 35 SHORT RR│
│ [O15] │ │ [O35] │
│ [15] │ │ [35] │
│ │ │ │
├────────────┤ ├────────────┤
│ 14 VIRGINIA│ │ 36 CHANCE │
│ [O14] │ │ │
│ [14] │ │ [36] │
│ │ │ │
├────────────┤ ├────────────┤
│ 13 STATES │ │ 37 PARK PLC│
│ [O13] │ │ [O37] │
│ [13] │ │ [37] │
│ │ │ │
├────────────┤ ├────────────┤
│ 12 ELECTRIC│ │ 38 LUXURY │
│ [O12] │ │ │
│ [12] │ │ [38] │
│ │ │ │
├────────────┤ ├────────────┤
│ 11 ST CHAR │ │ 39 BOARD W │
│ [O11] │ │ [O39] │
│ [11] │ │ [39] │
│ │ │ │
├───┬────────┼────────────┬────────────┬────────────┬────────────┬────────────┬────────────┬────────────┬────────────┬────────────┼────────────┤
│ F │ 10 JAIL│ 9 CONNET │ 8 VERMONT │ 7 CHANCE │ 6 ORIENTAL │ 5 READ RR │ 4 INCOME │ 3 BALTIC │ 2 CC │ 1 MED AVE │ 0 GO │
│ R │[40]│ [O09] │ [O08] │ │ [O06] │ [O05] │ │ [O03] │ │ [O01] │ │
│ E └────────┤ [09] │ [08] │ [07] │ [06] │ [05] │ [04] │ [03] │ [02] │ [01] │ [00] │
│ E [10] │ │ │ │ │ │ │ │ │ │ │
└────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴────────────┘)";
// Replace placeholders with player symbols
for (int i = 0; i < 41; ++i) {
// Create placeholder string [XX] format with leading 0's if needed
std::string placeholder = "[" + std::string(i < 10 ? "0" : "") + std::to_string(i) + "]";
std::string replacement;
// Append player symbols at this position, count number of players in field
int counter = 0;
for (const auto& symbol : positionMap[i]) {
counter++;
replacement += symbol;
}
// Fill remaining space with spaces to keep field width consistent
while (counter < 6) {
replacement += " ";
counter++;
}
replacement = "[" + replacement + "]";
// Replace in board string
size_t pos = board.find(placeholder);
if (pos != std::string::npos) {
board.replace(pos, 4, replacement);
}
}
// Replace player symbols with colored versions
for (const player& p : players) {
std::string replacement = colorCodes[p.color].first + p.symbol + RESET_COLOR;
size_t pos = board.find(p.symbol);
if (pos != std::string::npos) {
board.replace(pos, p.symbol.length(), replacement);
}
}
// Replace tile short names with colored versions
for (const tile& t : gameBoard) {
std::string replacement = " " + colorCodes[t.color].first + t.shortName + RESET_COLOR;
size_t pos = board.find(" " + t.shortName );
if (pos != std::string::npos) {
board.replace(pos, t.shortName.length() + 1, replacement);
}
std::string placeholderOwned = "[O" + std::string(t.tileIndex < 10 ? "0" : "") + std::to_string(t.tileIndex) + "]";
std::string ownedStatus;
// Place owned / houses / hotel status
size_t posStatus = board.find(placeholderOwned);
if (posStatus != std::string::npos) {
if (t.ownerId == -1) {
ownedStatus = " ";
} else {
std::string color = colorCodes[players[t.ownerId].color].first;
if (t.isMortgaged) {
ownedStatus = color + "MORTG" + RESET_COLOR;
} else {
switch (t.upgradeStage){
case 1:
ownedStatus = color + "⌂ " + RESET_COLOR;
break;
case 2:
ownedStatus = color + "⌂⌂ " + RESET_COLOR;
break;
case 3:
ownedStatus = color + "⌂⌂⌂ " + RESET_COLOR;
break;
case 4:
ownedStatus = color + "⌂⌂⌂⌂ " + RESET_COLOR;
break;
case 5:
ownedStatus = color + "HOTEL" + RESET_COLOR;
break;
default:
ownedStatus = color + "OWNED" + RESET_COLOR;
break;
}
}
}
board.replace(posStatus, 5, ownedStatus);
}
}
std::cout << board << std::endl;
}
// calculate rent for utilities based on rolled dice value and owned utilities
int calculateUtilityRent(tile& currentTile, int diceRoll)
{
int utilitiesOwned = 0;
for (const auto& t : gameBoard) {
if (t.ownerId == currentTile.ownerId && (t.tileIndex == 12 || t.tileIndex == 28)) {
utilitiesOwned++;
}
}
if (utilitiesOwned == 1)
return diceRoll * 4;
if (utilitiesOwned == 2)
return diceRoll * 10;
return 0;
}
// calculate rent for railroads based on number of owned railroads
int calculateRailroadRent(tile& currentTile)
{
int railroadsOwned = 0;
for (const auto& t : gameBoard) {
if (t.ownerId == currentTile.ownerId && (t.tileIndex == 5 || t.tileIndex == 15 || t.tileIndex == 25 || t.tileIndex == 35)) {
railroadsOwned++;
}
}
switch (railroadsOwned) {
case 1: return 25;
case 2: return 50;
case 3: return 100;
case 4: return 200;
default: return 0;
}
}
// check if player owns all properties in the color group
bool ownsMonopoly(tile& currentTile)
{
for (const auto& t : gameBoard) {
if (t.color == currentTile.color && t.ownerId != currentTile.ownerId) {
return false;
}
}
return true;
}
// calculate rent for properties based on upgrade stage and monopoly status
int calculatePropertyRent(tile& t)
{
if (t.isMortgaged) {
return 0;
}
if (t.upgradeStage == 0)
return ownsMonopoly(t) ? t.price0 * 2 : t.price0;
switch (t.upgradeStage) {
case 1: return t.price1;
case 2: return t.price2;
case 3: return t.price3;
case 4: return t.price4;
case 5: return t.price5; // hotel
default: return 0;
}
}
// roll two six-sided dice
int rollDice(){
std::uniform_int_distribution<> dis(1,6);
return dis(gen);
}
// check if both dice have the same value
bool checkPasch(int &x, int &y){
if(x != y){
return false;
}else{
return true;
}
}
// arrest player and move to jail
void arrest(player &p, bool &ok){
p.jailed = true;
p.currentPosition = 10; //Jail Position
ok = true;
displayGameBoard();
std::cout<<"You have been arrested! 😡"<<std::endl;
}
// visual representation of dice roll for output in the Terminal
std::string visualDice(int &x){
switch (x) {
case 1:{
return "⚀ 1 ";
}
case 2:{
return "⚁ 2 ";
}
case 3:{
return "⚂ 3 ";
}
case 4:{
return "⚃ 4 ";
}
case 5:{
return "⚄ 5 ";
}
case 6:{
return "⚅ 6 ";
}
default:{
return "Invalid Dice Value";
}
}
}
void transferTile(player &from, int targetID, std::vector<int> &tiles){
for(int t : tiles){
auto it = std::find(from.ownedStreets.begin(),from.ownedStreets.end(),t);
if(it != from.ownedStreets.end()){
from.ownedStreets.erase(it);
}
players[targetID].ownedStreets.push_back(t);
gameBoard[t].ownerId = targetID;
}
}
// transfer money between players
void transferMoney(player &from, int targetID, int amount){
// implement bancruptcy logic
// implement logic to handle transfer from/to bank/free parking?
if (from.money < amount) {
bankruptcy(from, targetID, amount);
}
from.money -= amount;
if (targetID == -1) {
freeParkingFunds += amount;
return;
}
players[targetID].money = players[targetID].money + amount;
}
// draw a card from the specified deck and apply its effects
void drawCard(std::string type, player& player, bool& ok) {
card* currentCard = nullptr;
// Get current Card, shuffle if every card was used once, tracked per deck
if (type == "chance") {
if (chanceCardCounter == chanceCards.size()) {
chanceCardCounter = 0;
std::shuffle(chanceCards.begin(), chanceCards.end(), gen);
}
currentCard = &chanceCards[chanceCardCounter++];
}
else if (type == "community") {
if (communityCardCounter == communityCards.size()) {
communityCardCounter = 0;
std::shuffle(communityCards.begin(), communityCards.end(), gen);
}
currentCard = &communityCards[communityCardCounter++];
}
// Safety check to see if a card was drawn, crashes program since not handled by design
if (!currentCard) {
throw std::runtime_error("No card drawn!");
}
// Display drawn card
std::cout << "You drew a " << type << " card: " << currentCard->text << ".\nPress enter to continue..." << std::endl;
std::cin.get();
clearInputBuffer();
// Apply card effects
// receive money from bank
if (currentCard->action == "receive") {
player.money += std::stoi(currentCard->value.at("amount"));
}
// pay money to bank
else if (currentCard->action == "pay") {
transferMoney(player, -1, std::stoi(currentCard->value.at("amount")));
freeParkingFunds += std::stoi(currentCard->value.at("amount"));
}
// move player to specified position
else if (currentCard->action == "move") {
if (currentCard->value.at("position") == "-3") {
movePlayer(std::stoi(currentCard->value.at("position")), player, ok, "You moved 3 spaces back!");
}
else {
movePlayer(
(std::stoi(currentCard->value.at("position")) - player.currentPosition + 40) % 40,
player,
ok,
"You moved to position " + gameBoard[std::stoi(currentCard->value.at("position"))].tileName + "!"
);
}
}
// give player a Get Out of Jail Free card
else if (currentCard->action == "jailFree") {
player.jailFreeCard += 1;
}
// send player to jail
else if (currentCard->action == "jail") {
arrest(player, ok);
}
// receive money from all other players
else if (currentCard->action == "receiveFromPlayers") {
int totalAmount = 0;
for (auto &p : players) {
if (p.playerId != player.playerId && !p.bankrupt) {
int amt = std::stoi(currentCard->value.at("amount"));
totalAmount += amt;
transferMoney(p, -1, amt);
}
}
player.money += totalAmount;
}
// pay money to all other players
else if (currentCard->action == "payEach") {
int totalAmount = 0;
for (auto &p : players) {
if (p.playerId != player.playerId && !p.bankrupt) {
int amt = std::stoi(currentCard->value.at("amount"));
totalAmount += amt;
}
}
int playerID = player.playerId;
transferMoney(player, -1, totalAmount);
// duplicate loop to avoid money transfer before deduction and possible bankruptcy
for (auto &p : players) {
if (p.playerId != playerID) {
int amt = std::stoi(currentCard->value.at("amount"));
p.money += amt;
}
}
}
// move to nearest utility or railroad
else if (currentCard->action == "moveNearest") {
if (currentCard->value.at("destination") == "railroad") {
int distances[] = {5, 15, 25, 35};
int minDistance = 40;
for (int d : distances) {
int distance = (d - player.currentPosition + 40) % 40;
if (distance < minDistance) minDistance = distance;
}
movePlayer(minDistance, player, ok, "You moved to the nearest Railroad!");
}
else if (currentCard->value.at("destination") == "utility") {
int distances[] = {12, 28};
int minDistance = 40;
for (int d : distances) {
int distance = (d - player.currentPosition + 40) % 40;
if (distance < minDistance) minDistance = distance;
}
movePlayer(minDistance, player, ok, "You moved to the nearest Utility!");
}
}
// pay for repairs based on owned properties
else if (currentCard->action == "repairTax") {
int totalHouses = 0;
int totalHotels = 0;
for (const auto& t : gameBoard) {
if (t.ownerId == player.playerId) {
if (t.upgradeStage >= 1 && t.upgradeStage <= 4) totalHouses += t.upgradeStage;
else if (t.upgradeStage == 5) totalHotels += 1;
}
}
int amountDue = (totalHouses * std::stoi(currentCard->value.at("perHouse"))) +
(totalHotels * std::stoi(currentCard->value.at("perHotel")));
transferMoney(player, -1, amountDue);
freeParkingFunds += amountDue;
}
}
// move player by s spaces and handle landing on different tile types
void movePlayer(int s, player &p, bool &ok, std::string message){
if(p.currentPosition+s > 40){
p.money += 200;
}
p.currentPosition = (p.currentPosition+s)%40;
displayGameBoard();
std::cout<<message<<std::endl;
switch (p.currentPosition){
case 0:{ //Tiletyp: GO
p.money += 400;
break;
}
case 2:{ //Tiletyp: Com Chest
drawCard("community", p, ok);
break;
}
case 4:{ //Tiletyp: Income Tax
transferMoney(p, -1, 200);
break;
}
case 7:{ //Tiletyp: Chance
drawCard("chance", p, ok);
break;
}
case 10:{ //Tiletyp: visit Jail
break;
}
case 17:{ //Tiletyp: Com Chest
drawCard("community", p, ok);
break;
}
case 20:{ //Tiletyp: FreeParking
p.money += freeParkingFunds;
freeParkingFunds = 0;
break;
}
case 22:{ //Tiletyp: Chance
drawCard("chance", p, ok);
break;
}
case 30:{ //Tiletyp: GoToJail
std::cout << "You have come to the wroong neighbourhood my friend! Now the coppers will get you and take you to Jail!\nPress enter to continue..." << std::endl;
std::cin.get();
clearInputBuffer();
arrest(p,ok);
break;
}
case 33:{ //Tiletyp: Com Chest
drawCard("community", p, ok);
break;
}
case 36:{ //Tiletyp: Chance
drawCard("chance", p, ok);
break;
}
case 38:{ //Tiletyp: Luxury Tax
transferMoney(p, -1, 100);
break;
}
default:{ //Tiletyp: Streets, Trainstations, Facilities
tile& currentfield = gameBoard[p.currentPosition];
if( currentfield.ownerId == -1){ // unowned property
if(p.money >= currentfield.buyPrice){
bool correct = false;
while(!correct){
std::cout<<colorCodes[p.color].first << p.symbol << " " << p.name << RESET_COLOR
<< " Turn! Do you want to buy " << currentfield.tileName << " for " << currentfield.buyPrice << "$?\n"
<< "You currently have " << p.money << "$ in your bank." << std::endl;
std::cout
<<"┌────────┬────────┐\n"
<<"│ 1: YES │ 0: NO │\n"
<<"└────────┴────────┘\n"
<<std::endl;
int sel;
std::cin>>sel;
switch (sel) {
case 1:{
transferMoney(p, -1, currentfield.buyPrice);
currentfield.ownerId = p.playerId;
p.ownedStreets.push_back(currentfield.tileIndex);
displayGameBoard();
correct = true;
std::cout<<"Bravo you have succesfully puchased "<< currentfield.tileName <<"! 🥳" <<std::endl;
break;
}
case 0:{
displayGameBoard();
std::cout<<"Why tho? 🤨 "<<std::endl;
correct = true;
break;
}
default:{
displayGameBoard();
std::cout<<"No valid input! 😡"<<std::endl;
}
}
}
}else{
std::cout<<"Not enough Money. 😢"<<std::endl;
}
}else if (players[currentfield.ownerId].playerId == p.playerId) { // own property
std::cout<< "Lucky, you landed on your own tile 🍀" <<std::endl;
}else{ // pay rent
int rentPayment;
if (currentfield.tileIndex == 12 || currentfield.tileIndex == 28) { // Utility
rentPayment = calculateUtilityRent(currentfield, s);
} else if (currentfield.tileIndex == 5 || currentfield.tileIndex == 15 || currentfield.tileIndex == 25 || currentfield.tileIndex == 35) { // Railroad
rentPayment = calculateRailroadRent(currentfield);
} else { // Property
rentPayment = calculatePropertyRent(currentfield);
}
// transfer the money and check for bankruptcy
transferMoney(p, currentfield.ownerId, rentPayment);
std::cout<<"You had to pay " << rentPayment << " to " << players[currentfield.ownerId].name << " 💵" <<std::endl;
}
break;
}
}
}
// Main function for the GameLoop when the player is in Jail, with a selection for the possible actions in Monopoly
bool jailedaction(int &sel, player &p, int &diceRolls, bool &ok){
if (p.jailFreeCard > 0) {
std::cout<<colorCodes[p.color].first + p.symbol + " " + p.name + RESET_COLOR + ", it's your turn!\n"<<"You have " <<p.money<<"$ in your account.\n"
<<"What do you want to do? \n"
<<"┌────────────────────┬───────────────────────┬─────────────────────────────────┬───────────────────┬────────────────────────────────┐\n"
<<"│ 1 = Roll the dices │ 2 = Buy you out (50$) │ 3 = Play a get out of jail card │ 0 = End your turn │ 77 = Quit the whole game early │\n"
<<"└────────────────────┴───────────────────────┴─────────────────────────────────┴───────────────────┴────────────────────────────────┘"
<<std::endl;
} else {
std::cout<<colorCodes[p.color].first + p.symbol + " " + p.name + RESET_COLOR + ", it's your turn!\n"<<"You have " <<p.money<<"$ in your account.\n"
<<"What do you want to do? \n"
<<"┌────────────────────┬───────────────────────┬───────────────────┬────────────────────────────────┐\n"
<<"│ 1 = Roll the dices │ 2 = Buy you out (50$) │ 0 = End your turn │ 77 = Quit the whole game early │\n"
<<"└────────────────────┴───────────────────────┴───────────────────┴────────────────────────────────┘"
<<std::endl;
}
std::cin>>sel;
switch (sel) {
case 0:{
displayGameBoard();
if(!ok){
std::cout<<"You haven't rolled enough dice! 😡"<<std::endl;
}
return ok;
}
case 1:{
if(!ok){
int x = rollDice();
int y = rollDice();
ok = true;
if(p.jailCounter < 3){
p.jailCounter++;
if(checkPasch(x, y)){
p.jailed = false;
p.jailCounter = 0;
diceRolls++;
movePlayer(x+y,p,ok,visualDice(x)+"+ "+visualDice(y));
}else{
displayGameBoard();
visualDice(x);
visualDice(y);
std::cout<<"No doubles no FREEDOM! 🦅"<<std::endl;
}
}else{
p.jailed = false;
p.jailCounter = 0;
transferMoney(p,-1, 50);
diceRolls++;
movePlayer(x+y, p,ok,visualDice(x)+"+ "+visualDice(y));
std::cout<< "FREEDOM is not FREE! 🦅" <<std::endl;
}
}else{
displayGameBoard();
std::cout<<"You have already rolled enough dice! 😡"<<std::endl;
}
return false;
}
case 2:{
int x = rollDice();
int y = rollDice();
ok = true;
p.jailed = false;
p.jailCounter = 0;
transferMoney(p, -1, 50);
diceRolls++;
movePlayer(x+y,p,ok,visualDice(x)+"+ "+visualDice(y));
std::cout<< "FREEDOM is not FREE! 🦅" <<std::endl;
return false;
}
case 3:{
if (p.jailFreeCard <= 0) {
sel = -1;
displayGameBoard();
std::cout<<"No valid input! 😡"<<std::endl;
clearInputBuffer();
return false;
}
p.jailFreeCard--;
int x = rollDice();
int y = rollDice();
ok = true;
p.jailed = false;
p.jailCounter = 0;
diceRolls++;
movePlayer(x+y,p,ok,visualDice(x)+"+ "+visualDice(y));
std::cout<< "FREEDOM is FREE? 😫" <<std::endl;
return false;
}
case 77:{
return true;
}
default:{
sel = -1;
displayGameBoard();
std::cout<<"No valid input! 😡"<<std::endl;
clearInputBuffer();
return false;
}
}
}
//the whole function for the menue point financial menue with alle steps
bool financial_menue(player &p){
int sel;
displayGameBoard();
std::vector<tile> filteredTileListPlayer;
std::cout<<colorCodes[p.color].first << p.symbol << " " << p.name << RESET_COLOR << ", welcome to the financial menue\n"<<"You have " <<p.money<<"$ in your account.\n"
<<"What do you want to do? \n"
<<"┌─────────────────────────┬───────────────────────────┬─────────────┐\n"
<<"│ 1 = mortgage your cards │ 2 = unmortgage your cards │ 0 = go back │\n"
<<"└─────────────────────────┴───────────────────────────┴─────────────┘"
<<std::endl;
std::cin>>sel;
switch (sel) {
case 1:{
//checks if player owns tiles and searches for all unmortaged cards that can be mortaged
if(!p.ownedStreets.empty()){
for(int i : p.ownedStreets){
if(!gameBoard[i].isMortgaged && gameBoard[i].upgradeStage == 0){
filteredTileListPlayer.push_back(gameBoard[i]);
}
}
//loop for selecting cards that should be mortaged
do{
displayGameBoard();
if(!filteredTileListPlayer.empty()){
std::cout<<colorCodes[p.color].first << p.symbol << " " << p.name << RESET_COLOR << "here are the cards you can mortgage:" <<std::endl;
int i = 0;
for(tile t : filteredTileListPlayer){
std::cout<<colorCodes[t.color].first << std::string(i < 10 ? "0" : "") << i << " | " << "$" << t.buyPrice*0.5 << std::string(t.buyPrice*0.5 < 100 ? " " : "") << " | " << t.tileName << RESET_COLOR <<std::endl;
i++;
}
std::cout<< "99 | Finished in this menue" <<std::endl;
std::cout<<"Wich do you choose?"<<std::endl;
std::cin>>sel;
if(sel !=99 && sel < filteredTileListPlayer.size() && std::cin.good()){
p.money += (0.5*filteredTileListPlayer[sel].buyPrice);
gameBoard[filteredTileListPlayer[sel].tileIndex].isMortgaged = true;
filteredTileListPlayer.erase(filteredTileListPlayer.begin() + sel);
}
}else{
std::cout<<"All your cards are mortgaged! 🏚"<<std::endl;
break;
}
}while(sel != 99);
if(sel ==99){
displayGameBoard();
}
}else{
displayGameBoard();
std::cout<<"You are homeless! 🏚"<<std::endl;
}
break;
}
case 2:{
//checks if player owns tiles and searches for all mortaged cards
if(p.ownedStreets.size()){
for(int i : p.ownedStreets){
if(gameBoard[i].isMortgaged){
filteredTileListPlayer.push_back(gameBoard[i]);
}
}
//loop for selecting cards that should be unmortaged
do{
displayGameBoard();
if(!filteredTileListPlayer.empty()){
std::cout<<colorCodes[p.color].first << p.symbol << " " << p.name << RESET_COLOR << "here are the cards you can unmortgage:" <<std::endl;
int i = 0;
for(tile t : filteredTileListPlayer){
std::cout<<colorCodes[t.color].first << std::string(i < 10 ? "0" : "") << i << " | " << "$" << t.buyPrice*0.55 << std::string(t.buyPrice*0.55 < 100 ? " " : "") << " | " << t.tileName << RESET_COLOR <<std::endl;
i++;
}
std::cout<< "99 | Finished in this menue" <<std::endl;
std::cout<<"Wich do you choose?"<<std::endl;
std::cin>>sel;
if(sel !=99 && sel < filteredTileListPlayer.size() && std::cin.good()){
if (p.money < (0.55*filteredTileListPlayer[sel].buyPrice)){
displayGameBoard();
std::cout<<"Not enough Money. 😢"<<std::endl;
continue;
}
transferMoney(p, -1, (0.55*filteredTileListPlayer[sel].buyPrice));
gameBoard[filteredTileListPlayer[sel].tileIndex].isMortgaged = false;
filteredTileListPlayer.erase(filteredTileListPlayer.begin() + sel);
}
}else{
std::cout<<"All your cards are unmortgaged! 🏠"<<std::endl;
break;
}
}while(sel != 99);
if(sel ==99){
displayGameBoard();
}
}else{
displayGameBoard();
std::cout<<"You are homeless! 🏚"<<std::endl;
}
break;
}
case 0:{
displayGameBoard();
std::cout<<"i want to be MONKEY! 🐒"<<std::endl;
break;
}
default:{
displayGameBoard();
std::cout<<"No valid input! 😡"<<std::endl;
break;
}
}
return false;
}
//the whole function for the menue point building menue with alle steps
bool building_menue(player &p){
int sel;
displayGameBoard();
std::vector<int> filteredTileListPlayer;
std::cout<<colorCodes[p.color].first << p.symbol << " " << p.name << RESET_COLOR << ", welcome to the building menue\n"<<"You have " <<p.money<<"$ in your account.\n"
<<"What do you want to do? \n"
<<"┌────────────────┬─────────────────┬─────────────┐\n"
<<"│ 1 = buy houses │ 2 = sell houses │ 0 = go back │\n"
<<"└────────────────┴─────────────────┴─────────────┘"
<<std::endl;
std::cin>>sel;
switch (sel) {
case 1:{
//checks if the player ownes streets and checks if he has a monopoly and checks the upgrading status, then puts these tiles in a filtered list
if(!p.ownedStreets.empty()){
for(int i : p.ownedStreets){
if(ownsMonopoly(gameBoard[i])){
if(gameBoard[i].upgradeStage != 5 && !gameBoard[i].isMortgaged && gameBoard[i].housePrice != 0){
filteredTileListPlayer.push_back(gameBoard[i].tileIndex);
}
}
}
//loop for the upgrading menue
if(!filteredTileListPlayer.empty()){
std::sort(filteredTileListPlayer.begin(),filteredTileListPlayer.end());
do{
displayGameBoard();
if(!filteredTileListPlayer.empty()){
std::cout<<colorCodes[p.color].first << p.symbol << " " << p.name << RESET_COLOR << "here are the cards you can upgrade:" <<std::endl;
int i = 0;
for(int t : filteredTileListPlayer){
std::cout<<colorCodes[gameBoard[t].color].first << std::string(i < 10 ? "0" : "") << i << " | " << "$" << gameBoard[t].housePrice << std::string(gameBoard[t].housePrice < 100 ? " " : "") << " | " << gameBoard[t].tileName << RESET_COLOR <<std::endl;
i++;
}
std::cout<< "99 | Finished in this menue" <<std::endl;
std::cout<<"Wich do you choose?"<<std::endl;
std::cin>>sel;
if(sel !=99 && sel < filteredTileListPlayer.size() && std::cin.good()){
if (p.money < gameBoard[filteredTileListPlayer[sel]].housePrice){
displayGameBoard();
std::cout<<"Not enough Money. 😢"<<std::endl;
continue;
}
transferMoney(p, -1, gameBoard[filteredTileListPlayer[sel]].housePrice);
gameBoard[filteredTileListPlayer[sel]].upgradeStage++;
if(gameBoard[filteredTileListPlayer[sel]].upgradeStage == 5){
filteredTileListPlayer.erase(filteredTileListPlayer.begin() + sel);
}
}
}else{
std::cout<<"All your cards max upgraded! 🏠"<<std::endl;
break;
}
}while(sel != 99);
if(sel ==99){
displayGameBoard();
}
}else{
displayGameBoard();
std::cout<<"No monopolys! 🏚"<<std::endl;
}
}else{
displayGameBoard();
std::cout<<"You are homeless! 🏚"<<std::endl;
}
break;
}case 2:{
//checks if the player ownes streets and checks if he has a monopoly and checks the upgrading status, then puts these tiles in a filtered list
if(!p.ownedStreets.empty()){
for(int i : p.ownedStreets){
if(ownsMonopoly(gameBoard[i])){
if(gameBoard[i].upgradeStage != 0 && !gameBoard[i].isMortgaged && gameBoard[i].housePrice != 0){
filteredTileListPlayer.push_back(gameBoard[i].tileIndex);
}
}
}
//loop tor the downgrading menue
if(!filteredTileListPlayer.empty()){
std::sort(filteredTileListPlayer.begin(),filteredTileListPlayer.end());
do{
displayGameBoard();
if(!filteredTileListPlayer.empty()){
std::cout<<colorCodes[p.color].first << p.symbol << " " << p.name << RESET_COLOR << "here are the cards you can downgrade:" <<std::endl;
int i = 0;
for(int t : filteredTileListPlayer){
std::cout<<colorCodes[gameBoard[t].color].first << std::string(i < 10 ? "0" : "") << i << " | " << "$" << gameBoard[t].housePrice*0.5 << std::string(gameBoard[t].housePrice*0.5 < 100 ? " " : "") << " | " << gameBoard[t].tileName << RESET_COLOR <<std::endl;
i++;
}
std::cout<< "99 | Finished in this menue" <<std::endl;
std::cout<<"Wich do you choose?"<<std::endl;
std::cin>>sel;
if(sel !=99 && sel < filteredTileListPlayer.size() && std::cin.good()){
p.money += (0.5*gameBoard[filteredTileListPlayer[sel]].housePrice);
gameBoard[filteredTileListPlayer[sel]].upgradeStage--;
if(gameBoard[filteredTileListPlayer[sel]].upgradeStage == 0){
filteredTileListPlayer.erase(filteredTileListPlayer.begin() + sel);
}
}
}else{
std::cout<<"All your cards are max downgraded! 🏚"<<std::endl;
break;
}
}while(sel != 99);
if(sel ==99){
displayGameBoard();
}
}else{
displayGameBoard();
std::cout<<"No monopolys! 🏚"<<std::endl;
}
}else{
displayGameBoard();
std::cout<<"You are homeless! 🏚"<<std::endl;
}
break;
}case 0:{
displayGameBoard();
std::cout<<"i want to be MONKEY! 🐒"<<std::endl;
break;
}
default:{
displayGameBoard();
std::cout<<"No valid input! 😡"<<std::endl;
break;
}
}
return false;
}
//the whole function for the menue point traiding menue with alle steps
bool trading_menue(player &p){
int sel;