• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

C# Recursive Backtracking maze generator in console

DragonCz

New Member
Joined
May 10, 2012
Messages
5 (0.00/day)
Hello there, I am fairy new to C#, soo our teacher gave us homework to create a backtracking program in console to solve mazes. But really hate writing it by hand, soo I wanted to make a mze generator, but I cannot realize how make it do these things:
It will run in console
You will specify width and height
You will set filepath
Then you will press enter and you are done
It will write a height*width maze into a file, where 0 is empty space, 1 is a wall, 2 is a start and 3 is the end

This is NOT a homework, it is just to add some special features to it, and no, I will not show it to our teacher, because he want to explain everything, and I dont know how this program will be made.

I dont want it 100% recursive backtracking method, but I want to learn something on it too by myself.

Regards, DragonCz

Here is my code, it is in Czech language at the end, where it ends.
http://pastebin.com/At30CFf5

I know its not optimal or recursive, but works
You have to edit the path, too
 
Last edited:
Joined
Jun 17, 2007
Messages
7,335 (1.19/day)
Location
C:\Program Files (x86)\Aphexdreamer\
System Name Unknown
Processor AMD Bulldozer FX8320 @ 4.4Ghz
Motherboard Asus Crosshair V
Cooling XSPC Raystorm 750 EX240 for CPU
Memory 8 GB CORSAIR Vengeance Red DDR3 RAM 1922mhz (10-11-9-27)
Video Card(s) XFX R9 290
Storage Samsung SSD 254GB and Western Digital Caviar Black 1TB 64MB Cache SATA 6.0Gb/s
Display(s) AOC 23" @ 1920x1080 + Asus 27" 1440p
Case HAF X
Audio Device(s) X Fi Titanium 5.1 Surround Sound
Power Supply 750 Watt PP&C Silencer Black
Software Windows 8.1 Pro 64-bit
Joined
Jun 17, 2007
Messages
7,335 (1.19/day)
Location
C:\Program Files (x86)\Aphexdreamer\
System Name Unknown
Processor AMD Bulldozer FX8320 @ 4.4Ghz
Motherboard Asus Crosshair V
Cooling XSPC Raystorm 750 EX240 for CPU
Memory 8 GB CORSAIR Vengeance Red DDR3 RAM 1922mhz (10-11-9-27)
Video Card(s) XFX R9 290
Storage Samsung SSD 254GB and Western Digital Caviar Black 1TB 64MB Cache SATA 6.0Gb/s
Display(s) AOC 23" @ 1920x1080 + Asus 27" 1440p
Case HAF X
Audio Device(s) X Fi Titanium 5.1 Surround Sound
Power Supply 750 Watt PP&C Silencer Black
Software Windows 8.1 Pro 64-bit
I've only just finished my intro to C++ class so I doubt I'm the guy your looking for.

But... Are you sure you have the required libraries to make it work?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

??
 

DragonCz

