- Home /
Generate random path in grid
I'm trying to generate a random path in a 3x3 grid and don't know where to start. What I want to do is have it always start in the bottom center room and end in the top center room, and set values in an array to a different number depending on where there needs to be doors depending on the path. (eg. top-left room (number 0 in array) gets set to 3 because it needs to have a door on the south wall and the east wall. another function then reads those variables to know what kind of room it needs to build) Currently I have made paths that it will choose from but obviously this can get figured out easily. How would I go about doing this so that no rooms are left out?
If you final goal is a 3x3 grid (or even a 4x4 grid), then consider statically initializing a 2D (Jagged) array. Number your rooms 0 through 8 and then work out various paths. There are not that many with a 3x3 grid. Then just declare an array of these paths. You can use Random.Range() to select between them.
Answer by codecranker · May 17, 2013 at 04:09 AM
Since you already fixed the start and end points for your path that makes things lot less simple.
you can do something like the following:
chose a start point. let this be your current position.
get a list of all reachable positions from the current position in all valid directions. (these positions should be the grid-cells adjacent to the current position)
select a random position from the list
move to the new position
if the new position is the end position, return
goto 2
you might end up with longer paths sometime because you are choosing your next positions randomly. if you want to be sure that your path should not get longer beyond a certain length, you can put some logic in choosing your next position from the list of reachable positions. For eg; select a random position from the list whose distance from the end point is within a certain range.
I can see how this would work, but I am doing this with restrictions. With this method it could easily go up, down, then left and get stuck.
Or you could ins$$anonymous$$d use that system which codecranker said but also store already explored positions aswell as checking if the sector your going to move to has possible positions available to move ahead to.
well it depends how complicated you wanna get. the one I mentioned is just high level. As bunnybomb7670 said you can record already explored positions or you can also set repeat count for every position i.e. how many times is it O$$anonymous$$ for your algorithm to repeat a position in your path without raising a red flag.
The step (2) in a algorithm I mentioned takes care of finding "all reachable positions from the current position in all valid directions.". You can always add more validations to it.
Your answer
Follow this Question
Related Questions
Generating random paths 1 Answer
Perform Function, If Not Clicked In 3 Seconds 2 Answers
Code to randomly generate a mesh? 0 Answers
How to move an object on a random path with iTween 1 Answer
Sound playing at random. (JS) 2 Answers