Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Effectively chosing a random direction  (Read 2126 times)
0 Members and 1 Guest are viewing this topic.
Offline Anju_Maaka

Junior Devvie

Medals: 1
Exp: 2 years

« Posted 2013-09-05 21:53:41 »


I'm trying to make a program that generates a random labyrinth, I do this by using a Randomized Depth-First (se wikipedia, , for the basic idea). I have just one problem, I want to be going in randomized directions but I don't want the program to check the same direction twice. So I want it to pick a random direction and if that "cell" has already been visited (meaning there can't be a path going from the current cell to that cell) then I want the program to check another direction from the current cell without ever checking the same direction again (since we know it's unavailable).
One suggestion I was considering is to create a collection and mixing it with Collection.shuffle but I've never used collections before and I don't know how to use that to make the program call 4 methods in a random order that way.
Anyone have a recommendation or a good trick I can use?

Thank you!
Offline wessles
« Reply #1 - Posted 2013-09-05 22:01:50 »

Just pick a random direction and if it is taken add 90 degrees, and repeat.
Offline BurntPizza

« JGO Bitwise Duke »

Medals: 485
Exp: 7 years

« Reply #2 - Posted 2013-09-05 22:33:25 »

Just pick a random direction and if it is taken add 90 degrees, and repeat.

Even if you randomly select which direction to 'spin' 90 degrees, it would produce different output than a purely random selection, which may be undesirable.
For pure random, see my response in the other thread. (posted there before I saw this)

Also, double thread? Merge perhaps?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2013-09-06 00:45:21 »

Learning to use Collections and shuffling is not so bad! Suppose you create a grid of "Cells" and for each cell, create an ArrayList<Cell> called neighborCells, and populate it.

Then, to shuffle the list, one only needs this line:


Any collection that extends the List interface can be used as a parameter in the above code.

To iterate through the randomized collections is easy. Just use a "for each" loop. Assuming a method called something like checkDirection():

for (Cell n: neighborCells)

There are better performing collections than ArrayList for this sort of thing, and neat recursive implementations for tree searching. But I am refraining from going into that because of the reluctance you expressed about Collections. I figure it's good to first get something easy working. As a consequence, I may have aimed this answer too low for your knowledge level.

Which algo from the wiki are you thinking of implementing?

music and music apps:
Pages: [1]
  ignore  |  Print  

EgonOlsen (45 views)
2018-06-10 19:43:48

EgonOlsen (27 views)
2018-06-10 19:43:44

EgonOlsen (47 views)
2018-06-10 19:43:20

DesertCoockie (210 views)
2018-05-13 18:23:11

nelsongames (127 views)
2018-04-24 18:15:36

nelsongames (126 views)
2018-04-24 18:14:32

ivj94 (867 views)
2018-03-24 14:47:39

ivj94 (128 views)
2018-03-24 14:46:31

ivj94 (779 views)
2018-03-24 14:43:53

Solater (143 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!