Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Maze game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Maze game" Watch "Maze game" New topic

Maze game

Becky Bloomwood

Joined: Nov 08, 2006
Posts: 2
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?

Thanks in advance
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

"de pressed",

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.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
search the Programming Diversions forum for 'maze' and you'll find some
interesting threads
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

"Becky Bloomwood"

Your name appears to be based on a fictitious character. Please choose a more appropriate name.

[OCA 8 Book] [Blog]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1044
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.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
I agree. Here's the link:
subject: Maze game
It's not a secret anymore!