Maze Builder Docs 6.7.5
Loading...
Searching...
No Matches
grid.h
Go to the documentation of this file.
1#ifndef GRID_H
2#define GRID_H
3
4#include <MazeBuilder/enums.h>
7
8#include <atomic>
9#include <cstdint>
10#include <functional>
11#include <memory>
12#include <mutex>
13#include <optional>
14#include <string>
15#include <tuple>
16#include <unordered_map>
17#include <vector>
18
19namespace mazes {
20
21class cell;
22
26class grid : public grid_interface, public grid_operations {
27
29 friend class binary_tree;
30 friend class dfs;
31 friend class sidewinder;
32
33public:
34
39 explicit grid(unsigned int r = 1u, unsigned int c = 1u, unsigned int l = 1u);
40
43 explicit grid(std::tuple<unsigned int, unsigned int, unsigned int> dimens);
44
47 grid(const grid& other);
48
52 grid& operator=(const grid& other);
53
56 grid(grid&& other) noexcept;
57
61 grid& operator=(grid&& other) noexcept;
62
64 ~grid() override;
65
69 virtual std::string contents_of(std::shared_ptr<cell>const & c) const noexcept override;
70
74 virtual std::uint32_t background_color_for(std::shared_ptr<cell> const& c) const noexcept override;
75
78 grid_operations& operations() noexcept override;
79
82 const grid_operations& operations() const noexcept override;
83
84 std::tuple<unsigned int, unsigned int, unsigned int> get_dimensions() const noexcept override;
85
90 virtual std::shared_ptr<cell> get_neighbor(std::shared_ptr<cell> const& c, Direction dir) const noexcept override;
91
95 virtual std::vector<std::shared_ptr<cell>> get_neighbors(std::shared_ptr<cell> const& c) const noexcept override;
96
102 virtual void set_neighbor(const std::shared_ptr<cell>& c, Direction dir, std::shared_ptr<cell> const& neighbor) noexcept override;
103
106 void sort(std::vector<std::shared_ptr<cell>>& cells) const noexcept override;
107
108 // Convenience methods for accessing neighbors
109 virtual std::shared_ptr<cell> get_north(const std::shared_ptr<cell>& c) const noexcept override;
110 virtual std::shared_ptr<cell> get_south(const std::shared_ptr<cell>& c) const noexcept override;
111 virtual std::shared_ptr<cell> get_east(const std::shared_ptr<cell>& c) const noexcept override;
112 virtual std::shared_ptr<cell> get_west(const std::shared_ptr<cell>& c) const noexcept override;
113
117 virtual std::shared_ptr<cell> search(int index) const noexcept override;
118
119 virtual std::vector<std::shared_ptr<cell>> get_cells() const noexcept override;
120
123 virtual int num_cells() const noexcept override;
124
126 virtual void clear_cells() noexcept override;
127
131 virtual bool set_cells(const std::vector<std::shared_ptr<cell>>& cells) noexcept override;
132
133 virtual void set_str(std::string const& str) noexcept override;
134
135 virtual std::string get_str() const noexcept override;
136
137private:
138
140 std::function<int(unsigned int, unsigned int)> m_calculate_cell_index;
141
142 std::unordered_map<int, std::shared_ptr<cell>> m_cells;
143
144 std::tuple<unsigned int, unsigned int, unsigned int> m_dimensions;
145
146 // Store topology - which cell is neighbor to which in what direction
147 // Key: cell index, Value: map of direction to neighbor cell index
148 mutable std::mutex m_topology_mutex;
149 std::unordered_map<int, std::unordered_map<Direction, int>> m_topology;
150
151 std::atomic<bool> m_configured;
152
153 std::string m_str;
154}; // class
155
156} // namespace mazes
157
158#endif // GRID_H
Binary tree algorithm for generating mazes.
Definition binary_tree.h:16
Cell class with links to other cells.
Definition cell.h:17
Depth-first search algorithm for generating mazes.
Definition dfs.h:20
Interface for the grid class.
Definition grid_interface.h:18
Interface for grid navigation and manipulation operations.
Definition grid_operations.h:16
General purpose grid class for 2D maze generation.
Definition grid.h:26
grid_operations & operations() noexcept override
grid(const grid &other)
Copy constructor.
virtual std::shared_ptr< cell > search(int index) const noexcept override
Search for a cell by index.
virtual void clear_cells() noexcept override
Cleanup cells by cleaning up links within cells.
virtual std::uint32_t background_color_for(std::shared_ptr< cell > const &c) const noexcept override
Get the background color for a cell in the grid.
virtual bool set_cells(const std::vector< std::shared_ptr< cell > > &cells) noexcept override
Set cells and build topology from them.
virtual std::vector< std::shared_ptr< cell > > get_cells() const noexcept override
Retrieves a collection of cell objects.
grid(unsigned int r=1u, unsigned int c=1u, unsigned int l=1u)
grid & operator=(const grid &other)
Assignment operator.
grid & operator=(grid &&other) noexcept
Move assignment operator.
std::tuple< unsigned int, unsigned int, unsigned int > get_dimensions() const noexcept override
Retrieves the dimensions as a tuple of three unsigned integers.
virtual std::shared_ptr< cell > get_neighbor(std::shared_ptr< cell > const &c, Direction dir) const noexcept override
Get neighbor by the cell's respective location.
grid(std::tuple< unsigned int, unsigned int, unsigned int > dimens)
grid(grid &&other) noexcept
Move constructor.
virtual std::string contents_of(std::shared_ptr< cell >const &c) const noexcept override
Get detailed information of a cell in the grid.
void sort(std::vector< std::shared_ptr< cell > > &cells) const noexcept override
Transformation and display cells.
virtual int num_cells() const noexcept override
Get the count of cells in the grid.
~grid() override
Destructor.
virtual void set_neighbor(const std::shared_ptr< cell > &c, Direction dir, std::shared_ptr< cell > const &neighbor) noexcept override
Set neighbor for a cell in a given direction.
virtual std::vector< std::shared_ptr< cell > > get_neighbors(std::shared_ptr< cell > const &c) const noexcept override
Get all the neighbors by the cell.
Sidewinder algorithm for generating mazes.
Definition sidewinder.h:16
Enumerations for the maze builder program.
Namespace for the maze builder.
Definition algo_interface.h:9
Direction
Directional neighbors for grid topology.
Definition enums.h:118