1

Im using Java to create a maze of specified "rows" and "columns" over each other to look like a grid. I plan to use a depth-first recursive method to "open the doors" between the rooms (the box created by the rows and columns).

I need help writing a openDoor method that will break the link between rooms.

4
  • 3
    Before writing an openDoor method, we need a couple of basic objects that describe the room and the relationship between them (the doors?). Can you show your definition of the maze in terms of data objects? Commented Jun 2, 2010 at 7:29
  • are you trying to CREATE a maze or FIND a way thru maze? Commented Jun 2, 2010 at 8:00
  • I am wanting to create the maze and then later find a way through it. Tho at the moment I am stuck on writing the method to openDoors between "rooms". Commented Jun 2, 2010 at 8:04
  • Im parsing in the current row and column along with the direction (North East South West) into my method. I also have the total number of rows and columns in the class. Is this what you mean? Commented Jun 2, 2010 at 8:04

2 Answers 2

1

try something like this : example

Sign up to request clarification or add additional context in comments.

Comments

1

Because you mention depth-first(-search) (DFS) I assume, your maze is a graph where nodes represent rooms. The nodes are connected if there is an unlocked door between rooms. The graph may be cyclic.

You have a start room and may be looking for something in the maze. So you enter a room, check every door, whether it is unlocked or you have key that fits and open every possible door. You may find a key. Then you add that key to your keyring and restart in the start room.

Formally (adapted from de:wikipedia; see also en.wikipedia):

DFS(node, goal)
{
  if (node == goal)
    return node;
  else if (node.contains(newKey)) 
  {
    addToKeyRing(newKey);
    resetMaze();
    DFS(startRoom, goal);
  } else 
  {
    stack := expand (node) // all unvisited rooms that can be entered pushed on stack
    while (stack is not empty)
    {
      node' := pop(stack);
      DFS(node', goal);
    }
  }
}

1 Comment

Thanks :) Yes, the links are the ones I used for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.