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;
}

Recommended Answers

All 3 Replies

What does "my code for the maze generator is not working"? Are we supposed to guess?

I'm sorry. But I don't know what's wrong. Just not getting it right

Details.

If you take your car to the mechanic, you can't just tell him "it doesn't work". You have to give him details.

Same with programming. If you want help, you need to explain
1) what's going wrong
2) how you know it's wrong
3) best guess as to where the code is wrong
4) what it should have done

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.