summaryrefslogtreecommitdiff
path: root/source/logic/Maze.h
diff options
context:
space:
mode:
Diffstat (limited to 'source/logic/Maze.h')
-rw-r--r--source/logic/Maze.h100
1 files changed, 100 insertions, 0 deletions
diff --git a/source/logic/Maze.h b/source/logic/Maze.h
new file mode 100644
index 0000000..245528d
--- /dev/null
+++ b/source/logic/Maze.h
@@ -0,0 +1,100 @@
+#ifndef MAZE_H
+#define MAZE_H
+
+#include <vector>
+#include <utility>
+using namespace std;
+
+/**
+* @brief A rectangular 2D boolean array, representing where cars can drive and where they cannot.
+*
+* @author Justin Wernick
+* @author David Schneider
+*/
+class Maze
+{
+ public:
+ /**
+ * @brief Defines the directions in which movement can happen in the maze.
+ */
+ enum Direction {UP, DOWN, LEFT, RIGHT};
+
+ /**
+ * @brief Creates an empty Maze with width and height of zero.
+ */
+ Maze();
+
+ //assignment and copy constructors have been left with the compiler generated versions
+
+ /**
+ * @brief Generates a new Maze from the vector of wall coordinates.
+ *
+ * The size of the Maze is chosen to just fit all of the walls. If objects exist outside
+ * of the walls, the x of the rightmost object and the y of the bottommost object can be
+ * passed in to make the Maze at least reach those coordinates.
+ *
+ * @param [in] walls A vector of x,y coordinate pairs representing the locations of each wall block.
+ * @param [in] maxObjectX The minimum x value that the Maze must be able to index.
+ * @param [in] maxObjectY The minimum y value that the Maze must be able to index.
+ */
+ void generateMaze(const vector<pair<int,int> >& walls, int maxObjectX=0, int maxObjectY=0);
+
+ /**
+ * @brief Checks if a given position contains a wall or not.
+ *
+ * This function is one of the most called as it is called for each block drawing the
+ * Maze on the Screen, by any Car checking if it can move, and by the EnemyCar to choose a
+ * viable direction to face. As such, it has been optimised by passing the parameters by
+ * constant reference, even though they are primitives. Further, the vector class's bounds
+ * checking is bypassed, with bounds checking performed manually with the assumption that the
+ * 2D vector is rectangular, to increase performance. Neither of these changes impare readability.
+ *
+ * @param [in] x The x coordinate being queried.
+ * @param [in] y The y coordinate being queried.
+ *
+ * @return True if the location contains a wall. Also returns true if the coordinate is outside of the Maze.
+ */
+ bool getSolid(const int& x, const int& y) const;
+
+ /**
+ * @brief Provides access to the width of the Maze object.
+ *
+ * @return The amount of blocks in each row of the maze.
+ */
+ int width() const;
+ /**
+ * @brief Provides access to the height of the Maze object.
+ *
+ * @return The amount of blocks in each column of the maze.
+ */
+ int height() const;
+
+ /**
+ * @brief Inverts a given direction, to give the value to face in the opposite direction.
+ *
+ * @param [in] forwards The direction to be inverted.
+ *
+ * @return The inverse of the given direction.
+ */
+ static Direction backwards(Direction forwards);
+
+ private:
+ /**
+ * @brief Provides an easier to read pseudonym for a 2 dimensional boolean vector.
+ */
+ typedef vector<vector<bool> > BoolGrid;
+
+ /**
+ * @brief The 2 dimensional vector that stores the locations of walls.
+ *
+ * The outer vector is columns, indexed with the x coordinate, and the inner vectors
+ * are the vertical positions in the column, indexed with the y coordinate.
+ * This results in a vector that is acced with _wallLocations.at(x).at(y).
+ */
+ BoolGrid _wallLocations;
+
+ int _width; ///< The number of blocks in each row.
+ int _height; ///< The number of blocks in each column.
+};
+
+#endif // MAZE_H