views:

291

answers:

2

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.

+1  A: 

try something like this : example

c-verde
+1  A: 

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);
    }
  }
}
Andreas_D
+1 Added links; please revert if incorrect.
trashgod
Thanks :) Yes, the links are the ones I used for the answer.
Andreas_D