Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (488)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 633 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 »

Hello!

I'm trying to make a program that generates a random labyrinth, I do this by using a Randomized Depth-First (se wikipedia, http://en.wikipedia.org/wiki/Maze_generation_algorithm , 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 Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #1 - Posted 2013-09-05 22:01:50 »

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

Online BurntPizza
« 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:

1  
Collections.shuffle(neighborCells);


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():

1  
2  
3  
4  
for (Cell n: neighborCells)
{
    checkDirection(n);
}


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?

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (69 views)
2014-08-11 02:49:23

BurntPizza (46 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!