1
\$\begingroup\$

I am a novice programmer trying to add an unnecessary and complicated element to a text adventure! ...and I've run into a question I am not sure how to research further (I suspect due to lack knowledge of the proper terminology).

I am working on a small text adventure that uses an 11x11 array. Player is capable of moving into spaces with a 1 and incapable of moving to a 0. I need to a) retain the outer boundary 0s, b) randomize the content inside the border, and c) ensure that there is a linked path of 1s that will eventually connect to a 2 (the exit):

0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 1 0 0 0
0 0 1 1 1 1 1 1 1 1 0
0 0 1 0 0 0 0 0 0 1 0
0 0 1 1 1 1 1 1 1 1 0
0 1 1 0 1 0 1 0 0 0 0
0 1 0 0 1 0 1 0 0 0 0
0 1 1 1 1 0 1 1 1 2 0
0 0 0 1 0 0 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0

I'd appreciate any pointers here - not sure what direction I should be looking in. Thinking of scrapping this feature so if it's not viable I don't mind hearing that :) Thanks for reading.

\$\endgroup\$
2
  • \$\begingroup\$ So you are essentially trying to generate a maze, but possibly with cycles? If you do not want cycles I suggest researching algorithms to generate spanning trees on graphs. If you want do allow cycles a possibility would be to fill you grid with 1s and iteratively remove a random one, and check if the 1s are still connected. Do this until you are satisfied with the number of 1s. \$\endgroup\$
    – LukeG
    Jan 14, 2017 at 13:57
  • 2
    \$\begingroup\$ What did you try? \$\endgroup\$
    – Vaillancourt
    Jan 14, 2017 at 14:11

1 Answer 1

2
\$\begingroup\$

You are essentially trying to generate a spanning tree.

But what in the hell is a spanning tree? The non-tech humans usually call it a maze!

I will not give you javascript code, but I'll tell you where you can learn how to develop the algorithm yourself. This Presentation from Jamis Buck is the Maze algorithms 101. You MUST read it! It is very simple reading, and has lots of interactive demos.

If you want to dive deep into the theory, try reading the Wikipedia page on Spanning Trees. You'll learn the nomenclatures and find about cycles.

Good luck!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .