Maze Builder Docs 6.3.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>
6
7#include <atomic>
8#include <cstdint>
9#include <functional>
10#include <memory>
11#include <mutex>
12#include <optional>
13#include <string>
14#include <tuple>
15#include <unordered_map>
16#include <vector>
17
18namespace mazes {
19
23class grid : public grid_interface {
24
26 friend class binary_tree;
27 friend class dfs;
28 friend class sidewinder;
29
30public:
31
36 explicit grid(unsigned int r = 1u, unsigned int c = 1u, unsigned int l = 1u);
37
40 explicit grid(std::tuple<unsigned int, unsigned int, unsigned int> dimens);
41
44 grid(const grid& other);
45
49 grid& operator=(const grid& other);
50
53 grid(grid&& other) noexcept;
54
58 grid& operator=(grid&& other) noexcept;
59
61 ~grid() override;
62
66 virtual void configure(const std::vector<int>& indices) noexcept;
67
72 std::shared_ptr<cell> get_neighbor(std::shared_ptr<cell> const& c, Direction dir) const noexcept;
73
77 std::vector<std::shared_ptr<cell>> get_neighbors(std::shared_ptr<cell> const& c) const noexcept;
78
79 void set_neighbor(const std::shared_ptr<cell>& c, Direction dir, std::shared_ptr<cell> const& neighbor) noexcept;
80
81 // Convenience methods for accessing neighbors
82 std::shared_ptr<cell> get_north(const std::shared_ptr<cell>& c) const noexcept override;
83
84 std::shared_ptr<cell> get_south(const std::shared_ptr<cell>& c) const noexcept override;
85
86 std::shared_ptr<cell> get_east(const std::shared_ptr<cell>& c) const noexcept override;
87
88 std::shared_ptr<cell> get_west(const std::shared_ptr<cell>& c) const noexcept override;
89
92 virtual std::tuple<unsigned int, unsigned int, unsigned int> get_dimensions() const noexcept override;
93
96 virtual void to_vec(std::vector<std::shared_ptr<cell>>& cells) const noexcept override;
97
101 virtual std::string contents_of(std::shared_ptr<cell>const & c) const noexcept override;
102
106 virtual std::uint32_t background_color_for(std::shared_ptr<cell> const& c) const noexcept override;
107
111 std::shared_ptr<cell> search(int index) const noexcept;
112
115 int num_cells() const noexcept;
116
118 void clear_cells() noexcept;
119private:
122 void configure_cells(std::vector<std::shared_ptr<cell>>& cells) noexcept;
123
125 std::function<int(unsigned int, unsigned int)> m_calculate_cell_index;
126
127 // Store cells by index
128 std::unordered_map<int, std::shared_ptr<cell>> m_cells;
129 std::tuple<unsigned int, unsigned int, unsigned int> m_dimensions;
130
131 // Store topology - which cell is neighbor to which in what direction
132 // Key: cell index, Value: map of direction to neighbor cell index
133 mutable std::mutex m_topology_mutex;
134 std::unordered_map<int, std::unordered_map<Direction, int>> m_topology;
135
136 std::atomic<bool> m_configured;
137}; // class
138
139} // namespace mazes
140
141#endif // GRID_H
Binary tree algorithm for generating mazes.
Definition binary_tree.h:17
Cell class for maze generation - only stores its index and links to other cells.
Definition cell.h:17
Depth-first search algorithm for generating mazes.
Definition dfs.h:23
Interface for the grid class.
Definition grid_interface.h:26
General purpose grid class for 2D maze generation.
Definition grid.h:23
std::vector< std::shared_ptr< cell > > get_neighbors(std::shared_ptr< cell > const &c) const noexcept
Get all the neighbors by the cell.
virtual void configure(const std::vector< int > &indices) noexcept
Configure the grid's cells' neighbors.
grid(const grid &other)
Copy constructor.
virtual std::tuple< unsigned int, unsigned int, unsigned int > get_dimensions() const noexcept override
Provides dimensions of grid in no assumed ordering.
virtual void to_vec(std::vector< std::shared_ptr< cell > > &cells) const noexcept override
Convert a 2D grid to a vector of cells (sorted by row then column)
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.
grid(unsigned int r=1u, unsigned int c=1u, unsigned int l=1u)
grid & operator=(const grid &other)
Assignment operator.
void clear_cells() noexcept
Cleanup cells by cleaning up links within cells.
grid & operator=(grid &&other) noexcept
Move assignment operator.
int num_cells() const noexcept
Get the count of cells in the grid.
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.
std::shared_ptr< cell > get_neighbor(std::shared_ptr< cell > const &c, Direction dir) const noexcept
Get neighbor by the cell's respective location.
std::shared_ptr< cell > search(int index) const noexcept
~grid() override
Destructor.
Sidewinder algorithm for generating mazes.
Definition sidewinder.h:17
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