hi everyone
i have to write c++ code to implement a disjoint set class to make a maze generator. i have the disjoint set class working fine. my code for the maze generator is not working.. kindly help
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "djset.h"
using namespace std;
struct Cell
{
Cell() { north = true; south = true; west = true; east = true; }
bool north;
bool south;
bool west;
bool east;
};
int main()
{
srand((unsigned)time(NULL));
int rowCells, columnCells, selectWall, currentCell, randomEntExit;
int partitions;
bool mazeEntExit;
cout << "Please enter the number of cells on each row of the maze: ";
cin >> rowCells;
cout << endl;
cout << "Please enter the number of cells in each column of the maze: ";
cin >> columnCells;
cout << endl << endl;
partitions = rowCells * columnCells;
DisjSet Maze(partitions);
Cell *cells;
cells = new Cell[Maze.GetTotalNodes()];
while (partitions > 1)
{
selectWall = rand() % 4;
// north is selectWall = 0
// south is selectWall = 1
// west is selectWall = 2
// east is selectWall = 3
currentCell = rand() % Maze.GetTotalNodes();
if (selectWall == 0)
{
if (currentCell - rowCells >= 0)
{
if (Maze.find(currentCell) != Maze.find(currentCell - rowCells)
&& cells[currentCell].north == true &&
cells[currentCell-rowCells].south == true)
{
Maze.UnionSets(currentCell, currentCell - rowCells);
cells[currentCell].north = false;
cells[currentCell-rowCells].south = false;
partitions--;
}
}
}
if (selectWall == 1)
{
if (currentCell + rowCells < Maze.GetTotalNodes())
{
if (Maze.find(currentCell) != Maze.find(currentCell + rowCells)
&& cells[currentCell].south == true && cells[currentCell+rowCells].north == true)
{
Maze.UnionSets(currentCell, currentCell + rowCells);
cells[currentCell].south = false;
cells[currentCell+rowCells].north = false;
partitions--;
}
}
}
if (selectWall == 2)
{
if ((currentCell - 1) >= 0)
{
if (Maze.find(currentCell) != Maze.find(currentCell - 1) &&
(currentCell % rowCells) < rowCells
&& cells[currentCell].west == true &&
cells[currentCell - 1].east == true)
{
Maze.UnionSets(currentCell, currentCell - 1);
cells[currentCell].west = false;
cells[currentCell - 1].east = false;
partitions--;
}
}
}
if (selectWall == 3)
{
if ((currentCell + 1) < Maze.GetTotalNodes())
{
if (Maze.find(currentCell) != Maze.find(currentCell + 1) &&
(currentCell % rowCells) > 0
&& cells[currentCell].east == true &&
cells[currentCell + 1].west == true)
{
Maze.UnionSets(currentCell, currentCell + 1);
cells[currentCell].east = false;
cells[currentCell + 1].west = false;
partitions--;
}
}
}
}
mazeEntExit = rand() % 2;
if (mazeEntExit == 0)
{
randomEntExit = rand() % rowCells;
cells[randomEntExit].north = false;
randomEntExit = rand() % rowCells;
randomEntExit -= (Maze.GetTotalNodes() - 1);
cells[randomEntExit].south = false;
}
else
{
randomEntExit = (rand() % columnCells) * rowCells;
cells[randomEntExit].west = false;
randomEntExit = (rand() % columnCells) * rowCells;
if (randomEntExit == 0)
{
while (randomEntExit == 0)
randomEntExit = (rand() % columnCells) * rowCells;
}
randomEntExit--;
cells[randomEntExit].east = false;
}
for (int i=0; i<Maze.GetTotalNodes(); i++)
{
cout << "Cell " << i+1 << ": ";
if (cells[i].north == true)
cout << "North, ";
if (cells[i].south == true)
cout << "South, ";
if (cells[i].west == true)
cout << "West, ";
if (cells[i].east == true)
cout << "East, ";
cout << endl;
}
return 0;
}