Skip to content

Commit c6b3d01

Browse files
refactored my code. Cleaned up the grid class
1 parent 9f58313 commit c6b3d01

6 files changed

+324
-160
lines changed

Grid.hpp

+65-152
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,67 @@
99
using namespace std;
1010
using namespace sf;
1111

12+
template <typename T>
1213
class Grid
1314
{
1415
private:
15-
GridValue **gridArr; // the grid
16+
T ***gridArr; // the grid
1617
int gridWidth; // the width of the grid
1718
int gridHeight; // the height of the grid
1819
int cellSize; // in pixels
19-
int outlineThickness; // in pixels
20-
21-
Vector2i *startPos;
22-
Vector2i *destPos;
2320

2421
public:
2522
Grid(int width, int height, int cellSize);
2623

27-
Grid(int width, int height, int cellSize, int outlineThickness);
28-
29-
GridValue getValueAt(int x, int y);
24+
T *getValueAt(int x, int y);
3025

3126
Vector2f gridToScreen(int x, int y);
3227

3328
Vector2i screenToGrid(Vector2i pos);
3429

3530
Vector2f centerScreenCoord(Vector2i pos);
3631

37-
void drawGrid(RenderWindow *window);
32+
bool setValAt(int x, int y, T *val);
3833

39-
bool setValAt(int x, int y, GridValue val);
40-
41-
bool setValAt(Vector2i pos, GridValue val);
34+
bool setValAt(Vector2i pos, T *val);
4235

4336
int getCellSize();
44-
45-
~Grid();
4637

47-
private:
38+
int getGridWidth();
39+
40+
int getGridHeight();
41+
4842
bool validCoords(int x, int y);
43+
44+
~Grid();
4945
};
5046

