-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathownable.cc
464 lines (423 loc) · 14.4 KB
/
ownable.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
//
// Created by aiden on 03/04/2023.
//
#include "ownable.h"
#include <string>
#include <iostream>
#include "player.h"
#include "cell.h"
using namespace std;
Ownable::Ownable(Board &board, std::string name, int posn, int i, int j, bool ownable, OwnableType otype, int price) :
Cell(board, name, posn, i, j, ownable, otype, price) {}
string Ownable::getFacultyName(const std::string& buildingName) {
for (const auto& building : academic_buildings) {
if (buildingName == building.first) {
return std::get<0>(building.second);
}
}
return "Building not found.";
}
void Ownable::buy(Player *p, int newPrice) {
int tempPrice = newPrice == -1 ? price : newPrice;
if (p->getMoney() < tempPrice) {
cout << "Not enough funds" << endl;
return;
}
p->subFunds(tempPrice);
setOwnedBy(p);
p->addProperty(this);
cout << "Bought " << getName() << " for " << tempPrice << endl;
}
bool Ownable::singleBool(vector<bool> b) {
int num = 0;
for (int i = 0; i < b.size(); i++) {
if (b[i]) {
num++;
}
}
if (num > 1) {
return false;
}
return true;
}
void Ownable::auction() {
cout << "Auctioning " << getName() << endl;
int highestBid = 0;
vector<bool> playerParticipation;
for (auto &p : board.getPlayerList()) {
playerParticipation.emplace_back(true);
}
Player *highestBidder = nullptr;
int i = 0;
while (!singleBool(playerParticipation)) {
for (auto &player : board.getPlayerList()) {
if (!playerParticipation[i]) {
i++;
continue;
}
string response;
cout << "Player: " << player->getName() << ", wanna drop out? type 'y' if u do, else type something else" << endl;
cin >> response;
if (response == "y") {
playerParticipation[i] = false;
i++;
continue;
}
if (player->getMoney() > highestBid) {
int bid;
cout << "How much would you like to bid, " << player->getName() << "?" << endl;
while (cin >> bid) {
if (bid > highestBid) highestBidder = player.get();
break;
}
}
i++;
}
}
// int i = 0;
// P
if (highestBidder) {
buy(highestBidder);
cout << "Auction won by " << highestBidder->getName() << " for " << highestBid << endl;
}
}
int Ownable::improvementCost() {
if (otype != OwnableType::Academic) {
return 0;
} else {
return std::get<2>(academic_buildings.find(getName())->second);
}
}
void Ownable::sellImprovement() {
if (otype != OwnableType::Academic) {
return;
}
getImproveCount();
Player *owner = getOwnedBy();
string cName = getName();
if (owner == nullptr) {
cout << "Not owned: No Improvements to sell" << endl;
return;
}
if (getImproveCount() < 0) {
cout << "No Improvements to sell" << endl;
return;
}
//tuple w costs
auto building = academic_buildings.find(cName)->second;
//get cost of curr improvement and subtract
owner->addFunds((std::get<2>(building)) * 0.5);
cout << "Sold Improvement: " << getImproveCount() << endl;
if (getImproveCount() < 4) {
cout << "Sold a Bathroom" << endl;
} else {
cout << "Sold a Cafeteria" << endl;
}
cout << "Received:$" << ((std::get<2>(building)) * 0.5) << endl;
setImproveCount(getImproveCount() - 1);
}
void Ownable::buyImprovement() {
if (otype != OwnableType::Academic) {
return;
}
Player *owner = getOwnedBy();
string cName = getName();
if (isMortgage) {
cout << "mortgaged property: No improvements Allowed" << endl;
return;
}
string facName = getFacultyName(cName);
if (owner) {
owner->partMonopoly();
if (getOwnedBy()->FacultyMap[facName].second && getImproveCount() < 5) {
//owner has a monopoly and < 5 built so can buy improvement
//tuple w costs
auto building = academic_buildings.find(cName)->second;
//get cost of curr improvement and subtract
owner->subFunds(std::get<2>(building));
if (getImproveCount() < 4) {
cout << "Installed a Bathroom" << endl;
} else {
cout << "Installed a Cafeteria" << endl;
}
setImproveCount( (getImproveCount() + 1) );
}
} else {
cout << "No Ownership:No Improvements allowed" << endl;
}
}
void Ownable::payTuition(Player *p) {
if (otype == OwnableType::Academic) {
Player *owner = getOwnedBy();
string cName = getName();
string facName = getFacultyName(cName);
if (owner) {
owner->partMonopoly();
if (getOwnedBy()->FacultyMap[facName].second && getImproveCount() == 0) {
//owner has a monopoly and < 5 built so can buy improvement
//tuple w costs
auto building = academic_buildings.find(cName)->second;
//double tuition at 0
owner->addFunds((std::get<3>(building)) * 2);
p->subFunds((std::get<3>(building)) * 2);
if (p->getMoney() < 0) {
// TODO dont bankrupt, option ot raise!
p->bankrupt();
}
} else {
auto building = academic_buildings.find(cName)->second;
//reg tuition
int num = getImproveCount();
int num1;
switch (num) {
case 0:
num1 = std::get<3>(building);
break;
case 1:
num1 = std::get<4>(building);
break;
case 2:
num1 = std::get<5>(building);
break;
case 3:
num1 = std::get<6>(building);
break;
case 4:
num1 = std::get<7>(building);
break;
case 5:
num1 = std::get<8>(building);
break;
}
owner->addFunds(num1);
p->subFunds(num1);
//make call to Bankrupt
//Hassan pls
if (p->getMoney() < 0) {
p->bankrupt();
}
}
} else {
cout << "Not owned: No Tuition needed" << endl;
return;
}
} else if (otype == OwnableType::Residence) {
Player *owner = getOwnedBy();
int numResOwned = owner->getNumResidences();
if (numResOwned <= 0) {
cout << "Not owned: No Rent" << endl;
return;
}
if (numResOwned == 1) {
cout << "Rent to be paid:$ " << oneResRent << endl;
owner->addFunds(oneResRent);
p->subFunds(oneResRent);
if (p->getMoney() < 0) {
p->bankrupt();
}
} else if (numResOwned == 2) {
cout << "Rent to be paid:$ " << twoResRent << endl;
owner->addFunds(twoResRent);
p->subFunds(twoResRent);
if (p->getMoney() < 0) {
p->bankrupt();
}
} else if (numResOwned == 3) {
cout << "Rent to be paid:$ " << threeResRent << endl;
owner->addFunds(threeResRent);
p->subFunds(threeResRent);
if (p->getMoney() < 0) {
p->bankrupt();
}
} else if (numResOwned == 4) {
cout << "Rent to be paid:$ " << fourResRent << endl;
owner->addFunds(fourResRent);
p->subFunds(fourResRent);
if (p->getMoney() < 0) {
p->bankrupt();
}
}
} else if (otype == OwnableType::Gym) {
int numGymsOwned = getOwnedBy()->getNumGyms();
if (numGymsOwned <= 0) {
cout << "Not owned: No Membership" << endl;
return;
}
if (numGymsOwned == 1) {
int die1 = board.rollDice()[0];
int die2 = board.rollDice()[1];
int sumDie = die1 + die2;
int membership = oneGym * sumDie;
cout << "Membership to be paid:$ " << membership << endl;
getOwnedBy()->addFunds(membership);
p->subFunds(membership);
if (p->getMoney() < 0) {
p->bankrupt();
}
} else if (numGymsOwned == 2) {
int die1 = board.rollDice()[0];
int die2 = board.rollDice()[1];
int sumDie = die1 + die2;
int membershipTwo = twoGym * sumDie;
cout << "Membership to be paid:$ " << membershipTwo << endl;
getOwnedBy()->addFunds(membershipTwo);
p->subFunds(membershipTwo);
if (p->getMoney() < 0) {
p->bankrupt();
}
}
} else {
return;
}
}
void Ownable::mortgage() {
if (otype == OwnableType::Academic) {
string cellName = getName();
Player *owner = getOwnedBy();
if (getMortgaged()) {
cout << "mortgaged property already." << endl;
return;
}
if (owner != nullptr) {
//sell off all improvements if existing
if (getImproveCount() > 0) {
for (int i = 0; i < getImproveCount(); i++) {
this->sellImprovement();
}
}
//give owner half of cost
//tuple w costs
auto building = academic_buildings.find(cellName)->second;
owner->addFunds((std::get<1>(building)) * 0.5);
setMortgaged(true);
setSuccesful(true);
cout << "Successfully mortgaged" << endl;
} else {
cout << "Not Owned: unsuccessfully mortgaged" << endl;
setMortgaged(false);
setSuccesful(false);
}
} else if (otype == OwnableType::Residence) {
string cellName = getName();
if (getMortgaged()) {
cout << "mortgaged property already." << endl;
return;
}
if (getOwnedBy() != nullptr) {
//give owner half of cost
int mortgageMoney = resCost * 0.5;
getOwnedBy()->addFunds(mortgageMoney);
setMortgaged(true);
setSuccesful(true);
cout << "Successfully mortgaged " << cellName << endl;
} else {
cout << "Not Owned: unsuccessfully mortgaged " << cellName << endl;
setMortgaged(false);
setSuccesful(false);
}
} else if (otype == OwnableType::Gym) {
if (getMortgaged()) {
cout << "mortgaged property already." << endl;
return;
}
if (getOwnedBy()) {
//give owner half of cost
int mortgageMoney = gym_cost * 0.5;
getOwnedBy()->addFunds(mortgageMoney);
setMortgaged(true);
setSuccesful(true);
cout << "Successfully mortgaged" << endl;
} else {
cout << "Not Owned: unsuccessfully mortgaged" << endl;
setMortgaged(false);
setSuccesful(false);
}
} else {
return;
}
}
void Ownable::unMortgage() {
if (otype == OwnableType::Academic) {
string cellName = getName();
Player *owner = getOwnedBy();
if (!getMortgaged()) {
cout << "property not mortgaged." << endl;
setSuccesful(false);
return;
}
if (owner) {
if (getMortgaged()) {
//give owner half of cost
//tuple w costs
auto building = academic_buildings.find(cellName)->second;
int moneyOwed = ((std::get<1>(building)) * 0.6);
cout << "Pay to unmortgage:$ " << moneyOwed << endl;
if (owner->getMoney() > moneyOwed) {
owner->subFunds(moneyOwed);
setMortgaged(false);
setSuccesful(false);
cout << "Successfully unMortgaged" << endl;
} else {
cout << "Not Enough Money to unMortgage" << endl;
}
}
} else {
cout << "Not Owned: Cannot Unmortgage" << endl;
}
} else if (otype == OwnableType::Residence || otype == OwnableType::Gym) {
if (!getMortgaged()) {
cout << "property not mortgaged." << endl;
return;
}
if (getOwnedBy()) {
if (getMortgaged()) {
// owner pay 60% of cost
int moneyOwed = (gym_cost * 0.6);
cout << "Pay to unmortgage:$ " << moneyOwed << endl;
if (getOwnedBy()->getMoney() > moneyOwed) {
getOwnedBy()->subFunds(moneyOwed);
setMortgaged(false);
setSuccesful(false);
cout << "Successfully unMortgaged" << endl;
} else {
cout << "Not Enough Money to unMortgage" << endl;
}
}
} else {
cout << "Not Owned: Cannot Unmortgage" << endl;
}
} else {
return;
}
}
void Ownable::event(Player *p) {
cout << "recieved notification. maybe intended for me idk. cellName: " << getName() << endl;
cout << "welcome to: " << getName() << endl;
if (getOwnedBy() != nullptr) {
cout << "owned by: " << getOwnedBy() << endl;
payTuition(p);
} else if (getOwnedBy() == nullptr && getPrice() > p->getMoney()) {
cout << "not owned by anyone, but you are broke" << endl;
auction();
} else if (getOwnedBy() == nullptr){
cout << "Do you want to buy this or not? (y/n)" << endl;
string input;
cin >> input;
while (true) {
if (input == "y") {
buy(p);
break;
} else if (input == "n") {
auction();
break;
} else {
cout << "Plase enter either 'y' or 'n'" << endl;
}
}
} else {
cout << "you own this property" << endl;
}
}
void Ownable::setOwner(Player *p) {
setOwnedBy(p);
}