Source of SimpleMazeBuilder.java


  1: 
  2: package maze; 
  3: 
  4: public class SimpleMazeBuilder implements MazeBuilder { 
  5: 
  6:   /**
  7:    *  Start to build a new Maze
  8:    */ 
  9:   public void newMaze() {
 10:     maze = new Maze(); 
 11:   } 
 12: 
 13:   /**
 14:    *  Fetch the Maze that have been built. 
 15:    */ 
 16:   public Maze getMaze() {
 17:     return maze; 
 18:   } 
 19: 
 20:   /**
 21:    *  Build a new room in the current Maze. 
 22:    */ 
 23:   public void buildRoom(int roomNumber) {
 24:     if (maze == null) { 
 25:       newMaze();
 26:     }
 27:     Room room = new Room(roomNumber);
 28:     for (Direction dir = Direction.first(); dir != null; dir = dir.next()) { 
 29:       room.setSide(dir, new Wall()); 
 30:     }
 31:     maze.addRoom(room);  
 32:   }
 33: 
 34:   /**
 35:    *  Build a new door in the current Maze between two the specified rooms.
 36:    *
 37:    *  @param roomNumber1 specifies the room number of the first room   
 38:    *  @param roomNumber2 specifies the room number of the second room   
 39:    *  @param dir         specifies the side of the first room at which the door will be located
 40:    *                     the second room will be on the other side of the door. 
 41:    */
 42:   public void buildDoor(int roomNumber1, int roomNumber2, Direction dir, boolean open) { 
 43:     if (maze == null) { 
 44:       newMaze();
 45:     }
 46:     Room room1 = maze.findRoom(roomNumber1);
 47:     Room room2 = maze.findRoom(roomNumber2);
 48:     if (room1 != null && 
 49:         room2 != null &&
 50:         dir != null) { 
 51:       Door door = new Door(room1, room2); 
 52:       room1.setSide(dir, door); 
 53:       room2.setSide(dir.opposite(), door); 
 54:       door.setOpen(open);
 55:     }
 56:   }
 57: 
 58:   protected Maze maze; 
 59: 
 60: }