-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpace.cpp
191 lines (161 loc) · 5.62 KB
/
Space.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
//
// Created by mfbut on 1/20/2018.
//
#include <iostream>
#include "Space.h"
#include "MonopolyUtility.h"
#include "Player.h"
#include "GameState.h"
unsigned long Monopoly::Space::length_of_longest_space_name;
Monopoly::Space::Space(CSVReader& boardFile, const int spaceNumber, GameState& gameState)
: spaceNumber(spaceNumber), gameState(gameState) {
std::string space_type_string = boardFile.get_next_field();
if (space_type_string == "Go") {
spaceType = SpaceType::goSpace;
goPtr = std::make_unique<Go>(boardFile);
// goPtr = new Go(boardFile);
boardFile.skip_line(); // skip the remainder of the line in the file
} else
if (space_type_string == "Property") {
spaceType = SpaceType::propertySpace;
propertyPtr = std::make_unique<Property>(boardFile, gameState.rules);
} else {
std::cout << "Unrecognized Space type found in " << boardFile.getFileName() << std::endl;
}
}
virtual void Monopoly::Space::display() const {
const auto frmt_flags = std::cout.flags();
//display space name
std::cout.width(length_of_longest_space_name);
std::cout << getName() << " | ";
//display owner
std::cout.width(Monopoly::Player::length_of_longest_player_name);
if (spaceType == SpaceType::goSpace) {
const std::string temp = "None";
std::cout << temp << " | ";
std::cout << " | ";
} else if (spaceType == SpaceType::propertySpace) {
std::cout << propertyPtr->getOwnerName() << " | ";
std::cout << propertyPtr->getUpgradesString() << " | ";
} else {
std::cout << " This space has an unknown type" << std::endl;
}
std::cout.flags(frmt_flags);
auto player = playersOnSpace.cbegin();
for (; player != playersOnSpace.cend(); ++player) {
if (player + 1 == playersOnSpace.cend()) {
break;
}
(*player)->display();
std::cout << ", ";
}
if (player != playersOnSpace.cend()) {
(*player)->display();
}
}
const std::string& Monopoly::Space::getName() const {
if (spaceType == SpaceType::goSpace) {
return goPtr->getName();
} else {
return propertyPtr->getName();
}
}
void Monopoly::Space::addPlayer(Monopoly::Player& player) {
auto playerIt = playersOnSpace.cbegin();
for (; playerIt != playersOnSpace.cend(); ++playerIt) {
if (player.getId() < (*playerIt)->getId()) {
break;
}
}
playersOnSpace.insert(playerIt, &player);
}
void Monopoly::Space::removePlayer(const Monopoly::Player& player) {
for (auto playerIt = playersOnSpace.cbegin(); playerIt != playersOnSpace.cend(); ++playerIt) {
if ((*playerIt)->getId() == player.getId()) {
playersOnSpace.erase(playerIt);
return;
}
}
}
void Monopoly::Space::removeAllPlayers() {
playersOnSpace.clear();
}
Monopoly::SpaceType Monopoly::Space::getSpaceType() const {
return spaceType;
}
int Monopoly::Space::getSpaceNumber() const {
return spaceNumber;
}
void Monopoly::Space::activate(Monopoly::Player& activatingPlayer) {
if (spaceType == SpaceType::goSpace) {
// -1 below because they've already been given some of the salary for
// "passing" go
activatingPlayer.giveCash(goPtr->getSalary() * (gameState.rules.getSalary_multiplier_for_landing_on_go() - 1));
} else if (spaceType == SpaceType::propertySpace) {
Player* owner = propertyPtr->getOwner();
if (owner == nullptr) {
if (activatingPlayer.getCash() >= propertyPtr->getCost()) { //can afford the property
bool toBuy = activatingPlayer.getBuyDecision(*this);
if (toBuy) {
std::cout << activatingPlayer.getName() << " bought " << getName() << " for $" << propertyPtr->getCost()
<< std::endl;
activatingPlayer.purchase(*propertyPtr, propertyPtr->getCost());
} else if(gameState.rules.isAuction_properties()){
gameState.auction(*propertyPtr);
}
} else { // not enough money to buy property
std::cout << activatingPlayer.getName() << ", you don't have enough money to afford " << getName() << ". "
<< getName() << " costs $" << getCost() << " but you only have $" << activatingPlayer.getCash()
<< std::endl;
if(gameState.rules.isAuction_properties()){
gameState.auction(*propertyPtr);
}
}
} else if (owner->getId() == activatingPlayer.getId()) {// player landed on their own property
std::cout << activatingPlayer.getName() << ", you landed on " << getName() << " which you already own" <<
std::endl;
} else { //landed on a space another player owes
activatingPlayer.payPlayer(*owner, *propertyPtr, gameState.rules);
}
}
}
virtual int Monopoly::Space::getSalary() const {
if (spaceType == SpaceType::goSpace) {
return goPtr->getSalary();
}
else if(spaceType == SpaceType::FreeParking){
return freeParking
}else {
std::cout << "Get Salary called on non Go Space" << std::endl;
return -99;
}
}
int Monopoly::Space::getCost() const {
if (spaceType == SpaceType::propertySpace) {
return propertyPtr->getCost();
} else {
std::cout << "Get Cost called on non Property Space" << std::endl;
return -99;
}
}
int Monopoly::Space::getSetId() const {
if (spaceType == SpaceType::propertySpace) {
return propertyPtr->getSetId();
} else {
return -99;
}
}
int Monopoly::Space::getIntraSetId() const {
if (spaceType == SpaceType::propertySpace) {
return propertyPtr->getIntraSetId();
} else {
return -99;
}
}
int Monopoly::Space::getBasicRent() const {
if (spaceType == SpaceType::propertySpace) {
return propertyPtr->getRent();
} else {
return -99;
}
}