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
|
#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
|