Source of MazeGameBuilder.java


  1: 
  2: package maze; 
  3: 
  4: import java.awt.*;
  5: import javax.swing.*;
  6: 
  7: /*
  8:  *  Build a Maze game. 
  9:  *
 10:  *  This implmentation uses Builder and Abstract Factory design patterns. 
 11:  *
 12:  *  Run the program as follows:
 13:  *   java maze.MazeGameBuilder Harry
 14:  *         -- uses the FactoryMazeBuilder with the HarryPotterMazeFactory
 15:  *   java maze.MazeGameBuilder Snow
 16:  *         -- uses the FactoryMazeBuilder with the SnowWhiteMazeFactory
 17:  *   java maze.MazeGameBuilder Simple
 18:  *         -- uses the FactoryMazeBuilder with the default MazeFactory
 19:  *   java maze.MazeGameBuilder
 20:  *         -- uses the SimpleMazeBuilder, which does not use a factory
 21:  *
 22:  */
 23: public class MazeGameBuilder { 
 24: 
 25:   public static Maze createMaze(MazeBuilder builder) { 
 26:     builder.newMaze();
 27:     builder.buildRoom(1); 
 28:     builder.buildRoom(2); 
 29:     builder.buildRoom(3); 
 30:     builder.buildRoom(4); 
 31:     builder.buildRoom(5); 
 32:     builder.buildRoom(6); 
 33:     builder.buildRoom(7); 
 34:     builder.buildRoom(8); 
 35:     builder.buildRoom(9); 
 36: 
 37:     builder.buildDoor(1, 2, Direction.WEST, true); 
 38:     builder.buildDoor(2, 3, Direction.WEST, false); 
 39:     builder.buildDoor(4, 5, Direction.WEST, true); 
 40:     builder.buildDoor(5, 6, Direction.WEST, true); 
 41:     builder.buildDoor(5, 8, Direction.NORTH, false); 
 42:     builder.buildDoor(6, 9, Direction.NORTH, true); 
 43:     builder.buildDoor(7, 8, Direction.WEST, true); 
 44:     builder.buildDoor(1, 4, Direction.NORTH, true); 
 45: 
 46:     return builder.getMaze(); 
 47:   }
 48: 
 49:   public static void main(String[] args) { 
 50:     Maze maze; 
 51:     MazeBuilder builder; 
 52:     MazeFactory factory = null;
 53: 
 54:     if (args.length > 0) { 
 55:       if ("Harry".equals(args[0])) { 
 56:         factory = new maze.harry.HarryPotterMazeFactory(); 
 57:       } else if ("Snow".equals(args[0])) { 
 58:         factory = new maze.snow.SnowWhiteMazeFactory(); 
 59:       } else if ("Default".equals(args[0])) {
 60:         factory = new MazeFactory(); 
 61:       }
 62:     }
 63:     if (factory != null) { 
 64:       builder = new FactoryMazeBuilder(factory);
 65:     } else { 
 66:       builder = new SimpleMazeBuilder();     
 67:     }
 68:     maze = createMaze(builder); 
 69:     maze.setCurrentRoom(1); 
 70:     maze.showFrame("Maze -- Builder");
 71:   }
 72: 
 73: }