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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#include "Maze.hpp"
#include <iostream>
#include <random>
#include <stack>
constexpr int MAX_NEIGHBOURS = 4;
namespace RandomCellNumGenerator
{
std::random_device rd;
std::default_random_engine randEngine(rd());
}
Maze::Maze(int totalRows, int totalColmuns): ROWS(totalRows), COLUMNS(totalColmuns)
{
cells.reserve(ROWS * COLUMNS);
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
cells.push_back(Cell(i, j));
}
}
}
void Maze::displayMaze() const
{
for (int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLUMNS; j++)
{
if (cells[i * COLUMNS + j].walls[0])
{
std::cout << "+---";
}
else
std::cout << "+ ";
}
std::cout << "+" << std::endl;
for (int j = 0; j < COLUMNS; j++)
{
if (cells[i * COLUMNS + j].walls[3])
{
std::cout << "| ";
}
else
std::cout << " ";
}
std::cout << "|" << std::endl;
}
for (int i = 0; i < COLUMNS; i++)
{
std::cout << "+---";
}
std::cout << "+" << std::endl;
}
void Maze::generateMaze()
{
std::uniform_int_distribution<int> distRow(0, ROWS - 1);
std::uniform_int_distribution<int> distColumn(0, COLUMNS - 1);
int startingRow = distRow(RandomCellNumGenerator::randEngine);
int startingColumn = distColumn(RandomCellNumGenerator::randEngine);
generateMaze(startingRow, startingColumn);
}
void Maze::generateMaze(int row, int column)
{
int currentNeighbour = getCellIndex(row, column);
std::stack<Cell> dfs;
dfs.push(cells[currentNeighbour]);
cells[currentNeighbour].setVisitedCell(true);
while (!dfs.empty())
{
int nextNeighbour = getNeighbourCell(row, column);
if (nextNeighbour != -1)
{
dfs.push(cells[nextNeighbour]);
cells[nextNeighbour].setVisitedCell(true);
removeWalls(currentNeighbour, nextNeighbour);
row = cells[nextNeighbour].getRowNumber();
column = cells[nextNeighbour].getColumnNumber();
currentNeighbour = nextNeighbour;
}
else
{
dfs.pop();
if (!dfs.empty())
{
row = dfs.top().getRowNumber();
column = dfs.top().getColumnNumber();
}
}
}
}
int Maze::getNeighbourCell(int row, int column)
{
int neighbours[MAX_NEIGHBOURS];
int validNeighbours = 0;
int top = getCellIndex(row - 1, column);
int right = getCellIndex(row, column + 1);
int bottom = getCellIndex(row + 1, column);
int left = getCellIndex(row, column - 1);
if (top != -1 && !cells[top].isCellVisited())
{
neighbours[validNeighbours] = top;
validNeighbours++;
}
if (right != -1 && !cells[right].isCellVisited())
{
neighbours[validNeighbours] = right;
validNeighbours++;
}
if (bottom != -1 && !cells[bottom].isCellVisited())
{
neighbours[validNeighbours] = bottom;
validNeighbours++;
}
if (left != -1 && !cells[left].isCellVisited())
{
neighbours[validNeighbours] = left;
validNeighbours++;
}
if (validNeighbours > 0)
{
std::uniform_int_distribution<int> distNeighbours(0, validNeighbours - 1);
int randomNeighbourCellIndex = distNeighbours(RandomCellNumGenerator::randEngine);
int nextNeighbourIndex = neighbours[randomNeighbourCellIndex];
return nextNeighbourIndex;
}
return -1;
}
int Maze::getCellIndex(int row, int column) const
{
if (row < 0 || column < 0 || row > ROWS - 1 || column > COLUMNS - 1)
return -1;
return (row * COLUMNS) + column;
}
void Maze::removeWalls(int currentNeighbour, int nextNeighbour)
{
int result = cells[currentNeighbour].getRowNumber() - cells[nextNeighbour].getRowNumber();
if (result != 0)
{
if (result == 1)
{
cells[currentNeighbour].walls[0] = false;
cells[nextNeighbour].walls[2] = false;
}
else if (result == -1)
{
cells[currentNeighbour].walls[2] = false;
cells[nextNeighbour].walls[0] = false;
}
}
else
{
result = cells[currentNeighbour].getColumnNumber() - cells[nextNeighbour].getColumnNumber();
if (result == 1)
{
cells[currentNeighbour].walls[3] = false;
cells[nextNeighbour].walls[1] = false;
}
else if (result == -1)
{
cells[currentNeighbour].walls[1] = false;
cells[nextNeighbour].walls[3] = false;
}
}
}
|