-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
202 lines (160 loc) · 4.75 KB
/
Player.cpp
File metadata and controls
202 lines (160 loc) · 4.75 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
#include "Player.h"
#include "GameObject.h"
int Player::EqualWallets = -1; // initializing it with -1 refers to that until now no players have the same amount of money
Player::Player(Cell* pCell, int playerNum) : stepCount(0), wallet(100), playerNum(playerNum)
{
this->pCell = pCell;
this->turnCount = 0;
this->SetTurnsDisabled(0);
// Make all the needed initialization or validations
SetCell(pCell);
}
// ====== Setters and Getters ======
void Player::SetCell(Cell* cell)
{
pCell = cell;
}
Cell* Player::GetCell() const
{
return pCell;
}
void Player::SetWallet(int wallet)
{
this->wallet = wallet;
// Make any needed validations
if (this->wallet < 0)
{
this->wallet = 0;
}
}
int Player::GetWallet() const
{
return wallet;
}
int Player::GetTurnCount() const
{
return turnCount;
}
void Player::SetTurnCount(int t)
{
if (t<5 && t>-1)
turnCount = t;
else
turnCount = 0;
}
void Player::SetTurnsDisabled(int t)
{
TurnsDisabled = (t >= 0) ? t : 0;
}
int Player::getTurnsDisabled() const
{
return TurnsDisabled;
}
int Player::GetjustRolledDiceNum() const
{
return justRolledDiceNum;
}
int Player::GetEqualWallets()const
{
return EqualWallets;
}
int Player::getPlayerNum() const
{
return playerNum;
}
Player* Player::GetPoor(Player* P2)
{
if (GetWallet() < P2->GetWallet())
return this;
else if (GetWallet() > P2->GetWallet())
return P2;
else // They have the same amount of money
{
EqualWallets = GetWallet();
return this;
}
}
bool Player::IsEqualWallets()
{
if (EqualWallets == -1)
return false;
else
{
EqualWallets = -1;
return true;
}
}
// ====== Drawing Functions ======
void Player::Draw(Output* pOut) const
{
color playerColor = UI.PlayerColors[playerNum];
///TODO: use the appropriate output function to draw the player with "playerColor"
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)
{
///TODO: Implement this function as mentioned in the guideline steps (numbered below) below
Output* pOut = pGrid->GetOutput();
// == Here are some guideline steps (numbered below) to implement this function ==
// 1- Increment the turnCount because calling Move() means that the player has rolled the dice once
this->turnCount++;
// 2- Check the turnCount to know if the wallet recharge turn comes (recharge wallet instead of move)
// If yes, recharge wallet and reset the turnCount and return from the function (do NOT move)
if (this->turnCount == 4)
{
wallet += 10 * diceNumber;
this->turnCount = 0;
pOut->PrintMessage("Fourth time to roll ! Wallet increased by " + to_string(10 * diceNumber));
return;
}
// 3- Set the justRolledDiceNum with the passed diceNumber
if (getTurnsDisabled() == 0)
{
justRolledDiceNum = diceNumber;
// 4- Get the player current cell position, say "pos", and add to it the diceNumber (update the position)
// Using the appropriate function of CellPosition class to update "pos"
CellPosition pos(pCell->GetCellPosition());
pos.AddCellNum(diceNumber);
// 5- Use pGrid->UpdatePlayerCell() func to Update player's cell POINTER (pCell) with the cell in the passed position, "pos" (the updated one)
// the importance of this function is that it Updates the pCell pointer of the player and Draws it in the new position
pGrid->UpdatePlayerCell(this, pos);
// 6- Apply() the game object of the reached cell (if any)
if (pCell->GetCellPosition().GetCellNum()+justRolledDiceNum== 100)
{
pGrid->SetEndGame(true);
pOut->PrintMessage("Game Finished, P("+to_string(playerNum)+ ") won!");
pGrid->UpdatePlayerCell(this, 99);
}
CellPosition BeforeObj = pCell->GetCellPosition();
GameObject* pObj = this->pCell->GetGameObject();
if (pObj&&!pGrid->GetEndGame())
{
pObj->Apply(pGrid, this);
while (BeforeObj.GetCellNum() != pCell->GetCellPosition().GetCellNum() && this->pCell->GetGameObject() != NULL)
{
pObj = this->pCell->GetGameObject();
BeforeObj = pCell->GetCellPosition();
pObj->Apply(pGrid, this);
}
}
// 7- Check if the player reached the end cell of the whole game, and if yes, Set end game with true: pGrid->SetEndGame(true)
}
else
{
pOut->PrintMessage("Dice roll prevented ! Wait for the next turn..");
SetTurnsDisabled(TurnsDisabled - 1);
}
}
void Player::AppendPlayerInfo(string& playersInfo) const
{
playersInfo += "P" + to_string(playerNum) + "(";
playersInfo += to_string(wallet) + ", ";
playersInfo += to_string(turnCount) + ")";
}