Source of FactoryMazeBuilder.java


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