Hi, i'd like some information on creating a very simple maze program. No graphical features of java need be used. The walls would be represented by asterixes and the robot would be made of the keyboards full stop character. The robot will try to find its way out of the maze. I would think that the maze would be made from a 2 dimensional array and the code would be looped until the robot found its way out, but other the that i'm not sure. Can anyone help?
There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
As I see it, there are two pieces to this: A) Making a maze and B) Solving the maze.
A) Making a maze. I agree that the most obvious data structure, a two-dimensional array, is probably correct. But an array of what? Strings? chars? ints? A custom class? Some standard library class? Well what information do you need for each cell? You might say that you need to know whether each of the four surrounding walls is solid or not, but then the state of any one wall has to be maintained in the the two squares that the wall separates.
The other option is to track the state of only two walls (perhaps the top and left walls) for each square. The plus side of this approach is that you have to manipulate only half as many values per cell. The minus side is that the entire right column and bottom row of the maze pose strange border condition problems.
There is a neat algorithm for generating a maze:
B) Solving the maze The simplest algorithm for solving a maze is to have the "robot" stay against one wall. Whenever the robot enters a new cell, it should try to turn left, go straight, turn right, or do a u-turn, in that order.
Cool?
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
I started to write up some of my experience playing with mazes and then thought to look at Wikipedia. It's always worth a visit.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi