-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
620 lines (520 loc) · 15.4 KB
/
Copy pathGrid.cpp
File metadata and controls
620 lines (520 loc) · 15.4 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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include "Grid.h"
#include <iomanip>
#include"CellPosition.h"
#include "Cell.h"
#include "GameObject.h"
#include "Belt.h"
#include "Player.h"
#include "Flag.h"
#include "Action.h"
#include "WaterPit.h"
#include "DangerZone.h"
#include "RotatingGear.h"
#include "Workshop.h"
#include "Antenna.h"
#include "fstream"
#include "iostream"
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
// 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)
bool 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
CellList[pos.VCell()][pos.HCell()]->SetGameObject(NULL);
return true;
}
else
{
return false;
}
}
void Grid::UpdatePlayerCell(Player * player, const CellPosition & newPosition)
{
// Clear the player's triangle 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 triangle on the new cell position
player->Draw(pOut);
}
bool Grid::HasFlag() const {
for (int i = 0; i < NumVerticalCells; ++i) {
for (int j = 0; j < NumHorizontalCells; ++j) {
if (dynamic_cast<Flag*>(CellList[i][j]->GetGameObject())) {
return true;
}
}
}
return false;
}
bool Grid::HasAntenna() const
{
for (int i = 0; i < NumVerticalCells; ++i) {
for (int j = 0; j < NumHorizontalCells; ++j) {
if (dynamic_cast<Antenna*>(CellList[i][j]->GetGameObject())) {
return true;
}
}
}
return false;
}
bool Grid::Beltsconflicts(CellPosition& startCell, CellPosition& endCell) const
{
for (int i = 0; i < NumVerticalCells; i++) {
for (int j = 0; j < NumHorizontalCells; j++) {
GameObject* obj = CellList[i][j]->GetGameObject();
Belt* belt = dynamic_cast<Belt*>(obj);
if (belt) {
CellPosition beltStart = belt->GetPosition();
CellPosition beltEnd = belt->GetEndPosition();
// Check if one belt's end is another's start
if ((startCell.GetCellNum() == beltEnd.GetCellNum() || endCell.GetCellNum() == beltStart.GetCellNum())) {
return true;
}
}
}
}
return false;
}
void Grid::setCurrentplayer(int num)
{
currPlayerNumber = num;
}
bool Grid::isflag(CellPosition pos)
{
if (CellList[pos.VCell()][pos.HCell()]->HasFlag())
{
return true;
}
return false;
}
// ========= Setters and Getters Functions =========
Input * Grid::GetInput() const
{
return pIn;
}
Output * Grid::GetOutput() const
{
return pOut;
}
void Grid::SetClipboard(GameObject * gameObject) // to be used in copy/cut
{
// you may update slightly in implementation if you want (but without breaking responsibilities)
Clipboard = gameObject;
}
GameObject * 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
}
GameObject* Grid::getGameobject(const CellPosition& CELL) const
{
Cell* cell = CellList[CELL.VCell()][CELL.HCell()];
if (cell!=nullptr)
{
return cell->GetGameObject();
}
return nullptr;
}
void Grid::SaveAll(ofstream& OutFile, string file, Type type)
{
int Flags_num = 0, Antennas_num = 0, DangerZones_num = 0, RotatingGears_num = 0, WaterPits_num = 0, Workshops_num = 0, Belt_num = 0;
//Counters For GameObjects
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j <= NumHorizontalCells - 1; j++)
{
if (dynamic_cast<Flag*>(CellList[i][j]->GetGameObject())) //Chaceking if the GameObject at this postion is Flag
Flags_num++;
if (dynamic_cast<Antenna*>(CellList[i][j]->GetGameObject())) //Chaceking if the GameObject at this postion is Antenna
Antennas_num++;
if (dynamic_cast<DangerZone*>(CellList[i][j]->GetGameObject()))//Chaceking if the GameObject at this postion is DangerZone
DangerZones_num++;
if (dynamic_cast<RotatingGear*>(CellList[i][j]->GetGameObject())) //Chaceking if the GameObject at this postion is RotatingGear
RotatingGears_num++;
if (dynamic_cast<WaterPit*>(CellList[i][j]->GetGameObject())) //Chaceking if the GameObject at this postion is WaterPit
WaterPits_num++;
if (dynamic_cast<Workshop*>(CellList[i][j]->GetGameObject())) //Chaceking if the GameObject at this postion is Workshop
Workshops_num++;
if (dynamic_cast<Belt*>(CellList[i][j]->GetGameObject())) //Chaceking if the GameObject at this postion is Belt
Belt_num++;
}
}
//1st step print out the Number of flags
//2nd step for loops for the whole grid
//3th step casting the object at the cell if success it will save it
//4th save the object
//5th step add the object to the grid
//6th Repeat for all objects Types
switch (type)
{
case Flags:
OutFile << left << "Number Of Flags: " << setw(2) << to_string(Flags_num) << endl;
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j <= NumHorizontalCells - 1; j++)
{
if (Flag* Object = dynamic_cast<Flag*>(CellList[i][j]->GetGameObject()))
{
Object->Save(OutFile, file);
}
}
}
break;
case WaterPits:
OutFile << left << "Number Of WaterPits: " << setw(2) << to_string(WaterPits_num) << endl;
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j <= NumHorizontalCells - 1; j++)
{
if (WaterPit* Object = dynamic_cast<WaterPit*>(CellList[i][j]->GetGameObject()))
{
Object->Save(OutFile, file);
}
}
}
break;
case DangerZones:
OutFile << left << "Number Of DangerZones: " << setw(2) << to_string(DangerZones_num) << endl;
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j <= NumHorizontalCells - 1; j++)
{
if (DangerZone* Object = dynamic_cast<DangerZone*>(CellList[i][j]->GetGameObject()))
{
Object->Save(OutFile, file);
}
}
}
break;
case Belts:
OutFile << left << "Number Of Belts: " << setw(2) << to_string(Belt_num) << endl;
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j <= NumHorizontalCells - 1; j++)
{
if (Belt* Object = dynamic_cast<Belt*>(CellList[i][j]->GetGameObject()))
{
Object->Save(OutFile, file);
}
}
}
break;
case Workshops:
OutFile << left << "Number Of Workshops: " << setw(2) << to_string(Workshops_num) << endl;
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j <= NumHorizontalCells - 1; j++)
{
if (Workshop* Object = dynamic_cast<Workshop*>(CellList[i][j]->GetGameObject()))
{
Object->Save(OutFile, file);
}
}
}
break;
case Antennas:
OutFile << left << "Number Of Antennas: " << setw(2) << to_string(Antennas_num) << endl;
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j <= NumHorizontalCells - 1; j++)
{
if (Antenna* Object = dynamic_cast<Antenna*>(CellList[i][j]->GetGameObject()))
{
Object->Save(OutFile, file);
}
}
}
break;
case RotatingGears:
OutFile << left << "Number Of RotatingGears: " << setw(2) << to_string(RotatingGears_num) << endl;
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j <= NumHorizontalCells - 1; j++)
{
if (RotatingGear* Object = dynamic_cast<RotatingGear*>(CellList[i][j]->GetGameObject()))
{
Object->Save(OutFile, file);
}
}
}
break;
}
}
void Grid::LoadAll(ifstream& Infile, string file, Type type)
{
int objectCount; //Counter for GAmeObhects
string line;
getline(Infile, line); //Read line until /n
objectCount = stoi(line.substr(line.find(":") + 1)); //stores the number of objects
//1st step for loop for the object count
//2nd step make a cellpotion object with default constructor
//3th step make a new object
//4th step load the data
//5th step add the object to the grid
//6th step if the object count not equale zero skip the /n and get to the next line
//Repeat According to the type of the object and its valditations
switch (type)
{
case Flags:
for (int i = 0; i < objectCount; i++)
{
CellPosition pos; //cellpostion object with defualt constructor
Flag* newFlag = new Flag(pos); //new flag
newFlag->Load(Infile, file); //loading the data
AddObjectToCell(newFlag); //add the object to the grid
}
if (objectCount != 0)
{
getline(Infile, line); //skips /n to get to the next line
}
break;
case WaterPits:
for (int i = 0; i < objectCount; i++)
{
CellPosition pos;
WaterPit* newWaterPit = new WaterPit(pos);
newWaterPit->Load(Infile, file);
AddObjectToCell(newWaterPit);
}
if (objectCount != 0)
{
getline(Infile, line);
}
break;
case DangerZones:
for (int i = 0; i < objectCount; i++)
{
CellPosition pos;
DangerZone* newDangerZone = new DangerZone(pos);
newDangerZone->Load(Infile, file);
AddObjectToCell(newDangerZone);
}
if (objectCount != 0)
{
getline(Infile, line);
}
break;
case Belts:
for (int i = 0; i < objectCount; i++)
{
CellPosition spos,epos;
Belt* newBelt = new Belt(spos,epos);
newBelt->Load(Infile, file);
AddObjectToCell(newBelt);
}
if (objectCount != 0)
{
getline(Infile, line);
}
break;
case Workshops:
for (int i = 0; i < objectCount; i++)
{
CellPosition pos;
Workshop* newWorkshop = new Workshop(pos);
newWorkshop->Load(Infile, file);
AddObjectToCell(newWorkshop);
}
if (objectCount != 0)
{
getline(Infile, line);
}
break;
case Antennas:
for (int i = 0; i < objectCount; i++)
{
CellPosition pos;
Antenna* newAntenna = new Antenna(pos);
newAntenna->Load(Infile, file);
AddObjectToCell(newAntenna);
}
if (objectCount != 0)
{
getline(Infile, line);
}
break;
case RotatingGears:
for (int i = 0; i < objectCount; i++)
{
CellPosition pos;
int dierc = 0;
RotatingGear* newRotatingGear = new RotatingGear(pos,dierc);
newRotatingGear->Load(Infile, file);
AddObjectToCell(newRotatingGear);
}
if (objectCount != 0)
{
getline(Infile, line);
}
break;
}
}
void Grid::removeGrid()
{
for (int i = NumVerticalCells - 1; i >= 0; i--)
{
for (int j = 0; j <= NumHorizontalCells - 1; j++)
{
CellPosition pos(i, j);
RemoveObjectFromCell(pos); // Remove object from the cell
}
}
}
void Grid::removeClipBoard()
{
Clipboard = NULL;
}
// ========= Other Getters =========
Player * Grid::GetCurrentPlayer() const
{
return PlayerList[currPlayerNumber];
}
Belt * Grid::GetNextBelt(const CellPosition & position)
{
int startH = position.HCell(); // represents the start hCell in the current row to search for the belt 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
{
///TODO: Check if CellList[i][j] has a belt, 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
}
// ========= User Interface Functions =========
void Grid::UpdateInterface() const
{
if (UI.InterfaceMode == MODE_DESIGN)
{
// 1- Draw cells with or without waterpits or dangerzone
for (int i = NumVerticalCells-1; i >= 0 ; i--) // bottom up
{
for (int j = 0; j < NumHorizontalCells; j++) // left to right
{
CellList[i][j]->DrawCellOrWaterPitOrDangerZone(pOut);
}
}
// 2- Draw other game objects(excluding waterpit and dangerzone)
for (int i = NumVerticalCells-1; i >= 0 ; i--) // bottom up
{
for (int j = 0; j < NumHorizontalCells; j++) // left to right
{
CellList[i][j]->DrawGameObject(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, cgame objects 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();
}
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];
}
}
void Grid::setcommand(Command* x, int size) {
themovingcommands = x;
themovingcommandsnum = size;
}
Command* Grid::getcommands() {
return themovingcommands;
}
void Grid::setrandomcommands(Command* x, int size) {
themovingcommands = x;
themovingcommandsnum = size;
}
int Grid::getsize() {
return themovingcommandsnum;
}