This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need An Idea to solve This Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need An Idea to solve This" Watch "Need An Idea to solve This" New topic
Author

Need An Idea to solve This

Ross Miller
Greenhorn

Joined: Jan 10, 2010
Posts: 7
Write a program in Java that randomly generates two-dimensional mazes and prints
them to the console. For example, a possible output might look like:
XXXXXXXXXXX
X X X
XXX X X X X
X X X X
X XXX X XXX
X X X
XXXXXXXXX X

The width of this maze is 11, and its height is 7. Your program should generate and
display 20 such mazes whose widths and heights are random odd numbers between 5 and
79, inclusive.

Each maze must be 2 entrances that are reachable from each other.

The following mazes do not meet the constraint:
XXXXXXX
X
XXXXX X
X X X There is only 1 entrance.
X XXX X
X X
XXXXXXX

X XXXXX
X X
X XXX X
X X X There are 3 entrances.
X XXX X
X X
XXX XXX

XXXXX X
X X X
X XX X X
X X X No path exists between both entrances.
X XXX X
X X X
XXX XXX
Feel free to use standard APIs in your solution.

Can any one help me in getting a logic to build a single maze atleast..! Constructing such a maze is been bugging me since two days !!!

PS: Attached is the proper image. Please download it to have better view of the mazes !


[Thumbnail for puzzle.JPG]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
And what have you done so far? What problems have you encountered? And your thread title is not at all helpful.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and Welcome to JavaRanch
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You can use code tags or HTML <pre> tags to make formatted text look right.
Ross Miller
Greenhorn

Joined: Jan 10, 2010
Posts: 7
@Seetha : Yeah my first post in the Ranch ! :p

well, hmm.. my progress till now is to create a random odd number X odd number matrix using a two dimensional array filled with "X"s.

And i could get a random entry point and an exit point.

and i am stuck up at a point where i should actually create a path from entry to exit and which i mean the whole array should actually look like a maze !


any ideas to progress with this ? or rather generating a maze creating a path from entry to exit ??!!
Ross Miller
Greenhorn

Joined: Jan 10, 2010
Posts: 7
>
Ross Miller
Greenhorn

Joined: Jan 10, 2010
Posts: 7
XXXX XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX XXXXXXXXX


A sample output of my above code :-|.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Isn't part of the assignment to figure out how to generate a maze?
Ross Miller
Greenhorn

Joined: Jan 10, 2010
Posts: 7
ofcourse objective is to generate a maze !
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So, what do you have so far for the maze generation part?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Froggy, please read your private messages.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ross Miller
Greenhorn

Joined: Jan 10, 2010
Posts: 7
@David: Nothing as of now, except to create a 2D array filled with all X's and get a random entry and exit point for it.

and i am wondering how would i progress from here to generate a maze !
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 174


  1. Initialize 2D array of ints to 0x0F where each of the 4 bits represents its four walls (north,south,east, and west).

  2. Initialize an empty array of 'neighbors' holding the X,Y position of potential neighbors.

  3. Select a random cell and add its four neighbors to the neighbor list.

  4. while neighbor list is not empty:


    1. Select a random neighbor.

    2. Knock down a random wall between neighbor and established cell.

    3. Add to neighbor list any available neighbors to the selected cell.


  5. Remove any two exterior walls.

  6. Draw.


 
GeeCON Prague 2014
 
subject: Need An Idea to solve This