Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need An Idea to solve This  RSS feed

 
Ross Miller
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
puzzle.JPG
[Thumbnail for puzzle.JPG]
 
Campbell Ritchie
Sheriff
Posts: 55323
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use code tags or HTML <pre> tags to make formatted text look right.
 
Ross Miller
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>
 
Ross Miller
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't part of the assignment to figure out how to generate a maze?
 
Ross Miller
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ofcourse objective is to generate a maze !
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what do you have so far for the maze generation part?
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Froggy, please read your private messages.
 
Ross Miller
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Saloon Keeper
Posts: 2834
42
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

  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.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!