-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.hpp
155 lines (140 loc) · 4.7 KB
/
Maze.hpp
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
149
150
151
152
153
154
155
/**
* @file Maze.hpp
* @brief Class definition for Maze, representing a maze with cells and walls
* @date Created on 24-12-2023.
* @author Renato Chavez
*/
#ifndef ALGOVISUALIZER_MAZE_HPP
#define ALGOVISUALIZER_MAZE_HPP
/**
* Standard library vector for dynamic array management.
*/
#include <vector>
/**
* Standard library cstdlib for general purpose functions.
*/
#include <cstdlib>
#include <cryptopp/osrng.h>
#include <sodium.h>
#include <array>
/**
* @brief Represents a maze with cells and walls.
*
* The Maze class stores a maze grid where each cell can have walls on four sides.
* It provides functionality to generate and access the maze structure.
*/
#include "IRenderable.hpp"
#include <random>
class Maze : public IRenderable{
public:
/**
* @brief Represents a single cell in the maze.
*
* Each cell has four walls and a visited flag, which can be used for maze generation algorithms.
*/
std::pair<int, int> farthestPoint_;
bool farthestPointSet_;
struct Point {
int row;
int col;
Point(int x , int y) : row(x), col(y) {}
bool operator ==(const Point& other) const{
return row == other.row && col == other.col;
}
Point operator+(const Point& other) const {
return {row + other.row, col + other.col};
}
bool operator<(const Point& other) const {
return std::tie(row, col) < std::tie(other.row, other.col);
}
};
std::vector<Point> path_;
static std::array<Point, 4> directions;
struct Cell {
bool visited;
bool topWall;
bool leftWall;
bool bottomWall;
bool rightWall;
bool isBlocked(const Point& direction) const {
if(direction.row == -1 && topWall) return true;
if(direction.row == 1 && bottomWall) return true;
if(direction.col == -1 && leftWall) return true;
if(direction.col == 1 && rightWall) return true;
return false;
}
};
/**
* @brief Constructs a Maze object with specified dimensions.
* @param rows Number of rows in the maze.
* @param cols Number of columns in the maze.
*/
Maze(int rows, int cols);
/**
* @brief Generates the maze structure.
*
* This method uses a maze generation algorithm to create a maze with one possible path.
*/
void generateMaze();
[[nodiscard]] int getRows() const { return rows_; }
[[nodiscard]] int getCols() const { return cols_; }
[[nodiscard]] const std::vector<std::vector<Cell>>& getMaze() const;
void update() override{}
void render(SDL_Renderer* renderer) override;
void setScreenDimensions(int screenWidth, int screenHeight);
void handleMouseClick(Sint32 mouseX, Sint32 mouseY, SDL_Window* sdlWindow);
[[nodiscard]] std::pair<int, int> getStartPosition() const {
return startPosition_;
}
void findShortestPath(Point startPoint, Point endPoint);
bool isValid(const Point& p) const {
return p.row >= 0 && p.row < rows_ && p.col >= 0 && p.col < cols_;
}
bool isWall(const Point& current, const Point& direction) const {
return maze_[static_cast<unsigned long>(current.row)][static_cast<unsigned long>(current.col)].isBlocked(direction);
}
void traceBackPath(const std::map<Point, Point>& predecessor, const Point& start, const Point& end);
private:
/**
* @brief Initializes the maze grid.
*
* Sets up the grid with specified dimensions and initializes cells.
*/
void initializeMaze();
/**
* @brief Recursive function to generate the maze.
*
* @param r Row index for the current cell.
* @param c Column index for the current cell.
*/
void generateMazeRecursive(std::size_t r, std::size_t c);
/**
* @brief 2D grid representing the maze.
* A vector of vectorof cells, where each cell represents a position in the maze.
*/
std::vector<std::vector<Cell>> maze_;
/**
* @brief Number of rows in the maze.
*/
int rows_;
/**
* @brief Number of columns in the maze.
*/
int cols_;
int windowWidth_;
int windowHeight_;
int wallThickness_;
std::pair<int, int> startPosition_;
bool startPositionSet_;
void drawCell(std::size_t row, std::size_t col, int startX, int startY, int cellWidth, int cellHeight, int wallThickness, SDL_Renderer* sdlRenderer);
class SodiumRandom {
public:
using result_type = uint32_t;
static constexpr result_type min() { return 0; }
static constexpr result_type max() { return UINT32_MAX; }
result_type operator()() {
return randombytes_random();
}
};
};
#endif //ALGOVISUALIZER_MAZE_HPP