Hi !
Featured games (88)
games approved by the League of Dukes
Games in Showcase (679)
Games in Android Showcase (194)
games submitted by our members
Games in WIP (733)
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 1161 times)
0 Members and 1 Guest are viewing this topic.
Offline Anju_Maaka

Senior Newbie

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

JGO Kernel

Medals: 256
Projects: 4

Enthusiast of amusement. Lover of code.

« 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: 482
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  
You cannot reply to this message, because it is very, very old.

Hydroque (20 views)
2016-05-26 14:45:46

Mac70 (41 views)
2016-05-24 21:16:33

theagentd (43 views)
2016-05-14 18:38:35

theagentd (77 views)
2016-05-10 22:37:41

theagentd (85 views)
2016-05-10 22:33:46

IanParcs (111 views)
2016-04-18 14:18:53

KaiHH (110 views)
2016-04-18 08:35:41

KaiHH (132 views)
2016-04-15 12:43:58

theagentd (136 views)
2016-04-14 02:16:17

theagentd (159 views)
2016-04-14 02:15:43
FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17

List of Learning Resources
by SilverTiger
2016-02-05 09:39:47

List of Learning Resources
by SilverTiger
2016-02-05 09:38:38

List of Learning Resources
by SilverTiger
2016-02-05 09:35:50 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!