开发者

Creating a Maze using Java

开发者 https://www.devze.com 2023-01-01 16:46 出处:网络
Im using Java to create a maze of specified \"rows\" and \"columns\" over each other to look like a grid.

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.


try something like this : example


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);
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消