MiniGame-Madness
maze.h
Go to the documentation of this file.
1 #ifndef MAZE_H
2 #define MAZE_H
3 
10 #include "screenBuffer.h"
11 #include <map>
12 #include <vector>
13 #include <set>
14 #include <memory>
15 #include <cstdlib>
16 #include <ctime>
17 
18  // Forward declaration of the test class
19 namespace MazeTests {
20  class MazeTests;
21 }
22 
27 enum Direction {
30  EAST,
31  WEST,
32 };
33 
39 struct MazeNode {
40  std::map<Direction, MazeNode*> neighbors;
47  neighbors[NORTH] = nullptr;
48  neighbors[SOUTH] = nullptr;
49  neighbors[EAST] = nullptr;
50  neighbors[WEST] = nullptr;
51  }
52 
59  void addNeighbor(MazeNode* node, Direction direction) {
60  neighbors[direction] = node;
61  }
62 
68  void removeNeighbor(Direction direction) {
69  if (neighbors[direction] == nullptr) {
70  throw std::runtime_error("Neighbor not found in the given direction.");
71  }
72 
73  neighbors.erase(direction);
74  }
75 
81  MazeNode* getNeighbor(Direction direction) const {
82  auto neighbor = neighbors.find(direction);
83  return (neighbor != neighbors.end()) ? neighbor->second : nullptr;
84  }
85 
90  std::map<Direction, MazeNode*> getNeighbors() const {
91  return neighbors;
92  }
93 };
94 
95 
101 class Maze {
102  private:
103  // Declare the test class as a friend
104  friend class MazeTests::MazeTests;
105 
106  static const int OUT_OF_BOUNDS = -1;
107  ScreenBuffer screenBuffer;
109  int WIDTH;
110  int HEIGHT;
112  std::pair<int, int> start;
113  std::pair<int, int> end;
114  std::pair<int, int> playerPosition;
116  std::map<std::pair<int, int>, std::unique_ptr<MazeNode>> mazeMap;
117  std::set<std::pair<int, int>> inMaze;
124  bool isValidPosition(std::pair<int, int> position) const;
125 
131  Direction pickRandomDirection(std::vector<Direction>& directions);
132 
138  Direction getOppositeDirection(Direction direction) const;
139 
145  void linkNodes(std::pair<int, int> start, std::pair<int, int> end);
146 
153  std::vector<std::pair<int, int>> eraseLoop(std::vector<std::pair<int, int>> path, std::pair<int, int> current);
154 
160  std::pair<int, int> nextStep(std::pair<int, int> current);
161 
168  std::vector<std::pair<int, int>> randomWalk(std::pair<int, int> start);
169 
176  void generateMaze(int width, int height);
177 
181  void printMaze();
182 
187  void updateMaze(std::pair<int, int> playerPosition);
188 
193  std::pair<std::pair<int, int>, std::pair<int, int>> chooseStartAndEnd();
194 
199  bool checkWin() const;
200 
206  bool movePlayer(Direction direction);
207 
212  Direction getPlayerInput();
213 
214 public :
220  Maze(int width, int height);
221 
225  ~Maze() = default;
226 
232  int run();
233 };
234 
235 #endif // MAZE_H
MazeNode * getNeighbor(Direction direction) const
Get the neighboring node in a given direction.
Definition: maze.h:81
std::map< Direction, MazeNode * > getNeighbors() const
Get all neighboring nodes.
Definition: maze.h:90
std::map< Direction, MazeNode * > neighbors
Definition: maze.h:40
A class that represents the screen buffer.
Definition: screenBuffer.h:26
Definition: maze.h:28
Definition: maze.h:30
void removeNeighbor(Direction direction)
Remove a neighboring node based on a direction.
Definition: maze.h:68
Definition: maze.h:29
void addNeighbor(MazeNode *node, Direction direction)
Add a neighboring node with a direction.
Definition: maze.h:59
MazeNode()
Default constructor for MazeNode.
Definition: maze.h:46
A struct that represents the nodes in the graph of the maze.
Definition: maze.h:39
Definition: maze.h:19
Definition: maze.h:31
A class that represents the maze game.
Definition: maze.h:101