Hello guys,
I'm working on a maze designer. I start with a 2 dimensional block of cells, and randomly take down walls until there's a path from top left corner to bottom right. My approach is to create an adjacency matrix where:
-1 means two cells (or 2 groups of cells) are not connected or separated by single wall (2+ walls between them)
0 means that 2 cells (or 2 groups of cells) are separated by one wall
1 means that 2 cells are "joined" or it's the cell itself ( my adjacency matrix initialized as cell x (x = any cell) is joined to itself)
locating 0's and connecting cells into cell groups until Begin and End points are in one cell group. However, the code (see below) seems to run even after Begin and End are in one cell group, and leaving pretty much no walls in the maze. I've been trying different things for about a day, and can't find a fix. Anyone has any suggestions?
Thank you!
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
void main()
{
//Begin with a rectangle of maze cells.
// Adjacency matrix is created to represent it.
// Begin Adjacency Matrix Initialization
//delcare matrix dimensions and adjacency matrix
int X = 15;
int Y = 17;
int MazeMatrix[255][255];
// Fill Adjacency Matrix with -1.
for (int aa=0; aa < X*Y; aa++){
for (int bb=0; bb < X*Y; bb++){
MazeMatrix[aa][bb] = -1;}}
//Add current cell groups (represent with 1)
//Initially each individual cell = cell group;
for (int cc=0; cc < X*Y; cc++){
MazeMatrix[cc][cc] = 1;}
// Add edges (represent with 0)
for (int dd=0; dd < X*Y-1; dd++){
if ((dd+1)%X != 0)
{MazeMatrix[dd][dd+1] = 0;
MazeMatrix[dd+1][dd] = 0;}
if (dd < X*Y-X)
{MazeMatrix[dd][dd+X] = 0;
MazeMatrix[dd+X][dd] = 0;}
}
// End Adjacency Matrix Initialization
srand((unsigned)time(0));
// Now the maze will be created by randomly removing separators
// (edges in adjacency matrix), updating adjacency matrix and
// checking if Begin and End points belong to a single cell group.
// Begin Maze Design
//This loop will be executed until Begin and End are in one maze cell
while (MazeMatrix[0][X*Y-1] < 1) {
// Pick coordiantes for a random position in the adjacency matrix
int random_cell1 = rand()%(X*Y);
int random_cell2 = rand()%(X*Y);
// If there's a 0 in it (edge), remove it, merge 2 cells and update
// adjacency matrix
int tracker = 0;
while (MazeMatrix[random_cell1][random_cell2] != 0) {
random_cell2 = (random_cell2 + 1)%(X*Y);
tracker++;
if (tracker%Y == 0)
random_cell1 = (random_cell1 + 1)%(X*Y);
}
for (int ee=0; ee < X*Y; ee++)
{
if (MazeMatrix[random_cell1][ee] < MazeMatrix[random_cell2][ee])
MazeMatrix[random_cell1][ee] = MazeMatrix[random_cell2][ee];
MazeMatrix[random_cell2][ee] = MazeMatrix[random_cell1][ee];
if (MazeMatrix[ee][random_cell1] < MazeMatrix[ee][random_cell2])
MazeMatrix[ee][random_cell1] = MazeMatrix[ee][random_cell2];
MazeMatrix[ee][random_cell2] = MazeMatrix[ee][random_cell1];
}
}
//At this point Adjacency matrix should represent a Maze - there's a path from
// MazeMatrix[0][0] to MazeMatrix[X*Y-1][X*Y-1]
//Now the mase is displayed in the console window
for (int ee = 0; ee < X * Y; ee++ ){
if (ee < X*Y - X){
if (MazeMatrix[ee][ee+1] == 1)
if (MazeMatrix[ee][ee+X] == 1)
cout<<" ";
else
cout<<"_ ";
else
if (MazeMatrix[ee][ee+X] < 1)
cout<<"_|";
else
if (MazeMatrix[ee][ee+X] == 1)
cout<<" |";
if ((ee+1)%X == 0)
cout<<endl;
}
else{
if ((ee + 1) % X == 0)
cout<<endl;
else if (MazeMatrix[ee][ee+1] == 1)
cout<<" ";
else
cout<<" |";
}
}
}