Results 1 to 7 of 7

Thread: 2D Arrays and mazes(?)

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    46

    2D Arrays and mazes(?)

    I have a 2 dimensional array. Im trying to use it to generate a maze via some algorithms. Its pretty much way over my head though.
    But thats beside the point. If i need each array coordinate to store multiple values (like if south, west, north, east has walls), what would be a clever way to do so?

    For instance:
    1, 1 in the array needs to keep track of 4 values at least (as do all other positions).

    Should i make it an integer value like "1111" and replace with 0's where the wall is gone? the first 1 being north, second being east e.t.c. Would you choose this option?

    If someone wants to do a bit more research, here is some info on mazes:
    http://www.mazeworks.com/mazegen/mazetut/index.htm
    http://www.astrolog.org/labyrnth/algrithm.htm

    I've been stuck here for a few days, because i cant seem to wrap my head around it.
    I feel like the code is gonna look pretty horrible once im through with it, unless i get it figured out from the start, and that seems hard.
    Has anyone else had any experience doing mazes? Or maybe someone with more experience in programming in general could help me flesh it out.

    Its not for a school project e.t.c. and all that. Just for fun. I'll gladly post the code here when i feel its done so anyone can look at it.

    Thank you.

  2. #2
    New Member
    Join Date
    Nov 2009
    Posts
    15

    Re: 2D Arrays and mazes(?)

    well i dont think u need to keep track of 4 variables, you could just keep track of x and y, and you can just save that specific x,y cell position.

    So what you do is you have an integer array, you mark the current cell visited by changing its value to 1. to get the current cell position you take X+Y*Size of maze , so each time you move you set that cell to 1.

    Then you need backtracking to not get stuck, you could use a vector2 array or something that saves x and y, so if its stuck it goes back to previous x,y of that array, LIFO stack.

    sry if the post makes no sense im a bit tired atm :P

    I made a maze generator a few months ago so if its anything else just ask.

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: 2D Arrays and mazes(?)

    If you need to store multiple values that all relate to the same 'item' then I would say you should be creating your own class to hold those values.
    I havent looked at those links but using your example of needing to store North, East, South and West, you could define a class like so:

    vb Code:
    1. Public Class MazeCoordinate
    2.  
    3.     Public SouthWall As Boolean
    4.     Public NorthWall As Boolean
    5.     Public EastWall As Boolean
    6.     Public WestWall As Boolean
    7.  
    8. End Class
    Note that you should really make those values Properties rather than fields but I wanted to keep it very simple in this example.

    Then to use this class with a 2d array:
    vb Code:
    1. 'Create a 10 by 10 array of our MazeCoordinate class
    2.         Dim CoOrds(9, 9) As MazeCoordinate
    3.  
    4.         'Create a new MazeCoordinate
    5.         Dim NewCoord As New MazeCoordinate
    6.         'We dont need to set the wall properties that we want to be false because
    7.         'they will be false by default anyway
    8.         NewCoord.EastWall = True
    9.         NewCoord.WestWall = True
    10.         'Store this new MazeCoordinate at the first position in the array
    11.         CoOrds(0, 0) = NewCoord

    That help at all?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: 2D Arrays and mazes(?)

    One general question would be this: Is each cell an open space, with walls only on the border between the cell, or is a wall a solid cell? The latter would look kind of like a crossword puzzle, with many solid areas and the rest as open cells, but if two open cells abutted each other, there would never be a wall between them. The former would mean that if Cell(1,1) had a wall to the East, then Cell(2,1) MUST have a wall to the West.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    46

    Re: 2D Arrays and mazes(?)

    First of all, thanks for all the responses. ^^

    Each cell is an open space with 4 walls on the border between the cells.

    The former would mean that if Cell(1,1) had a wall to the East, then Cell(2,1) MUST have a wall to the West.
    I agree with this. Setting 1,1 east wall must also set 2,1's west wall (i dont see any other option atm).

    I really like the look of what you suggested chris128. Defining a class like that would look so much better.
    I guess i better read up on classes and properties... I've been putting it off for too long.

    And i agree with bastanien that in using some algorithms, i need a way to backtrack (when i run into a cell where
    i cant knock down anymore walls) to an earlier spot to see where it can excavate next.

    Thank you for all the lovely suggestions ^^ I'll keep trying and maybe you can keep thinking! Im sure i'll be back to beg further assistance.

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    46

    Re: 2D Arrays and mazes(?)

    Something like this? Keep in mind im totally new to classes and patched this up from examples e.t.c. Im still a bit curious as to how it all works:

    Code:
    Dim coords(2, 2) As MazeCoordinate
    What kind of array type does this code make it? I thought arrays had to be type-specified. If one "Cell" in this array is capable of holding multiple boolean values, i guess it cant be boolean? I feel like i have so much to learn here...
    Code:
    coords(0, 0) = NewCoord
    This is related i guess. Im just curious how it all gets saved, and in what form. Tried converting coords(0, 0) and displaying it, but i havent had any luck (probably for understandable reasons beyond me atm)

    Thank you all ^^

    Code:
    Public Class Form1
        Public Class MazeCoordinate
            Private BooleanSouthwall As Boolean
            Private BooleanNorthwall As Boolean
            Private BooleanEastwall As Boolean
            Private BooleanWestwall As Boolean
    
            Public Property Southwall() As Boolean
                Get
                    Return BooleanSouthwall
                End Get
                Set(ByVal value As Boolean)
                    BooleanSouthwall = value
                End Set
            End Property
    
            Public Property Northwall() As Boolean
                Get
                    Return BooleanNorthwall
                End Get
                Set(ByVal value As Boolean)
                    BooleanNorthwall = value
                End Set
            End Property
    
            Public Property Eastwall() As Boolean
                Get
                    Return BooleanEastwall
                End Get
                Set(ByVal value As Boolean)
                    BooleanEastwall = value
                End Set
            End Property
    
            Public Property westwall() As Boolean
                Get
                    Return BooleanWestwall
                End Get
                Set(ByVal value As Boolean)
                    BooleanWestwall = value
                End Set
            End Property
        End Class
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim coords(2, 2) As MazeCoordinate
            Dim NewCoord As New MazeCoordinate
            NewCoord.Eastwall = True
            NewCoord.Northwall = True
            coords(0, 0) = NewCoord
        End Sub
    End Class

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: 2D Arrays and mazes(?)

    What kind of array type does this code make it? I thought arrays had to be type-specified.
    They do - what you have to realise is that a class IS a type. So your array is of type MazeCoordinate.

    Tried converting coords(0, 0) and displaying it, but i havent had any luck (probably for understandable reasons beyond me atm)
    Well without seeing your code for that part I cant tell you what you are doing wrong
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width