-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
406 lines (367 loc) · 10.9 KB
/
Board.cpp
File metadata and controls
406 lines (367 loc) · 10.9 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
#include "Space.h"
#include "Player.h"
#include "Card.h"
#include "Bank.h"
#include "Board.h"
#include "moveaction.h"
#include "MoneyAction.h"
#include "GotoAction.h"
#include <QString>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <QInputDialog>
#include <QMessageBox>
using namespace std;
Board::Board()
{
srand(time(NULL));
numPlayers = setNumberOfPlayers(); //Gets the number of players for the game
playerToGo = 0;
setTokens();
shuffleCards();
fillBoard();
for (int i = 0; i < numPlayers; i++)
allPlayers[i].addMoney(banker.withdrawFunds(2500)); //Add 2500 to each player from the banker
}
bool Board::nextPerson()
{
if(numAlivePlayers() > 1)
{
shouldDie(playerToGo);
playerToGo = (playerToGo + 1) % numPlayers;
while(!allPlayers[playerToGo].isAlive())
playerToGo = (playerToGo + 1) % numPlayers;
QString info = QString("Player %1\'s Turn.").arg(playerToGo + 1); //Rotate through the players
QMessageBox::information(0, "Turn Information", info);
//printConsoleBoard();
//printStats();
// doAction(i);
return true;
}
else
{
QString info = QString("Player %1 wins!").arg(findWinner() + 1);
QMessageBox::information(0, "Turn Information", info);
return false;
}
}
Board::~Board()
{
delete[] allPlayers;
delete[] board;
}
void Board::printConsoleBoard() //Print out the 40-square board
{
int count = 0;
for (int i = 0; i < 44; i++)
{
for (int j = 0; j < 11; j++)
{
if (i / 4 == 0 || i / 4 == 10 || j == 0 || j == 10)
{
if (i % 4 == 0)
cout << "+--+";
else if (i % 4 == 1)
{
printSquare(BOARD_ORDER[count], 0);
count++;
}
else if (i % 4 == 2)
{
printSquare(BOARD_ORDER[count], 2);
count++;
}
else
cout << "+--+";
}
else
cout << " ";
}
cout << endl;
}
}
void Board::shouldDie(int i) //consider scenarios where a player is eliminated
{
if (allPlayers[i].isAlive())
{
if (allPlayers[i].getMoney() <= 0) //out of money
{
QString info = QString("Player %1 ran out of money.").arg(i + 1);
QMessageBox::information(0, "Turn Information", info);
allPlayers[i].die();
removeProperties(i);
}
}
}
void Board::shouldResign()
{
if (allPlayers[playerToGo].isAlive()) //by choice
{
int reply = QMessageBox::question(0, "Turn Question", "Want to resign the game?", QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes)
{
QString info = QString("Player %1 resigned.").arg(playerToGo + 1);
QMessageBox::information(0, "Turn Information", info);
allPlayers[playerToGo].die();
removeProperties(playerToGo);
}
}
}
void Board::selectBuyOrUpgrade()
{
doAction(playerToGo);
}
void Board::printStats()
{
/*
for (int i = 0; i < numPlayers; i++)
cout << "Player " << allPlayers[i].getToken() << ": " << allPlayers[i].getMoney() << endl;
*/
}
int Board::getNumPlayers()
{
return numPlayers;
}
int Board::getNextPlayer()
{
return playerToGo;
}
Player* Board::getPlayer(int i)
{
if(i >= numPlayers)
return NULL;
else
return &(allPlayers[i]);
}
Player* Board::getAllPlayers()
{
return allPlayers;
}
void Board::roll() //Gets the number of steps the current token needs to take
{
int die1 = rand() % 6 + 1;
int die2 = rand() % 6 + 1;
int amount = die1 + die2;
allPlayers[playerToGo].move(amount);
QString info = QString("Rolling ... Move %1 Spaces" ).arg(amount);
QMessageBox::information(0, "Roll Information", info);
}
int Board::setNumberOfPlayers() //Prompts user to enter number of players between two and four
{
int players = QInputDialog::getInt(0, "Number Of Players", "How Many Players?: ", 1);
while (players < 2 || players > 4)
players = QInputDialog::getInt(0, "Invalid Input", "How Many Players?: ", 1); //bad input checking
allPlayers = new Player[players];
return players;
}
void Board::setTokens() //Prompts user to enter the token representing each player
{
for (int i = 0; i < numPlayers; i++)
{
QString text = QString("Enter the token Player %1 should use %2:").arg(i + 1).arg(printTokens());
QString token = QInputDialog::getText(0, "Token Type", text);
while (findMatch(token) || !isValidToken(token))
{
if (findMatch(token))
token = QInputDialog::getText(0, "Token Already Used", text);
else
token = QInputDialog::getText(0, "Invalid Token", text);
}
allPlayers[i].setToken(token);
}
}
bool Board::findMatch(QString tok) //Supplementary functions for the setTokens’ checking
{
for (int i = 0; i < numPlayers; i++)
if (tok == allPlayers[i].getToken())
return true;
return false;
}
bool Board::isValidToken(QString tok) //Supplementary functions for the setTokens’ checking
{
for (int i = 0; i < NUM_TOKENS; i++)
if (tok == TOKENS[i])
return true;
return false;
}
QString Board::printTokens() //Print out the four tokens available for use
{
QString str = "{";
for (int i = 0; i < NUM_TOKENS; i++)
{
str += TOKENS[i];
if(i != NUM_TOKENS - 1)
str += ',';
}
str += "}";
return str;
}
void Board::printSquare(int loc, int group)
{
cout << loc << " " << group << endl;
}
bool Board::hasAction()
{
return (allPlayers[playerToGo].getPosition() % 5 != 0 && board[allPlayers[playerToGo].getPosition()]->getOwner() == -1)
|| (board[allPlayers[playerToGo].getPosition()]->getOwner() == playerToGo);
}
void Board::shuffleCards() //Shuffles the cards that would be picked up if a player land on designated spaces
{
topCard = 0;
for (int i = 0; i < NUM_CARDS; i++)
cardOrder[i] = i;
for (int i = 0; i < 10000; i++)
{
int first = rand() % NUM_CARDS, second = rand() % NUM_CARDS;
int temp = cardOrder[first];
cardOrder[first] = cardOrder[second];
cardOrder[second] = temp;
}
}
void Board::drawCard(Player &p) //Prints out the action of the card being picked up
{
Card *card = getCard(cardOrder[topCard]);
topCard++;
if (topCard == NUM_CARDS)
shuffleCards();
card->executeAction(p); //Executes the action of the card
QString info = card->getDescription();
QMessageBox::information(0, "Card Information", info);
}
void Board::fillBoard()
{
int value = 100;
board = new Space*[40];
for (int i = 0; i < 40; i++)
{
if (i % 10 == 0)
board[i] = new Space(false, false, 0, 0, PLACE_NAMES[i]);
else if (i % 10 == 5)
board[i] = new Space(true, false, 0, 0, PLACE_NAMES[i]);
else
{
board[i] = new Space(false, true, value, value * 3, PLACE_NAMES[i]);
value += 50;
}
}
}
Card* Board::getCard(int pos) //Sub-function of drawCard to help execute the actions from moveaction and MoneyAction
{
switch (CARD_TYPE[pos])
{
case 1: return new moveaction(CARD_DESCRIPTION[pos], CARD_VALUE[pos]);
break;
case 2: return new MoneyAction(CARD_DESCRIPTION[pos], CARD_VALUE[pos]);
break;
}
return new GotoAction(CARD_DESCRIPTION[pos], CARD_VALUE[pos]);
}
void Board::doAutomaticActions()
{
Space *sp = board[allPlayers[playerToGo].getPosition()];
if (sp->canDraw())
{
drawCard(allPlayers[playerToGo]);
sp = board[allPlayers[playerToGo].getPosition()];
}
if (sp->isPurchasable() && sp->getOwner() != playerToGo && sp->getOwner() != -1)
{
int amt = sp->getValue();
if (sp->getUpgradeValue() > 0)
{
amt *= 0.10;
QString info = QString("Player %1 must pay 10% rent: %2.").arg(playerToGo + 1).arg(amt);
QMessageBox::information(0, "Bought Information", info);
}
else
{
amt *= 0.20;
QString info = QString("Player %1 must pay 20% rent: %2.").arg(playerToGo + 1).arg(amt);
QMessageBox::information(0, "Bought Information", info);
}
Card *act = new MoneyAction("rent", -1 * amt);
Card *act2 = new MoneyAction("give", amt);
act->executeAction(allPlayers[playerToGo]);
act2->executeAction(allPlayers[sp->getOwner()]);
}
}
void Board::doAction(int i)
{
Space *sp = board[allPlayers[i].getPosition()];
if (sp->isPurchasable())
{
if (sp->getOwner() == -1 && sp->getValue() < allPlayers[i].getMoney())
{
//Prompts user to buy an un-owned property
QString text = QString("Would you like to purchase %1 for $ %2 ? ").arg(sp->getName().trimmed()).arg(sp->getValue());
int resp = QMessageBox::question(0, "Property Question", text, QMessageBox::Yes|QMessageBox::No);
if (resp == QMessageBox::Yes)
{
Card *act = new MoneyAction("subtract", -1 * sp->getValue());
act->executeAction(allPlayers[i]);
QString info = QString("Player %1 bought %2.").arg(i + 1).arg(sp->getName());
QMessageBox::information(0, "Bought Information", info);
sp->setOwner(i);
}
}
else if (sp->getOwner() == -1) //Checks if player has enough money
{
QString info = QString("Cannot Buy Property (Not Enough Money). Need: $ %1").arg(sp->getValue());
QMessageBox::information(0, "Not Enough Money", info);
}
else if (sp->getOwner() == i && sp->getUpgradeValue() < allPlayers[i].getMoney())
{
if (sp->getUpgradeValue() > 0)
{
//Prompts user to upgrade his own property
QString text = QString("Would you like to upgrade %1 for $ %2 ? ").arg(sp->getName().trimmed()).arg(sp->getValue());
int resp = QMessageBox::question(0, "Property Question", text, QMessageBox::Yes|QMessageBox::No);
if (resp == QMessageBox::Yes)
{
Card *act = new MoneyAction("subtract", -1 * sp->getUpgradeValue());
act->executeAction(allPlayers[i]);
sp->setUpgradeValue(0);
QString info = QString("Player %1 upgraded %2.").arg(i + 1).arg(sp->getName());
QMessageBox::information(0, "Bought Information", info);
}
}
else
QMessageBox::information(0, "Max Upgraded", "Cannot Upgrade Any Further.");
}
}
}
int Board::numAlivePlayers()
{
int count = 0;
for (int i = 0; i < numPlayers; i++)
if (allPlayers[i].isAlive())
count++;
return count;
}
int Board::findWinner()
{
for (int i = 0; i < numPlayers; i++)
if (allPlayers[i].isAlive())
return i;
return -1;
}
void Board::removeProperties(int player)
{
int value = 100;
for (int i = 0; i < 40; i++)
{
if (board[i]->getOwner() == player)
{
board[i]->setOwner(-1);
board[i]->setValue(value);
board[i]->setUpgradeValue(value * 3);
QString info = QString("Freed up: %1").arg(board[i]->getName());
QMessageBox::information(0, "Bought Information", info);
value += 50;
}
else if (i % 10 != 0 && i % 10 != 5)
value += 50;
}
}