|
-
Nov 6th, 2009, 06:18 PM
#1
Thread Starter
Member
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.
-
Nov 6th, 2009, 06:53 PM
#2
New Member
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.
-
Nov 6th, 2009, 06:54 PM
#3
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:
Public Class MazeCoordinate
Public SouthWall As Boolean
Public NorthWall As Boolean
Public EastWall As Boolean
Public WestWall As Boolean
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:
'Create a 10 by 10 array of our MazeCoordinate class
Dim CoOrds(9, 9) As MazeCoordinate
'Create a new MazeCoordinate
Dim NewCoord As New MazeCoordinate
'We dont need to set the wall properties that we want to be false because
'they will be false by default anyway
NewCoord.EastWall = True
NewCoord.WestWall = True
'Store this new MazeCoordinate at the first position in the array
CoOrds(0, 0) = NewCoord
That help at all?
-
Nov 6th, 2009, 07:18 PM
#4
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
 
-
Nov 7th, 2009, 06:11 AM
#5
Thread Starter
Member
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.
-
Nov 7th, 2009, 02:21 PM
#6
Thread Starter
Member
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
-
Nov 8th, 2009, 09:34 AM
#7
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|