• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Creating A Maze With A Console Application in C#.

I've been learning about maze Algorithms and have started developing my own. I am still at the very beginning of my maze development. Currently I have three cells stacked on top of each other using these strings down below. What I am aiming for is to have three columns and three rows of cells instead. Any help and feedback would be greatly appreciated.

Code:
        public static int rows = 3;
        public static int cols = 3;
        
        public static string space = "  ";
        public static string corner = "+";
        public static string horz = "----";
        public static string vert = "|";
        public static string result = "";
        
        public static int[,]Cell = new int [rows,cols];

        static void DrawMaze()
        {
            
            for(int i = 0; i < Cell.GetLength(0); i++)
            {
                Console.WriteLine(corner + horz + corner);
                Console.WriteLine(vert + space + space + vert);
            }
        }
 
If you want to get into generating and solving mazes then start how you want you represent the maze internally… some kind of structure that encodes which cells have walls top/left/bottom/right, or maybe encoding which cells are connected to which others (better if you want to allow for non-rectangular mazes). You’ll also want it to hold info like whether a cell is part of a solution, whether you have visited it while searching, etc. Think about how you can make that structure easy for implementing maze-solving algorithms.
Then you can code something to display that maze using console characters, vector graphics, HTML or whatever (Maybe more than one over time).
Now you can code and test solvers.
 
Last edited:
To achieve a 3x3 grid with walls and open spaces, you can extend your DrawMaze method. Here's a modified version of your code:


C#:
public static int rows = 3;
public static int cols = 3;

public static string space = "  ";
public static string corner = "+";
public static string horz = "----";
public static string vert = "|";
public static string result = "";

public static int[,] Cell = new int[rows, cols];

static void DrawMaze()
{
    for (int i = 0; i < Cell.GetLength(0); i++)
    {
        // Draw the top wall of each cell
        for (int j = 0; j < Cell.GetLength(1); j++)
        {
            Console.Write(corner + horz);
        }
        Console.WriteLine(corner);

        // Draw the content and side walls of each cell
        for (int j = 0; j < Cell.GetLength(1); j++)
        {
            Console.Write(vert + space);
        }
        Console.WriteLine(vert);
    }

    // Draw the bottom wall of the maze
    for (int j = 0; j < Cell.GetLength(1); j++)
    {
        Console.Write(corner + horz);
    }
    Console.WriteLine(corner);
}

static void Main()
{
    DrawMaze();
}


This code will produce a simple 3x3 maze with walls and open spaces. You can customize the appearance of the walls and spaces by modifying the corner, horz, vert, and space variables.

Anyone Can Learn to Code! 550+ Hours of Course Content!
 
To achieve a 3x3 grid with walls and open spaces, you can extend your DrawMaze method. Here's a modified version of your code:


C#:
public static int rows = 3;
public static int cols = 3;

public static string space = "  ";
public static string corner = "+";
public static string horz = "----";
public static string vert = "|";
public static string result = "";

public static int[,] Cell = new int[rows, cols];

static void DrawMaze()
{
    for (int i = 0; i < Cell.GetLength(0); i++)
    {
        // Draw the top wall of each cell
        for (int j = 0; j < Cell.GetLength(1); j++)
        {
            Console.Write(corner + horz);
        }
        Console.WriteLine(corner);

        // Draw the content and side walls of each cell
        for (int j = 0; j < Cell.GetLength(1); j++)
        {
            Console.Write(vert + space);
        }
        Console.WriteLine(vert);
    }

    // Draw the bottom wall of the maze
    for (int j = 0; j < Cell.GetLength(1); j++)
    {
        Console.Write(corner + horz);
    }
    Console.WriteLine(corner);
}

static void Main()
{
    DrawMaze();
}


This code will produce a simple 3x3 maze with walls and open spaces. You can customize the appearance of the walls and spaces by modifying the corner, horz, vert, and space variables.

Anyone Can Learn to Code! 550+ Hours of Course Content!
Thanks for replying to my post. I have manage to progress although my maze does not look as good as yours and my code is not as clean! Its fine for now I guess.
 
Last edited:
That does indeed draw a complete grid. Now the interesting question is how to draw it with some N, S, E, or W walls missing so as to draw a solvable maze.
This is where I'm at now. I am thinking of using binary tree and then researching and implementing other algorithms for maze generating. After I am done with that I will try and draw my maze with html.
 
I did something similar a few years ago. Built a generic maze model with implementations for rectangular , circular, and random geometries.Used simple console output for development, then a proper gui once it was working. All in Java, so no different from C# really. It was enormous fun. I used Kruskal’s algorithm to generate mazes, but your post has triggered me to dig out the original project and play with other algorithms.
The one big lesson I took away from it was the Importance of how you represent the cells of the maze and how they connect. Get that right and implementing algorithms becomes easy.
I Think you‘re going to have a lot of fun with this. Let us know here how you get on, or if you need any help.
J
 

New Threads

Latest posts

Buy us a coffee!

300x250
Top Bottom