-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cpp
367 lines (321 loc) · 10.1 KB
/
Player.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
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
#include "Player.h"
#include "GameObject.h"
Player::Player(Cell* pCell, int playerNum) : stepCount(0), wallet(100), playerNum(playerNum)
{
this->pCell = pCell;
this->turnCount = 0;
// Make all the needed initialization or validations
this->justRolledDiceNum = 0;
this->pGrid = NULL;
this->pGameObject = NULL;
this->prevented = false;
this->turnsInPrison = -1;
this->specialAttacksUsed = 0;
this->burned = false;
this->burnedTurns = 0;
this->poisoned = false;
this->poisonedTurns = 0;
this->usedFire = false;
this->usedIce = false;
this->usedPoison = false;
this->usedLightning = false;
}
// ====== Setters and Getters ======
void Player::SetCell(Cell* cell)
{
pCell = cell;
}
Cell* Player::GetCell() const
{
return pCell;
}
void Player::SetWallet(int wallet)
{
if (wallet >= 0)
this->wallet = wallet;
else
wallet = 0;
}
void Player::SetTurnCount(int count) {
turnCount = count;
}
void Player::PreventNextTurn(bool choice)
{
prevented = choice;
}
void Player::PutInPrison()
{
prevented = true;
turnsInPrison = 1;
}
bool Player::IsInPrison() const
{
if (IsPrevented() && turnsInPrison != -1) return true;
return false;
}
void Player::GetOutOfPrison() {
prevented = false;
turnsInPrison = -1;
}
bool Player::Pay(int amount) {
if (amount > wallet) return false;
this->SetWallet(GetWallet() - amount);
return true;
}
bool Player::IsPrevented() const
{
return prevented;
}
int Player::GetWallet() const
{
return wallet;
}
int Player::GetTurnCount() const
{
return turnCount;
}
int Player::GetPlayerNum() const
{
return playerNum;
}
// ====== Drawing Functions ======
void Player::Draw(Output* pOut) const
{
color playerColor = UI.PlayerColors[playerNum];
pOut->DrawPlayer(pCell->GetCellPosition(), playerNum, playerColor);
}
void Player::ClearDrawing(Output* pOut) const
{
color cellColor = pCell->HasCard() ? UI.CellColor_HasCard : UI.CellColor_NoCard;
///TODO: use the appropriate output function to draw the player with "cellColor" (to clear it) **
pOut->DrawPlayer(pCell->GetCellPosition(), playerNum, cellColor);
}
// ====== Game Functions ======
void Player::Move(Grid* pGrid, int diceNumber)
{
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
turnCount++;
// If the player is burned he/she will lose 20 coins for 3 games
if (this->burned) {
this->Pay(20);
burnedTurns++;
if (burnedTurns == 3)
{
burned = false;
burnedTurns = 0;
}
}
// If the player is poisoned he/she will have a dicenumber-1 for 5 games
if (this->poisoned) {
diceNumber--;
poisonedTurns++;
if (poisonedTurns == 5)
{
poisoned = false;
poisonedTurns = 0;
}
}
// If the player is prisoned he/she should spend 3 games in prison
if (IsInPrison()) {
pOut->PrintMessage("Player : " + to_string(GetPlayerNum()) + " is in prison.");
turnsInPrison++;
if (turnsInPrison == 4) {
pOut->PrintMessage("Player : " + to_string(GetPlayerNum()) + " is now out of prison.");
this->GetOutOfPrison();
}
}
// If the player is prevented from moving he won't move this turn
else if (IsPrevented()) {
pOut->PrintMessage("Player : " + to_string(GetPlayerNum()) + " is prevented from rolling this turn.");
this->PreventNextTurn(false);
}
// If the player has money less than 1 he can't move
else if (wallet < 1) {
pGrid->PrintErrorMessage("Player: " + to_string(playerNum) + " Can't move for having money less than 1, Click to continue...");
}
// If it's recharge turn the player won't move
else if (turnCount % 3 == 0 && turnCount != 0) {
pGrid->PrintErrorMessage("It's wallet recharge turn for player " + to_string(GetPlayerNum()) + ". Click to continue...");
if (specialAttacksUsed < 3) {
pOut->PrintMessage("Do You Wish To Launch A Special Attack Instead Of Recharging ? y/n");
string choice = pIn->getString(pOut);
if (tolower(choice[0]) == 'y') {
string msg = "Choose one of the following available attacks : ";
if (!usedIce) msg += " 1-Ice";
if (!usedFire) msg += " 2-Fire";
if (!usedPoison) msg += " 3-Poison";
if (!usedLightning) msg += " 4-Lightning";
pOut->PrintMessage(msg);
int attack = pIn->GetInteger(pOut);
while (attack < 0 || attack > 5) {
pOut->PrintMessage(msg);
}
LaunchSpecialAttack(attack, pGrid);
}
else {
wallet = 10 * diceNumber + wallet;
pOut->PrintMessage("Player: " + to_string(playerNum) + " Increased Money By: " + to_string(diceNumber * 10));
}
}
else {
wallet = 10 * diceNumber + wallet;
pOut->PrintMessage("Player: " + to_string(playerNum) + " Increased Money By: " + to_string(diceNumber * 10));
}
}
else {
justRolledDiceNum = diceNumber;
CellPosition pos = pCell->GetCellPosition();
pos.AddCellNum(justRolledDiceNum);
pGrid->UpdatePlayerCell(this, pos);
pGameObject = pCell->GetGameObject();
if (pCell->HasCard() || pCell->HasLadder() || pCell->HasSnake())
pGameObject->Apply(pGrid, this);
if (CellPosition::GetCellNumFromPosition(pCell->GetCellPosition()) == NumHorizontalCells * NumVerticalCells) {
pGrid->SetEndGame(true);
pGrid->PrintErrorMessage("Game Over! Player : " + to_string(GetPlayerNum()) + " Won!");
}
}
}
void Player::AppendPlayerInfo(string& playersInfo) const
{
playersInfo += "P" + to_string(playerNum) + "(";
playersInfo += to_string(wallet) + "$, ";
playersInfo += to_string(turnCount) + ", ";
if (this->IsInPrison()) playersInfo += "Prison, ";
if (this->IsPrevented() && !this->IsInPrison()) playersInfo += "Prevented, ";
if (this->burned) playersInfo += "Fire, ";
if (this->poisoned) playersInfo += "Poison, ";
playersInfo += to_string(2 - specialAttacksUsed) + ")";
}
int Player::getJustRolledDiceNumber()const
{
return justRolledDiceNum;
}
void Player::Reset()
{
SetWallet(100); // Initial Values for turncount , wallet and Prevented
this->turnCount = 0;
prevented = false;
specialAttacksUsed = 0;
burned = false;
poisoned = false;
}
void Player::UseIceSpecialAttack(Player* p, Output* pOut)
{
// Checking if the player didn't use his ice special attack before and if he didn't play all his available attacks
if (specialAttacksUsed < 2 && !usedIce) {
p->prevented = true;
pOut->PrintMessage("Player: " + to_string(p->GetPlayerNum()) + " is now Frozen by player : " + to_string(this->GetPlayerNum()));
usedIce = true;
specialAttacksUsed++;
}
else {
pOut->PrintMessage("Player : " + to_string(this->GetPlayerNum()) + " used all his special attacks!");
}
}
void Player::UseFireSpecialAttack(Player* p, Output* pOut)
{
// Checking if the player didn't use his fire special attack before and if he didn't play all his available attacks
if (specialAttacksUsed < 2 && !usedFire) {
p->burned = true;
pOut->PrintMessage("Player: " + to_string(p->GetPlayerNum()) + " is now burned by player : " + to_string(this->GetPlayerNum()));
usedFire = true;
specialAttacksUsed++;
}
else {
pOut->PrintMessage("Player : " + to_string(this->GetPlayerNum()) + " used all his special attacks, Click to continue...");
}
}
void Player::UsePoisonSpecialAttack(Player* p, Output* pOut)
{
// Checking if the player didn't use his poison special attack before and if he didn't play all his available attacks
if (specialAttacksUsed < 2 && !usedPoison) {
p->poisoned = true;
pOut->PrintMessage("Player: " + to_string(p->GetPlayerNum()) + " is now poisoned by player : " + to_string(this->GetPlayerNum()));
usedPoison = true;
specialAttacksUsed++;
}
else {
pOut->PrintMessage("Player : " + to_string(this->GetPlayerNum()) + " used all his special attacks, Click to continue...");
}
}
void Player::UseLightningSpecialAttack(Output* pOut, Grid* pGrid)
{
// Checking if the player didn't use his ice special lightning before and if he didn't play all his available attacks
if (specialAttacksUsed < 2 && !usedLightning) {
string msg = "Players : ";
for (int i = 0; i < MaxPlayerCount; i++) {
if (i != playerNum) {
pGrid->GetPlayerByPlayerNum(i)->Pay(20);
msg += to_string(i) + ", ";
}
}
pOut->PrintMessage(msg + "lost 20 coins!");
specialAttacksUsed++;
}
else {
pOut->PrintMessage("Player : " + to_string(this->GetPlayerNum()) + " used all his special attacks, Click to continue...");
}
}
void Player::LaunchSpecialAttack(int type, Grid* pGrid) {
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
// If it's a lightning attack there is no target player
if (type == 4) {
UseLightningSpecialAttack(pOut, pGrid);
}
else {
string msg = "Choose one of the following players to attack : ";
for (int i = 0; i < MaxPlayerCount; i++) {
if (i != playerNum) msg += to_string(i);
}
pOut->PrintMessage(msg);
int num = pIn->GetInteger(pOut);
while (num < 0 || num > 3 || num == playerNum)
{
pOut->PrintMessage(msg);
num = pIn->GetInteger(pOut);
}
Player* p = pGrid->GetPlayerByPlayerNum(num);
if (type == 1) {
// Getting a valid player number
num = pIn->GetInteger(pOut);
while (num < 0 || num > 3 || num == playerNum)
{
pOut->PrintMessage(msg);
num = pIn->GetInteger(pOut);
}
// Making sure that the player is not already frozen
while (p->IsPrevented()) {
p = pGrid->GetPlayerByPlayerNum(num);
pOut->PrintMessage("Player: " + to_string(p->GetPlayerNum()) + " Is Already Frozen! Try another player");
num = pIn->GetInteger(pOut);
}
UseIceSpecialAttack(p, pOut);
}
else if (type == 2) UseFireSpecialAttack(p, pOut);
else if (type == 3) UsePoisonSpecialAttack(p, pOut);
else pOut->PrintMessage("The Attack type is invalid!");
}
}
int Player:: GetNextPlayer(int &MinCellNum, int currentCellNum, int i, int NextPlayerIndex)
{
CellPosition* cellPosition;
int cellNum;
//Gets the cell number of each of the rest players
cellNum = cellPosition->GetCellNumFromPosition((this->GetCell())->GetCellPosition());
if (cellNum > currentCellNum)
{
//if cell number of the player(i) is greater than the cell number of the current player
//and if the minimum cell number is graeter than the cell number of the player(i)
//then set the index of the first next player with the index number of player(i)
//and set the minimum cell number with the cell number of the player(i)
if (MinCellNum > cellNum)
{
NextPlayerIndex = i;
MinCellNum = cellNum;
}
}
return NextPlayerIndex;
}