1  class TestCase {
   2      public TestCase(string forward, string backward) {
   3          forward_ = forward;
   4          backward_ = backward;
   5          }
   6  
   7      public void execute() {
   8          /* ----------------------------------------------------------------------
   9          Strategy:  Traverse the maze twice:
  10  
  11          The first traversal is a "dry run", performed without actually creating the maze.
  12          The purpose is just to determine the maze form factor (dimensions).
  13          This is done without actually creating the maze.  
  14            
  15          Its result is used to create the maze with every path blocked.  
  16          
  17          The second traversal is used to make a path through the maze by removing
  18          blockages.        
  19          ---------------------------------------------------*/
  20          // Determine maze form factor:        
  21          BoundsAction boundsAction = new BoundsAction();
  22          Position position = new Position(0, 0, boundsAction);  // Explorer starts at origin
  23          Orientation.Direction endDirection = traverseMaze(position);
  24  
  25          // Convenience shortcuts:
  26          bool endIsEast  = endDirection==Orientation.Direction.E;
  27          bool endIsWest  = endDirection==Orientation.Direction.W;
  28          bool endIsSouth = endDirection==Orientation.Direction.S;
  29          
  30          // Since we begin from North, subtract 1 from numRows, and zero from numCols:
  31          int numRows = boundsAction.numRows() - 1 - (endIsSouth?1:0); 
  32          int numCols = boundsAction.numCols() - (endIsEast || endIsWest ?1:0);
  33  
  34          // Create maze of correct size with all paths blocked:
  35          maze_ = new Maze(numRows, numCols);  
  36  
  37          // Traverse maze and open paths:
  38          OpenAction openAction = new OpenAction(maze_);
  39          // Translate (offset) starting position, 
  40          // because Maze can have only nonnegative indices:
  41          int startX = -boundsAction.minX() - (endIsWest?1:0);
  42          int startY = numRows;
  43          position = new Position(startX, startY, openAction);
  44          traverseMaze(position);
  45          }
  46  
  47          private Orientation.Direction traverseMaze(Position position) {
  48          Orientation orientation = new Orientation(Orientation.Direction.S);
  49          Explorer explorer = new Explorer(orientation, position);
  50          explorer.processInput(forward_);
  51          Orientation.Direction endDirection = explorer.getOrientation().getDirection();
  52          explorer.turnBackwards();
  53          explorer.processInput(backward_);
  54          return endDirection;  // Direction at which maze is exited after forward traverse.
  55          }
  56  
  57      // ======= Data Members ========
  58      private string forward_;
  59      private string backward_;
  60      public Maze maze_ {get; private set;}
  61  }
  62