New Member
Joined
May 10, 2012
Messages
5 (0.00/day)
Yes, I am sure, but still it dont work, because it cant find the Main (entry) point of the program, and I have no idea where it is
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
The code at the end is written in C (not C#) and uses pointers and it's own memory management, which is considered unsafe in C# code.

You said your code works. What's issues are you having?

Also ... Please post code here using the [code][/code] tags, so it's listed in the thread. Makes it a little easier when helping someone.
 
Last edited by a moderator:

DragonCz

New Member
Joined
May 10, 2012
Messages
5 (0.00/day)
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Maze.Logic
{
	[Flags]
	public enum Directions
	{
		N = 1,
		S = 2,
		E = 4,
		W = 8
	}

	public class Grid
	{
		private const int _rowDimension = 0;
		private const int _columnDimension = 1;

		public int MinSize { get; private set; }
		public int MaxSize { get; private set; }
		public int[,] Cells { get; private set; }

		public Grid() : this(3, 3)
		{

		}

		public Grid(int rows, int columns)
		{
			MinSize = 3;
			MaxSize = 10;
			Cells = Initialise(rows, columns);
		}

		public int[,] Initialise(int rows, int columns)
		{
			if (rows < MinSize)
				rows = MinSize;

			if (columns < MinSize)
				columns = MinSize;

			if (rows > MaxSize)
				rows = MaxSize;

			if (columns > MaxSize)
				columns = MaxSize;

			var cells = new int[rows, columns];

			for (int i = 0; i < rows; i++)
			{
				for (int j = 0; j < columns; j++)
				{
					cells[i, j] = 0;
				}				
			}

			return cells;
		}

		private Dictionary<Directions, int> DirectionX = new Dictionary<Directions, int>
		{
			{ Directions.N, 0 },
			{ Directions.S, 0 },
			{ Directions.E, 1 },
			{ Directions.W, -1 }
		};

		private Dictionary<Directions, int> DirectionY = new Dictionary<Directions, int>
		{
			{ Directions.N, -1 },
			{ Directions.S, 1 },
			{ Directions.E, 0 },
			{ Directions.W, 0 }
		};

		private Dictionary<Directions, Directions> Opposite = new Dictionary<Directions, Directions>
		{
			{ Directions.N, Directions.S },
			{ Directions.S, Directions.N },
			{ Directions.E, Directions.W },
			{ Directions.W, Directions.E }
		};

		public int[,] Generate()
		{
			var cells = Cells;
			CarvePassagesFrom(0, 0, ref cells);
			return cells;
		}

		public void CarvePassagesFrom(int currentX, int currentY, ref int[,] grid)
		{
			var directions = new List<Directions>
			{
				Directions.N,
				Directions.S,
				Directions.E,
				Directions.W
			}
			.OrderBy(x => Guid.NewGuid());

			foreach (var direction in directions)
			{
				var nextX = currentX + DirectionX[direction];
				var nextY = currentY + DirectionY[direction];

				if (IsOutOfBounds(nextX, nextY, grid))
					continue;

				if (grid[nextY, nextX] != 0) // has been visited
					continue;

				grid[currentY, currentX] |= (int)direction;
				grid[nextY, nextX] |= (int)Opposite[direction];

				CarvePassagesFrom(nextX, nextY, ref grid);
			}
		}

		private bool IsOutOfBounds(int x, int y, int[,] grid)
		{
			if (x < 0 || x > grid.GetLength(_rowDimension) - 1)
				return true;

			if (y < 0 || y > grid.GetLength(_columnDimension) - 1)
				return true;

			return false;
		}

		public void Print(int[,] grid)
		{
			var rows = grid.GetLength(_rowDimension);
			var columns = grid.GetLength(_columnDimension);

			// Top line
			Console.Write(" ");
			for (int i = 0; i < columns; i++)
				Console.Write(" _");
			Console.WriteLine();

			for (int y = 0; y < rows; y++)
			{
				Console.Write(" |");

				for (int x = 0; x < columns; x++)
				{
					var directions = (Directions)grid[y, x];

					var s = directions.HasFlag(Directions.S) ? " " : "_";

					Console.Write(s);

					s = directions.HasFlag(Directions.E) ? " " : "|";

					Console.Write(s);					
				}

				Console.WriteLine();
			}
		}
	}
}
This code works (no errors), BUT it does not have an entry point (Main), soo it cant run
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
So give it an entry point (main).
You currently just have a public class sitting there and nothing that is instancing it or call it's methods.

If you are using Visual Studio (or Visual C# Express), just create a new console app and it will auto-generate the entry point (main method in a class called "Program"). You can then stuff in your class and add the calls you need.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Main looks like this:
Code:
internal static void Main(string[] args)
{
}
or (if you don't want arguments)
Code:
internal static void Main()
{
}
You could name it anything and change it under the project properties -> Application -> Startup object. (Not set) will default it to Main.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Whatever you want the program to do which is likely execute the code you pasted before.
 
Top