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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
//Maze using Recursive Backtracker Algorithm
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
/*
System to circumvent making classes:
Unvisited Cells: O (Open Cell)
Visited Cells: X (First visit)
Used Cells: U (Backtracked visit)
Destroyed Walls: D
In cell arrays 0 = x, 1 = y
*/
void outputMaze(string table[13][13]) { //output the maze
int x = 0;
int y = 0;
while (y < 13) {
cout << table[x][y];
if (x == 12) {
cout << " \n";
y++;
x = 0;
}
else {
x++;
};
};
}
void inputMaze(string maze[13][13]) { //create the maze
int x = 0;
int y = 0;
while (y < 13) {
int cX = x + 1;
int cY = y + 1;
if (y == 0 || x == 0) {
maze[x][y] = "# ";
}
else if (y == 6 || x == 6) {
maze[x][y] = "# ";
}
else if (cX % 2 == 0 && cY % 2 == 0) {
maze[x][y] = "O ";
}
else {
maze[x][y] = "# ";
}
if (x == 12) {
x = 0;
y++;
}
else {
x++;
}
}
}
void cleanUp(string maze[13][13]) { //clean up the maze
int x = 0;
int y = 0;
while (y < 13) {
if (maze[x][y] == "U ") {
maze[x][y] = " ";
}
else if (maze[x][y] == "D ") {
maze[x][y] = " ";
}
else if (maze[x][y] == "O ") {
maze[x][y] = " ";
}
else if (maze[x][y] == "X ") {
maze[x][y] = " ";
}
if (x == 12) {
x = 0;
y++;
}
else {
x++;
}
}
}
int getNX(string maze[13][13], int xpos, string dir, int c) { //get the new x
int x;
if (dir == "Left") {
if (xpos == 1) {
x = xpos;
}
else {
x = xpos - c;
}
}
else if (dir == "Right") {
if (xpos == 12) {
x = xpos;
}
else {
x = xpos + c; //c should not be any other number than 1 or 2
}
}
else {
x = xpos;
}
return x;
}
int getNY(string maze[13][13], int ypos, string dir, int c) { //get the new y cell
int y;
if (dir == "Up") {
if (ypos == 1) {
y = ypos;
}
else {
y = ypos - c; //c should not be any other number than 1 or 2
}
}
else if (dir == "Down") {
if (ypos == 12) {
y = ypos;
}
else {
y = ypos + c;
}
}
else {
y = ypos;
}
return y;
}
string cellType(string maze[13][13], int x, int y) { // get us the cell type so we can do some more functions
string type;
if (maze[x][y] == "U ") {
type = "Used";
}
else if (maze[x][y] == "D ") {
type = "DestW";
}
else if (maze[x][y] == "O ") {
type = "Open";
}
else if (maze[x][y] == "X ") {
type = "Visited";
}
else if (maze[x][y] == "# ") {
type = "Wall";
}
return type;
}
void destWall(string maze[13][13], int x, int y, string dir) { //destroy wall
int change = 1;
int wall[2] = { getNX(maze,x,dir,change), getNY(maze,y,dir,change) };
//string type = cellType(maze,wall[1],wall[2]);
maze[wall[0]][wall[1]] = "D";
}
void getStarterCell(int sCell[2]) { //self explanatory
srand(time(0));
int posi[6] = { 1,3,5,7,9,11 };
sCell[0] = posi[(rand() % 6)];
sCell[1] = posi[(rand() % 6)];
}
string newD(string maze[13][13], int cCell[2]) { //first step is getting the new direction
string nDir = "";
string dirs[4] = { "Left", "Right", "Down", "Up" };
string dTypes[4];
string pDirs[4];
int index = 0;
for (int i = 0; i < 4; i++) { //determine usability of each direction
int tx = getNX(maze, cCell[0], dirs[i], 2);
int ty = getNY(maze, cCell[2], dirs[i], 2);
string tType = cellType(maze, tx, ty);
dTypes[i] = tType;
}
for (int i = 0; i < 4; i++) { //parse through array to give access to new, usable directions
if (dTypes[i] == "Open") {
pDirs[index] = dirs[i];
index++;
}
}
if (index > 0) {
int ttt = (rand() % index);
nDir = pDirs[ttt];
}
else {
nDir = "None";
}
return nDir;
}
string backD(string maze[13][13], int cCell[2]) { //if the first step failed then we find the next backtrackable spot
string nDir = "";
string dirs[4] = { "Left", "Right", "Down", "Up" };
string dTypes[4];
string pDirs[4];
int index = 0;
for (int i = 0; i < 4; i++) { //determine usability of each direction
int tx = getNX(maze, cCell[0], dirs[i], 2);
int ty = getNY(maze, cCell[2], dirs[i], 2);
string tType = cellType(maze, tx, ty);
dTypes[i] = tType;
}
for (int i = 0; i < 4; i++) { //parse through array to give access to new, usable directions
if (dTypes[i] == "Visited") {
pDirs[index] = dirs[i];
index++;
}
}
if (index > 0) {
int ttt = (rand() % index);
nDir = pDirs[ttt];
}
else {
nDir = "None";
}
return nDir;
}
void generateMaze(string maze[13][13]) {//need complete remake
int sCell[2];
getStarterCell(sCell);
bool loop = true;
int cCell[2] = {sCell[0], sCell[1]};
int backable = 0;
while (loop) { // gotta loop!
string nDir = newD(maze, sCell); //the new direction
int nexCell[2]; //the next cell
if (nDir == "None") { //backtrack start
nDir = backD(maze, sCell);
if (nDir == "None") {
cout << "Error, Backtracking issue! Backable: " << backable;
break;
}
else {
backable--;
nexCell[0] = {getNX(maze, cCell[0], nDir, 2) };
nexCell[1] = {getNY(maze, cCell[1], nDir, 2) };
}
}
else { //normal procedures
backable++;
nexCell[0] = {getNX(maze, cCell[0], nDir, 2)};
nexCell[1] = {getNY(maze, cCell[1], nDir, 2)};
}
destWall(maze, cCell[0], cCell[1], nDir);
cCell[0] = nexCell[0];
cCell[1] = nexCell[1];
if (backable <= 0) {
cout << "Maze (should be) finished!";
loop = false;
break;
}
}
}
int main() {
string maze[13][13] = {}; //a thirteen by thirteen maze
inputMaze(maze); // just so i dont get bored typing it over and over and over again
generateMaze(maze);
cleanUp(maze); //make it presentable/readable
outputMaze(maze); // output it
return 0;
}
|