Create maze field

rubenarrr

New member
Joined
Mar 22, 2014
Messages
3
Programming Experience
Beginner
Hi im new here. Im trying to create a maze game in which all the user does is hit go and the AI mouse goes on its own. Im having trouble getting started creating the field. Im trying to use a 2d array of string to make the field look like this. Note the size is dynamic since it's entered by the user.
C#:
x x x x x x
x x       x
x x x x x x
x x  x x x x
x  x x x x x
x x x x  x x
   x x x x x
x    
x x x x x x
I tried using a panel filled with labels and also tried using just one huge panel but when I try to fill it in with the array then it just continues on in the same line and I don't know how to make it stop once it hits the edges of the panel. Is there a more efficient way of doing this? Like without a panel or something I don't know about?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,130
Location
Sydney, Australia
Programming Experience
10+
You could simply use a single Label. You would assign a single String to the Text property built from your array. Traversing 2D arrays is usually done with a pair of nested loops and you would build up the String directly or, preferably, using a StringBuilder. You'd also need to use a fixed-width font for the Label so that the spaces representing the empty cells took up the same amount of space as those representing the walls.

Alternatively, you could use a TableLayoutPanel with a separate Label in each cell.
 

rubenarrr

New member
Joined
Mar 22, 2014
Messages
3
Programming Experience
Beginner
You could simply use a single Label. You would assign a single String to the Text property built from your array. Traversing 2D arrays is usually done with a pair of nested loops and you would build up the String directly or, preferably, using a StringBuilder. You'd also need to use a fixed-width font for the Label so that the spaces representing the empty cells took up the same amount of space as those representing the walls.

Alternatively, you could use a TableLayoutPanel with a separate Label in each cell.
Ok I was able to get that but I want the border to be filled with X's except two random spots on both the right and left sides that represent the entrance and exits. Im unsure of how to do that

C#:
 Random random = new Random();


            for (int i = 0; i < 70; i++)
            {
                for (int j = 0; j < 70; j++)
                {
                    findout[i, j] = random.Next(1,100);


                }
            }




            string[,] positions = new string[70, 70];
            for (int i = 0; i < 70; i++)
            {
                for (int j = 0; j < 70; j++)
                {
                   if (findout[i, j] < d)              //d is density that the user wants the maze to be filled with X's
                     positions[i, j] = "X"; 
                   else                        positions[i, j] = " ";

                }
            }


            for (int i = 0; i < 25; i++)
            {
                for (int j = 0; j < 25; j++)
                {
                    
                    gridmaze.Text += positions[i, j];


                }
            }
 
Last edited:
Top Bottom