-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.hh
148 lines (103 loc) · 3.82 KB
/
maze.hh
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
#include <iostream>
using namespace std;
// A simple class for representing locations in a 2D array. The class also
// implements equality/inequality operators so that we can see if two
// locations are the same or not.
class Location {
public:
// The row and column of the location
int row;
int col;
// Constructors for initializing locations
Location(int row, int col) : row(row), col(col) { }
Location() : row(0), col(0) { }
// Returns true if this location is the same as the specified location
bool operator==(const Location &loc) const {
return row == loc.row && col == loc.col;
}
// Returns true if this location is different from the specified location
bool operator!=(const Location &loc) const {
return !(*this == loc);
}
};
// Possible values for maze cells
enum class MazeCell {
EMPTY,
WALL,
VISITED
};
// Directions that we can go, relative to a given maze cell
enum class Direction {
NORTH,
EAST,
SOUTH,
WEST
};
class Maze {
// The number of rows with cells in them
int numRows;
// The number of columns with cells in them
int numCols;
// The maze's "expanded representation"
MazeCell *cells;
// The start of the maze, in cell coordinates
Location start;
// The end of the maze, in cell coordinates
Location end;
// Take 2D expanded coordinates and compute the corresponding 1D
// array index
int getArrayIndex(const Location loc) const;
// Returns the expanded coordinates of the specified cell
// coordinates
Location getCellArrayCoord(int cellRow, int cellCol) const;
// Returns the expanded coordinates of the wall on a specific side of
// a cell given in cell coordinates
Location getWallArrayCoord(int cellRow, int cellCol,
Direction direction) const;
public:
// Initialize a new maze of size rows x cols
Maze(int rows, int cols);
// Make a copy of an existing maze object
Maze(const Maze &m);
// Maze destructor
~Maze();
// Maze assignment operator
Maze & operator=(const Maze &m);
// Returns the number of rows in the maze
int getNumRows() const;
// Returns the number of columns in the maze
int getNumCols() const;
// Returns the starting point in the maze
Location getStart() const;
// Sets the starting point in the maze
void setStart(int row, int col);
// Returns the ending point in the maze
Location getEnd() const;
// Sets the ending point in the maze
void setEnd(int row, int col);
// Sets all cells and walls to be empty, so that the maze is
// completely cleared
void clear();
// Places a wall at every location that can be a wall in the maze
void setAllWalls();
// Returns the value of the specified
MazeCell getCell(int cellRow, int cellCol) const;
void setCell(int cellRow, int cellCol, MazeCell val);
// Returns the cell-coordinates of the neighboring cell in the specified
// direction. Trips an assertion if the given cell has no neighbor in the
// specified direction (e.g. the NORTH neighbor of cell (0,5)).
Location getNeighborCell(int cellRow, int cellCol,
Direction direction) const;
// Returns true if there is a wall in the specified direction from the
// given cell, false otherwise
bool hasWall(int cellRow, int cellCol, Direction direction) const;
// Puts a wall on the specified side of the given cell
void setWall(int cellRow, int cellCol, Direction direction);
// Removes a wall on the specified side of the given cell
void clearWall(int cellRow, int cellCol, Direction direction);
// Returns true if the specified maze cell has been visited.
bool isVisited(int cellRow, int cellCol) const;
// Changes the cell's value to VISITED
void setVisited(int cellRow, int cellCol);
void print(ostream &os) const;
};