-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
524 lines (471 loc) · 14.1 KB
/
main.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
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include "board.h"
#include "block.h"
#include "player.h"
#include "tile.h"
#include "building.h"
#include "acBuilding.h"
#include "gym.h"
#include "residence.h"
#include <map>
#include <cstdlib>
using namespace std;
bool runMain = true;
bool isTesting = false;
void roll(Board *b, Player *p) {
int newPos, oldPos;
if (isTesting == true) {
int x, y, sum;
cin >> x;
cin >> y;
sum = x + y;
oldPos = p->getPos();
p->move(sum);
newPos = p->getPos();
b->movePlayer(newPos, oldPos);
}
else if (isTesting == false) {
oldPos = p->getPos();
p->roll();
newPos = p->getPos();
b->movePlayer(newPos, oldPos);
}
cout << "You landed on " << b->getTile(newPos)->getName() << "." << endl;
if (newPos == 0) {
// COllECT OSAP
cout << "Congratulations! Collect $200!" << endl;
// player already gets 200 from roll function
}
else if (newPos == 2 || newPos == 33) {
// SLC
p->SLC(b);
}
else if (newPos == 4) {
// TUITON
cout << "You have to pay tuition! Options: " << endl
<< " 1) $300 or" << endl
<< " 2) 10% of your total worth?" << endl;
string option;
cin >> option;
istringstream ss(option);
int n;
ss >> n;
if (!p->payTution(n)) b->removePlayer(p, NULL);
}
else if (newPos == 10) {
// DC Tims line
cout << "Nothing happens!" << endl;
}
else if (newPos == 20) {
// Goose Nesting
cout << "GEESE ATTACK YOU!!" << endl;
}
else if (newPos == 7 || newPos == 22 || newPos == 36) {
// Needles hall
p->NeedlesHall(b);
}
else if (newPos == 30) {
// GO TO TIMS
cout << "You must go to the DC Tims Line. Wait 3 turns to get out." << endl;
p->goToTims();
}
else if (newPos == 38) {
// COOP FEE
cout << "You must pay $150" << endl;
if (!p->pay(150)) b->removePlayer(p, NULL);
}
else {
// otherwise, player has landed on property (academic building, residence, or gym)
// if no one owns this property
// player can buy, or do nothing
// if someone owns this property already
// player must pay rent
Building *building = (Building *)b->getTile(newPos);
if (building->getOwner() == NULL) {
// no owner
cout << "Would you like to purchase " << building->getName() << " for $" << building->getPurchaseCost() << "? (y/n) ";
string answer;
cin >> answer;
if (answer.at(0) == 'y') {
p->purchase(building);
}
}
else if (building->getOwner()->getPiece() == p->getPiece()) {
// current player owns this building already
cout << "You own " << building->getName() << " already!" << endl;
}
else {
// someone else owns it, player must pay fee
Player *owner = building->getOwner();
double fee = building->getFee();
cout << "You must pay " << fee << " to Player " << owner->getPiece() << endl;
if (p->pay(fee)) {
owner->collect(fee);
}
else {
b->removePlayer(p, owner);
}
}
}
}
void mortgage(Board *b, Player *p, string command) {
string propertyName;
cin >> propertyName;
Tile *t = b->getTile(propertyName);
int i = 0;
if (t) i = t->getIndex();
// not proud of this
// if tile is not BUILDING
if (i == 0 || i == 2 || i == 4 || i == 7 || i == 10 ||
i == 17 || i == 20 || i == 22 ||
i == 30 || i == 33 || i == 36 || i == 38) {
cout << "You cannot mortgage " << propertyName << "." << endl;
}
else {
Building *building = (Building *)b->getTile(propertyName);
if (command == "mortgage") p->mortgage(building);
if (command == "unmortgage") p->unmortgage(building);
}
}
void improve(Board *b, Player *p, string command) {
string propertyName, buyorsell;
cin >> propertyName;
cin >> buyorsell;
Tile *t = b->getTile(propertyName);
int i = 0;
if (t) i = t->getIndex();
// not proud of this
// if tile is not ACADEMICBUILDING
if (i == 0 || i == 2 || i == 4 || i == 5 || i == 7 || i == 10 || i == 12 ||
i == 15 || i == 17 || i == 20 || i == 22 || i == 25 || i == 28 ||
i == 30 || i == 33 || i == 35 || i == 36 || i == 38) {
cout << "You cannot buy/sell any improvements on " << propertyName << "." << endl;
}
// if the tile is an AcademicBuilding
else {
AcademicBuilding* ab = (AcademicBuilding*)b->getTile(propertyName);
if (buyorsell == "buy") p->improve(ab, true);
if (buyorsell == "sell") p->improve(ab, false);
}
}
void trade(Board *b, Player *p) {
// trade
// trade <name> <give> <receive>
string partner, give, receive;
cin >> partner;
cin >> give;
cin >> receive;
if (!b->playerExists(partner)) {
cout << partner << " is not a valid player." << endl;
}
else {
int nGive, nReceive;
istringstream ssGive(give);
if (ssGive >> nGive) {
// receive is a property then
Tile *t = b->getTile(receive);
int i = 0;
if (t) i = t->getIndex();
// not proud of this
// if tile is not BUILDING
if (i == 0 || i == 2 || i == 4 || i == 7 || i == 10 ||
i == 17 || i == 20 || i == 22 ||
i == 30 || i == 33 || i == 36 || i == 38) {
cout << "You cannot trade $" << nGive << " for " << receive << "." << endl;
}
else {
Building *building = (Building *)b->getTile(receive);
if (building->getOwner()->getName() != partner) {
cout << partner << " does not own " << receive << endl;
}
cout << partner << ", do you accept the trade? (y/n) ";
string response;
cin >> response;
if (response.at(0) == 'y') {
p->pay(nGive);
b->getPlayer(partner)->collect(nGive);
building->setOwner(p);
}
}
}
else {
// offer property for $$$
istringstream ssReceive(receive);
if (ssReceive >> nReceive) {
Tile *t = b->getTile(give);
int i = 0;
if (t) i = t->getIndex();
if (i == 0 || i == 2 || i == 4 || i == 7 || i == 10 ||
i == 17 || i == 20 || i == 22 ||
i == 30 || i == 33 || i == 36 || i == 38) {
cout << "You cannot trade " << give << " for $" << nReceive << "." << endl;
}
else {
Building *building = (Building *)b->getTile(give);
if (building->getOwner()->getName() != p->getName()) {
cout << "You do not own " << give << endl;
}
else {
cout << partner << ", do you accept the trade? (y/n) ";
string response;
cin >> response;
if (response.at(0) == 'y') {
p->collect(nReceive);
b->getPlayer(partner)->pay(nReceive);
building->setOwner(b->getPlayer(partner));
}
}
}
}
}
}
}
void help() {
cout << "Commands" << endl;
cout << " roll: rolls 2 dice and moves" << endl;
cout << " next: finish turn" << endl;
cout << " trade <name> <give> <receive>: trade with <name>, offering <give> in return for <receive>" << endl;
cout << " improve <property> buy/sell: buys/sells improvement on <property>" << endl;
cout << " mortgage <property>: mortgages a <property>" << endl;
cout << " unmortgage <property>: unmortgages a <property>" << endl;
cout << " assets: displays your assets" << endl;
cout << " save <filename>: saves current game under <filename>" << endl;
}
void assets(Board *b, Player *p) {
cout << "Assets: " << p->netWorth;
}
void save(Board *b, string filename) {
cout << "start save" << endl;
ofstream ofs;
const char *cstr = filename.c_str();
ofs.open (cstr);
ofs << b->numPlayers << endl;
for (int i = 0; i < b->numPlayers; i++) {
cout << "for1, i=" << i << endl;
ofs << b->players[i]->name << " ";
ofs << b->players[i]->money << " ";
ofs << b->players[i]->pos;
if (b->players[i]->pos == 10) {
if (b->players[i]->turnsInTims == 0) {
ofs << " 0" << endl;
}
else if (b->players[i]->turnsInTims > 0) {
ofs << " 1 " << b->players[i]->turnsInTims << endl;
}
}
else {
ofs << endl;
}
}
cout << "end for1" << endl;
for (int i = 0; i < 40; i++) {
cout << "for2, i=" << i << endl;
if (i==0 || i==2 || i==4 || i==7 || i==10 || i==17
|| i==20 || i==22 || i==30 || i==33 || i==36 || i==38) {
continue;
}
Building *bb = (Building *)b->getTile(i);
ofs << bb->getName() << " ";
if (bb->getOwner() == NULL) {
ofs << "BANK ";
}
else {
string owner = bb->getOwner()->getName();
ofs << owner << " ";
}
if (i==5 || i==12 || i==15 || i==25 || i==28 || i==35) {
ofs << 0 << endl;
continue;
}
else if (bb->isMortgaged() == true) {
ofs << -1 << endl;
}
else {
AcademicBuilding *ab = (AcademicBuilding *)bb;
ofs << ab->getNumImproves() << endl;
}
}
}
void takeTurn(Board *b, Player *p) {
if (p->turnsInTims > 0) {
if (!p->tryToLeaveTims(b)) return;
}
bool rolled = false;
// DRAW BOARD
b->reDraw();
cout << "----- " << p->getName() << "'s turn -----" << endl;
while (true) {
cout << "Options: help, ";
if (!rolled) cout << "roll, ";
cout << "next, trade, improve, mortgage, unmortgage, assets, save" << endl;
string filename;
string command;
cin >> command;
if (!cin) {
// READ ERROR
}
if (command == "help") {
help();
}
else if (command == "next") {
cout << endl << endl;
b->incrIterator();
return;
}
else if (!rolled && command == "roll") {
rolled = true;
roll(b, p);
}
else if (command == "improve") {
improve(b, p, command);
}
else if (command == "mortgage" || command == "unmortgage") {
mortgage(b, p, command);
}
else if (command == "trade") {
trade(b, p);
}
else if (command == "assets") {
assets(b, p);
}
else if (command == "save") {
cin >> filename;
save(b, filename);
}
else {
cout << "Not a valid command!" << endl;
}
}
}
int main(int argc, char* argv[]) {
if (argc > 1) {
string str(argv[1]);
//argv[0] is the program
//argv[1] is -load or -testing
//if argv[1] is -load, argv[2] is a filename
if (str == "-load") {
runMain = false;
char* file = argv[2];
ifstream f;
string name, owner;
int numPlayers, money, position, dc, dcnum, improvements;
Board board;
char pieces[8];
pieces[0] = 'G';
pieces[1] = 'B';
pieces[2] = 'D';
pieces[3] = 'P';
pieces[4] = 'S';
pieces[5] = '$';
pieces[6] = 'L';
pieces[7] = 'T';
f.open(file);
if (!f.is_open()) {
exit(EXIT_FAILURE);
}
f>>numPlayers;
Player *listofPlayers[6];
for (int i = 0; i < numPlayers; i++) {
//int piece = (rand() % 8);
f>>owner;
f>>money;
f>>position;
Player *p = board.addPlayer(owner, pieces[i], position);
p->money = money;
p->pos = position;
if (position == 10) {
f>>dc;
if (dc == 1) {
f>>dcnum;
p->turnsInTims = dcnum;
}
else dcnum = 0;
}
listofPlayers[i] = p;
} //end for
for (int i = 0; i < 40; i++) {
if (i==0 || i==2 || i==4 || i==7 || i==10 || i==17
|| i==20 || i==22 || i==30 || i==33 || i==36 || i==38) {
continue;
}
f>>name;
f>>owner;
Player *p1 = board.getPlayer(owner);
Building *bb = (Building *)board.getTile(i);
bb->setOwner(p1);
f>>improvements;
while (improvements > 0) {
AcademicBuilding *ab = (AcademicBuilding *)bb;
ab->improve();
board.addImprove(i);
improvements--;
}
}
while (true) {
// stop loop, TODO
// Players turn
Player *player = board.nextPlayer();
takeTurn(&board, player);
}
}
else if (str == "-testing") {
isTesting = true;
runMain = true;
}
}
if (runMain == true) {
cout << "Welcome to BB7K!" << endl;
if (isTesting == true) {
cout << "Testing mode enabled" << endl;
}
cout << "Enter number of players: (2-6) ";
int n = 0;
while (n < 2 || n > 6) {
string s;
cin >> s;
istringstream ss(s);
ss >> n;
}
Board board;
map<char, string> pieces;
pieces['G'] = "Goose";
pieces['B'] = "GRT Bus";
pieces['D'] = "Tims Donut";
pieces['P'] = "Professor";
pieces['S'] = "Student";
pieces['$'] = "Money";
pieces['L'] = "Laptop";
pieces['T'] = "Pink tie";
typedef map<char, string>::iterator it;
// Players are setup here
for (int i = 0; i < n; i++) {
cout << "Enter player name: " << endl;
string name;
cin >> name;
cout << "---Available pieces---" << endl;
for (it iterator = pieces.begin(); iterator != pieces.end(); iterator++) {
cout << " " << iterator->first << ": " << iterator->second << endl;
}
cout << name << ", choose a piece: " << endl;
char piece;
//if piece chosen is valid
if (cin >> piece && pieces.count(piece) > 0) {
//add player's piece to board
board.addPlayer(name, piece, 0);
//remove piece from list of pieces
pieces.erase(piece);
} else {
cout << "Unavailable, choose another piece" << endl;
i--;
}
}
while (true) { // stop loop, TODO
// Players turn
Player *player = board.nextPlayer();
takeTurn(&board, player);
}
}
}