47+
/// <summary>
48+
/// Get the grid's height
49+
/// </summary>
50+
/// <returns>the grid's height</returns>
51+
template <typename T>
52+
int Grid<T>::getGridHeight()
53+
{
54+
return gridHeight;
55+
}
56+
57+
/// <summary>
58+
/// Get the grid's width
59+
/// </summary>
60+
/// <returns>the grid's width</returns>
61+
template <typename T>
62+
int Grid<T>::getGridWidth()
63+
{
64+
return gridWidth;
65+
}
66+
5167
/// <summary>
5268
/// Get the cell size
5369
/// </summary>
5470
/// <returns>the cell size in pixels</returns>
55-
int Grid::getCellSize()
71+
template <typename T>
72+
int Grid<T>::getCellSize()
5673
{
5774
return cellSize;
5875
}
@@ -64,7 +81,8 @@ int Grid::getCellSize()
6481
/// <param name="pos">A position on the screen</param>
6582
/// <returns>the center screen position of the cell closest to the givest
6683
/// screen position</returns>
67-
Vector2f Grid::centerScreenCoord(Vector2i pos)
84+
template <typename T>
85+
Vector2f Grid<T>::centerScreenCoord(Vector2i pos)
6886
{
6987
int posX = (int)floor(pos.x / cellSize) * cellSize;
7088
int posY = (int)floor(pos.y / cellSize) * cellSize;
@@ -77,66 +95,39 @@ Vector2f Grid::centerScreenCoord(Vector2i pos)
7795
/// <param name="width">the width of the grid</param>
7896
/// <param name="height">the height of the grid</param>
7997
/// <param name="cellSize">the size of each grid square in pixels</param>
80-
Grid::Grid(int width, int height, int cellSize)
98+
template <typename T>
99+
Grid<T>::Grid(int width, int height, int cellSize)
81100
{
82101
this->cellSize = cellSize;
83102
// initialize grid
84-
gridArr = new GridValue *[width];
85-
for (int i = 0; i < width; i++)
103+
gridArr = new T **[width];
104+
for (int x = 0; x < width; x++)
86105
{
87-
gridArr[i] = new GridValue[height];
106+
gridArr[x] = new T *[height];
88107
}
89108

90109
// initialize instance variables
91110
gridWidth = width;
92111
gridHeight = height;
93112
this->cellSize = cellSize;
94-
outlineThickness = 1;
95-
96-
startPos = NULL;
97-
destPos = NULL;
98-
}
99-
100-
/// <summary>
101-
/// Create an instance of a grid
102-
/// </summary>
103-
/// <param name="width">the width of the grid</param>
104-
/// <param name="height">the height of the grid</param>
105-
/// <param name="cellSize">the size of each grid square in pixels</param>
106-
Grid::Grid(int width, int height, int cellSize, int outlineThickness)
107-
{
108-
this->cellSize = cellSize;
109-
// initialize grid
110-
gridArr = new GridValue* [width];
111-
for (int i = 0; i < width; i++)
112-
{
113-
gridArr[i] = new GridValue[height];
114-
}
115-
116-
// initialize instance variables
117-
gridWidth = width;
118-
gridHeight = height;
119-
this->cellSize = cellSize;
120-
this->outlineThickness = outlineThickness;
121-
122-
startPos = NULL;
123-
destPos = NULL;
124113
}
125114

126115
/// <summary>
127116
/// Get the value at the given grid coordinates
128117
/// </summary>
129118
/// <param name="x">the row cooridnate</param>
130119
/// <param name="y">the column coordinate</param>
131-
/// <returns>the value at the given grid coordinates</returns>
132-
GridValue Grid::getValueAt(int x, int y)
120+
/// <returns>if the coordinates are valid, returns the value at the given grid
121+
/// coordinates, and returns false otherwise</returns>
122+
template <typename T>
123+
T *Grid<T>::getValueAt(int x, int y)
133124
{
134125
if (x < gridWidth && y < gridHeight)
135126
{
136127
return gridArr[x][y];
137128
}
138129

139-
return GridValue::INVALID;
130+
return NULL;
140131
}
141132

142133
/// <summary>
@@ -146,15 +137,23 @@ GridValue Grid::getValueAt(int x, int y)
146137
/// <param name="y">the y coordinate of the dedired grid space</param>
147138
/// <returns>a screen position in pixels as a Vector2f of the given cell
148139
/// in the grid if the given coordinates are valid, otherwise returns NULL</returns>
149-
Vector2f Grid::gridToScreen(int x, int y)
140+
template <typename T>
141+
Vector2f Grid<T>::gridToScreen(int x, int y)
150142
{
151143
float screenX = x * cellSize;
152144
float screenY = y * cellSize;
153145

154146
return Vector2f(screenX, screenY);
155147
}
156148

157-
Vector2i Grid::screenToGrid(Vector2i pos)
149+
150+
/// <summary>
151+
/// Convert the given screen position to a grid position
152+
/// </summary>
153+
/// <param name="pos">The screen position to be converted</param>
154+
/// <returns>The screen position as a grid position</returns>
155+
template <typename T>
156+
Vector2i Grid<T>::screenToGrid(Vector2i pos)
158157
{
159158
int xPos = (int)floor(pos.x / cellSize);
160159
int yPos = (int)floor(pos.y / cellSize);
@@ -167,99 +166,24 @@ Vector2i Grid::screenToGrid(Vector2i pos)
167166
/// <param name="x">the x coordinate of the desired cell</param>
168167
/// <param name="y"the y coordinate of the desired cell></param>
169168
/// <returns>true if the cell is valid and false otherwise</returns>
170-
bool Grid::validCoords(int x, int y)
169+
template <typename T>
170+
bool Grid<T>::validCoords(int x, int y)
171171
{
172172
return (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight);
173173
}
174174

175-
/// <summary>
176-
/// Draws this grid in the given window
177-
/// </summary>
178-
/// <param name="window">window to draw into</param>
179-
void Grid::drawGrid(RenderWindow *window)
180-
{
181-
for (int x = 0; x < gridWidth; x++)
182-
{
183-
for (int y = 0; y < gridHeight; y++)
184-
{
185-
RectangleShape cell = RectangleShape(Vector2f(cellSize, cellSize));
186-
Vector2f pos = gridToScreen(x, y);
187-
cell.setPosition(pos);
188-
cell.setOutlineThickness(outlineThickness);
189-
190-
GridStateColor color = valToColor(gridArr[x][y]);
191-
cell.setFillColor(Color((unsigned long)color));
192-
cell.setOutlineColor(Color::White);
193-
194-
window->draw(cell);
195-
}
196-
}
197-
}
198-
199175
/// <summary>
200176
/// Set the value of the cell at the given coordinates
201177
/// </summary>
202178
/// <param name="x">the x coordinate of the cell</param>
203179
/// <param name="y">the y coordinate of the cell</param>
204180
/// <param name="val">the new value of the given cell</param>
205181
/// <returns>true if the cell is valid and false otherwise</returns>
206-
bool Grid::setValAt(int x, int y, GridValue val)
182+
template <typename T>
183+
bool Grid<T>::setValAt(int x, int y, T *val)
207184
{
208185
if (validCoords(x, y))
209186
{
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-
263187
// set the value at the cell
264188
gridArr[x][y] = val;
265189

@@ -276,7 +200,8 @@ bool Grid::setValAt(int x, int y, GridValue val)
276200
/// <param name="pos">the screen position of the cell</param>
277201
/// <param name="val">the new value of the cell</param>
278202
/// <returns>true if the cell is valid and false otherwise</returns>
279-
bool Grid::setValAt(Vector2i pos, GridValue val)
203+
template <typename T>
204+
bool Grid<T>::setValAt(Vector2i pos, T *val)
280205
{
281206
Vector2i gridCoords(screenToGrid(pos));
282207
return setValAt(gridCoords.x, gridCoords.y, val);
@@ -285,28 +210,16 @@ bool Grid::setValAt(Vector2i pos, GridValue val)
285210
/// <summary>
286211
/// Destructor for a grid object
287212
/// </summary>
288-
Grid::~Grid()
213+
template <typename T>
214+
Grid<T>::~Grid()
289215
{
290-
cout << "startPos == null: " << (startPos == NULL) << endl;
291-
cout << "destPos == null: " << (destPos == NULL) << endl;
292-
293216
// free the grid array
294-
for (int i = 0; i < gridWidth; i++)
217+
for (int x = 0; x < gridWidth; x++)
295218
{
296-
delete gridArr[i];
219+
delete gridArr[x];
297220
}
298221

299222
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-
}
310223
}
311224

312225
#endif

GridCellStates.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
enum class GridValue
77
{
88
INVALID = -1,
9-
OCCUPIED,
109
UNOCCUPIED,
10+
OCCUPIED,
1111
SELECTED,
1212
DESTINATION,
1313
START

0 commit comments

Comments
 (0)