Skip to content

Commit 9f58313

Browse files
can only place one start and destination cell
1 parent ce8fa1c commit 9f58313

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

Grid.hpp

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include <SFML/Graphics.hpp>
55
#include "GridCellStates.hpp"
66
#include "math.h"
7+
#include <stdio.h>
78

9+
using namespace std;
810
using namespace sf;
911

1012
class Grid
@@ -16,6 +18,9 @@ class Grid
1618
int cellSize; // in pixels
1719
int outlineThickness; // in pixels
1820

21+
Vector2i *startPos;
22+
Vector2i *destPos;
23+
1924
public:
2025
Grid(int width, int height, int cellSize);
2126

@@ -87,6 +92,9 @@ Grid::Grid(int width, int height, int cellSize)
8792
gridHeight = height;
8893
this->cellSize = cellSize;
8994
outlineThickness = 1;
95+
96+
startPos = NULL;
97+
destPos = NULL;
9098
}
9199

92100
/// <summary>
@@ -110,6 +118,9 @@ Grid::Grid(int width, int height, int cellSize, int outlineThickness)
110118
gridHeight = height;
111119
this->cellSize = cellSize;
112120
this->outlineThickness = outlineThickness;
121+
122+
startPos = NULL;
123+
destPos = NULL;
113124
}
114125

115126
/// <summary>
@@ -196,7 +207,62 @@ bool Grid::setValAt(int x, int y, GridValue val)
196207
{
197208
if (validCoords(x, y))
198209
{
210+
// only allow one start and dest cell
211+
if (val == GridValue::START)
212+
{
213+
// set old start to unoccupied
214+
if (startPos == NULL)
215+
{
216+
startPos = new Vector2i(x, y);
217+
}
218+
else
219+
{
220+
// unoccupy old start pos
221+
gridArr[startPos->x][startPos->y] = GridValue::UNOCCUPIED;
222+
223+
// set new start pos
224+
startPos->x = x;
225+
startPos->y = y;
226+
}
227+
}
228+
else if (val == GridValue::DESTINATION)
229+
{
230+
// set old dest to unoccupied
231+
if (destPos == NULL)
232+
{
233+
destPos = new Vector2i(x, y);
234+
}
235+
else
236+
{
237+
// unoccupy old dest pos
238+
gridArr[destPos->x][destPos->y] = GridValue::UNOCCUPIED;
239+
240+
// set new dest pos
241+
destPos->x = x;
242+
destPos->y = y;
243+
}
244+
}
245+
else
246+
{
247+
// make sure that dest/start are removed if the cell is overwritten
248+
GridValue currVal = getValueAt(x, y);
249+
if (startPos != NULL && currVal == GridValue::START)
250+
{
251+
// remove start pos
252+
delete(startPos);
253+
startPos = NULL;
254+
}
255+
else if (destPos != NULL && currVal == GridValue::DESTINATION)
256+
{
257+
// remove destination pos
258+
delete(destPos);
259+
destPos = NULL;
260+
}
261+
}
262+
263+
// set the value at the cell
199264
gridArr[x][y] = val;
265+
200266
return true;
201267
}
202268

@@ -213,25 +279,34 @@ bool Grid::setValAt(int x, int y, GridValue val)
213279
bool Grid::setValAt(Vector2i pos, GridValue val)
214280
{
215281
Vector2i gridCoords(screenToGrid(pos));
216-
if (validCoords(gridCoords.x, gridCoords.y))
217-
{
218-
gridArr[gridCoords.x][gridCoords.y] = val;
219-
return true;
220-
}
221-
return false;
282+
return setValAt(gridCoords.x, gridCoords.y, val);
222283
}
223284

224285
/// <summary>
225286
/// Destructor for a grid object
226287
/// </summary>
227288
Grid::~Grid()
228289
{
290+
cout << "startPos == null: " << (startPos == NULL) << endl;
291+
cout << "destPos == null: " << (destPos == NULL) << endl;
292+
293+
// free the grid array
229294
for (int i = 0; i < gridWidth; i++)
230295
{
231296
delete gridArr[i];
232297
}
233298

234299
delete gridArr;
300+
301+
// free the start and dest positions
302+
if (startPos != NULL)
303+
{
304+
delete(startPos);
305+
}
306+
if (destPos != NULL)
307+
{
308+
delete(destPos);
309+
}
235310
}
236311

237312
#endif

Main.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Grid *grid;
1414

1515
Vector2f selectedGridPos = Vector2f(0, 0);
1616

17+
/// <summary>
18+
/// Handle the logic for the player cursor on the grid
19+
/// </summary>
1720
void playerCursor()
1821
{
1922
// get mouse position
@@ -28,6 +31,9 @@ void playerCursor()
2831
window->draw(selectedSquare);
2932
}
3033

34+
/// <summary>
35+
/// Handle the player's ability to color the grid
36+
/// </summary>
3137
void playerController()
3238
{
3339
// get mouse position
@@ -54,8 +60,7 @@ void playerController()
5460
// place destination cell
5561
grid->setValAt(mousePos, GridValue::DESTINATION);
5662
}
57-
else if (Mouse::isButtonPressed(Mouse::Button::Middle)
58-
|| Keyboard::isKeyPressed(Keyboard::Key::Space))
63+
else if (Mouse::isButtonPressed(Mouse::Button::Middle) || Keyboard::isKeyPressed(Keyboard::Key::Space))
5964
{
6065
// middle mouse button
6166
// place starting cell
@@ -98,4 +103,6 @@ int main()
98103
// display window
99104
window->display();
100105
}
106+
107+
delete(grid);
101108
}

0 commit comments

Comments
 (0)