forked from ahmedibrahim404/Snakes_Ladders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.cpp
455 lines (385 loc) · 10.2 KB
/
Grid.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
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
#include "Grid.h"
#include "Cell.h"
#include "GameObject.h"
#include "Ladder.h"
#include "Snake.h"
#include "Player.h"
#include "Card.h"
#include "CardOne.h"
#include "CardTwo.h"
#include "CardThree.h"
#include "CardFour.h"
#include "CardFive.h"
#include "CardSix.h"
#include "CardSeven.h"
#include "CardEight.h"
#include "CardNine.h"
#include "CardTen.h"
#include "CardEleven.h"
#include "CardTwelve.h"
Grid::Grid(Input* pIn, Output* pOut) : pIn(pIn), pOut(pOut) // Initializing pIn, pOut
{
// Allocate the Cell Objects of the CellList
for (int i = NumVerticalCells - 1; i >= 0; i--) // to allocate cells from bottom up
{
for (int j = 0; j < NumHorizontalCells; j++) // to allocate cells from left to right
{
CellList[i][j] = new Cell(i, j);
}
}
// Allocate thePlayer Objects of the PlayerList
for (int i = 0; i < MaxPlayerCount; i++)
{
PlayerList[i] = new Player(CellList[NumVerticalCells - 1][0], i); // first cell
PlayerList[i]->Draw(pOut); // initially draw players in the first cell
}
// Initialize currPlayerNumber with 0 (first player)
currPlayerNumber = 0; // start with the first player
// Initialize Clipboard with NULL
Clipboard = NULL;
// Initialize endGame with false
endGame = false;
}
// ========= Adding or Removing GameObjects to Cells =========
bool Grid::AddObjectToCell(GameObject* pNewObject) // think if any validation is needed
{
// Get the cell position of pNewObject
CellPosition pos = pNewObject->GetPosition();
if (pos.IsValidCell()) // Check if valid position
{
// Get the previous GameObject of the Cell
GameObject* pPrevObject = CellList[pos.VCell()][pos.HCell()]->GetGameObject();
if (pPrevObject) // the cell already contains a game object
return false; // do NOT add and return false
if (IsOverlapping(pNewObject)) return false;
// Set the game object of the Cell with the new game object
CellList[pos.VCell()][pos.HCell()]->SetGameObject(pNewObject);
return true; // indicating that addition is done
}
return false; // if not a valid position
}
// Note: You may need to change the return type of this function (Think)
void Grid::RemoveObjectFromCell(const CellPosition& pos)
{
if (pos.IsValidCell()) // Check if valid position
{
// Note: you can deallocate the object here before setting the pointer to null if it is needed
Cell *pCell = CellList[pos.VCell()][pos.HCell()];
delete pCell->GetGameObject();
CellList[pos.VCell()][pos.HCell()]->SetGameObject(NULL);
}
}
void Grid::UpdatePlayerCell(Player* player, const CellPosition& newPosition)
{
// Clear the player's circle from the old cell position
player->ClearDrawing(pOut);
// Set the player's CELL with the new position
Cell* newCell = CellList[newPosition.VCell()][newPosition.HCell()];
player->SetCell(newCell);
// Draw the player's circle on the new cell position
player->Draw(pOut);
}
bool Grid::IsOverlapping(GameObject* p) {
int hCell = p->GetPosition().HCell();
for (int i = 0; i < NumVerticalCells; i++) {
GameObject* curCell = CellList[i][hCell]->GetGameObject();
if (p->IsOverlapping(curCell))
return true;
}
return false;
}
// ========= Setters and Getters Functions =========
Input* Grid::GetInput() const
{
return pIn;
}
Output* Grid::GetOutput() const
{
return pOut;
}
void Grid::SetClipboard(Card* pCard) // to be used in copy/cut
{
// you may update slightly in implementation if you want (but without breaking responsibilities)
Clipboard = pCard;
}
Card* Grid::GetClipboard() const // to be used in paste
{
return Clipboard;
}
void Grid::SetEndGame(bool endGame)
{
this->endGame = endGame;
}
bool Grid::GetEndGame() const
{
return endGame;
}
void Grid::AdvanceCurrentPlayer()
{
currPlayerNumber = (currPlayerNumber + 1) % MaxPlayerCount; // this generates value from 0 to MaxPlayerCount - 1
if (PlayerList[currPlayerNumber]->isPrevented()) {
PlayerList[currPlayerNumber]->decreasePreventTimes();
AdvanceCurrentPlayer();
}
}
void Grid::RollCurrentPlayer()
{
currPlayerNumber = (currPlayerNumber - 1 + MaxPlayerCount) % MaxPlayerCount; // this generates value from 0 to MaxPlayerCount - 1
}
void Grid::setCurrentPlayer(int player) {
currPlayerNumber = player;
}
void Grid::PreventNextTime(Player* currentPlayer) {
currentPlayer->preventNextTime();
}
int Grid::GetLaddersCount()
{
return Ladder::getLaddersCount();
}
int Grid::GetSnakesCount()
{
return Snake::getSnakesCount();
}
int Grid::GetCardsCount()
{
return Card::getCardsCount();
}
Card* Grid::GetCard(CellPosition& Pos)
{
if (!Pos.IsValidCell()) {
return NULL;
}
return CellList[Pos.VCell()][Pos.HCell()]->HasCard();
}
// ========= Other Getters =========
Player* Grid::GetCurrentPlayer() const
{
return PlayerList[currPlayerNumber];
}
Ladder* Grid::GetNextLadder(const CellPosition& position)
{
int startH = position.HCell(); // represents the start hCell in the current row to search for the ladder in
for (int i = position.VCell(); i >= 0; i--) // searching from position.vCell and ABOVE
{
for (int j = startH; j < NumHorizontalCells; j++) // searching from startH and RIGHT
{
Ladder* nextLadder = CellList[i][j]->HasLadder();
if (nextLadder != NULL) {
return nextLadder;
}
///TODO: Check if CellList[i][j] has a ladder, if yes return it
}
startH = 0; // because in the next above rows, we will search from the first left cell (hCell = 0) to the right
}
return NULL; // not found
}
Player* Grid::GetNextPlayer(const CellPosition& position)
{
int startH = position.HCell() + 1;
for (int i = position.VCell(); i >= 0; i--)
{
for (int j = startH; j < NumHorizontalCells; j++) // searching from startH and RIGHT
{
for (int k = 0; k < MaxPlayerCount; k++) {
CellPosition currentPlayerCell = PlayerList[k]->GetCell()->GetCellPosition();
if (currentPlayerCell.HCell() == j && currentPlayerCell.VCell() == i) return PlayerList[k];
}
}
startH = 0;
}
return NULL;
}
Player* Grid::GetPoorestPlayer() {
Player* poorest = PlayerList[0];
for (int i = 0; i < MaxPlayerCount; i++) {
if (poorest->GetWallet() > PlayerList[i]->GetWallet()) {
poorest = PlayerList[i];
}
}
return poorest;
}
// ========= User Interface Functions =========
void Grid::UpdateInterface() const
{
if (UI.InterfaceMode == MODE_DESIGN)
{
// 1- Draw cells with or without cards
for (int i = NumVerticalCells - 1; i >= 0; i--) // bottom up
{
for (int j = 0; j < NumHorizontalCells; j++) // left to right
{
CellList[i][j]->DrawCellOrCard(pOut);
}
}
// 2- Draw other cell objects (ladders, snakes)
for (int i = NumVerticalCells - 1; i >= 0; i--) // bottom up
{
for (int j = 0; j < NumHorizontalCells; j++) // left to right
{
CellList[i][j]->DrawLadderOrSnake(pOut);
}
}
// 3- Draw players
for (int i = 0; i < MaxPlayerCount; i++)
{
PlayerList[i]->Draw(pOut);
}
}
else // In PLAY Mode
{
// 1- Print Player's Info
string playersInfo = "";
for (int i = 0; i < MaxPlayerCount; i++)
{
PlayerList[i]->AppendPlayerInfo(playersInfo); // passed by reference
if (i < MaxPlayerCount - 1) // except the last player
playersInfo += ", ";
}
playersInfo += " | Curr = " + to_string(currPlayerNumber);
pOut->PrintPlayersInfo(playersInfo);
// Note: UpdatePlayerCell() function --> already update drawing players in Play Mode
// so we do NOT need draw all players again in UpdateInterface() of the Play mode
// In addition, cards/snakes/ladders do NOT change positions in Play Mode, so need to draw them here too
}
}
void Grid::PrintErrorMessage(string msg)
{
pOut->PrintMessage(msg);
int x, y;
pIn->GetPointClicked(x, y);
pOut->ClearStatusBar();
}
// ========= Save Grid ============
void Grid::SaveAll(ofstream& OutFile, ObjectType type)
{
for (int i = 0; i < NumVerticalCells; i++)
{
for (int j = 0; j < NumHorizontalCells; j++)
{
if (type == ObjectType::TypeLadder)
{
if (CellList[i][j]->HasLadder())
{
CellList[i][j]->GetGameObject()->Save(OutFile);
}
}
else if (type == ObjectType::TypeSnake)
{
if (CellList[i][j]->HasSnake())
{
CellList[i][j]->GetGameObject()->Save(OutFile);
}
}
else if (type == ObjectType::TypeCard)
{
if (CellList[i][j]->HasCard())
{
CellList[i][j]->GetGameObject()->Save(OutFile);
}
}
}
}
}
void Grid::LoadAll(ifstream& InFile, Grid* pGrid)
{
int LaddersNum, SnakesNum, CardsNum, CardType, CellNum;
CellPosition C;
Ladder* pLadder;
Snake* pSnake;
Card* pCard;
InFile >> LaddersNum;
while (LaddersNum--)
{
int sCell, eCell;
InFile >> sCell >> eCell;
pLadder = new Ladder(CellPosition::GetCellPositionFromNum(sCell), CellPosition::GetCellNumFromPosition(eCell));
pLadder->Load(InFile, pGrid);
}
InFile >> SnakesNum;
while (SnakesNum--)
{
int sCell, eCell;
InFile >> sCell >> eCell;
pSnake = new Snake(CellPosition::GetCellPositionFromNum(sCell), CellPosition::GetCellNumFromPosition(eCell));
pSnake->Load(InFile, pGrid);
}
InFile >> CardsNum;
while (CardsNum--)
{
InFile >> CardType;
InFile >> CellNum;
C = CellPosition::GetCellPositionFromNum(CellNum);
switch (CardType){
case 1:
pCard = new CardOne(C);
break;
case 2:
pCard = new CardTwo(C);
break;
case 3:
pCard = new CardThree(C);
break;
case 4:
pCard = new CardFour(C);
break;
case 5:
pCard = new CardFive(C);
break;
case 6:
pCard = new CardSix(C);
break;
case 7:
pCard = new CardSeven(C);
break;
case 8:
pCard = new CardEight(C);
break;
case 9:
pCard = new CardNine(C);
break;
case 10:
pCard = new CardTen(C);
break;
case 11:
pCard = new CardEleven(C);
break;
case 12:
pCard = new CardTwelve(C);
break;
}
if(pCard != nullptr)
pCard->Load(InFile, pGrid);
}
}
void Grid::ClearGrid()
{
for (int i = 0; i < NumVerticalCells; i++)
{
for (int j = 0; j < NumHorizontalCells; j++)
{
if (CellList[i][j]->GetGameObject())
{
delete CellList[i][j]->GetGameObject();
CellList[i][j]->SetGameObject(NULL);
}
}
}
}
Grid::~Grid()
{
delete pIn;
delete pOut;
// Deallocate the Cell Objects of the CellList
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j < NumHorizontalCells; j++)
{
delete CellList[i][j];
}
}
// Deallocate the Player Objects of the PlayerList
for (int i = 0; i < MaxPlayerCount; i++)
{
delete PlayerList[i];
}
}