예제 #1
0
  public static void main(String[] args) {
    int count = 0;

    int equal = 0;

    for (int i = 0; i < 100; ++i) {
      Maze maze = new Maze(10, 10);
      // maze.initTestCase();

      maze.printMaze();
      System.out.println(maze.startPoint);
      System.out.println(maze.targetPoint);
      maze.adaptiveAstar();
      int a = maze.numOfExpandNodes;
      System.out.println("");
      maze.repeatedFAstar();
      int b = maze.numOfExpandNodes;
      System.out.println("");
      maze.repeatedBAstar();

      if (maze.success) {
        if (b > a) count++;
        else if (b == a) equal++;
        else {
          System.out.println("*");
        }
      }
    }

    System.out.println(count);
    System.out.println(equal);
  }
 /**
  * THIS PLAYER USES MAZE.JAVA This player is very smart. Finds the closest gem without crossing
  * any walls and moves to obtain it. If no path to any jewel is available, the player stays in its
  * place.
  */
 public Direction nextMove(
     HashMap<String, MazePosition> players, ArrayList<MazePosition> jewels, MazeView maze) {
   MazePosition myGem = findClosestLegalGem(players.get(this.name), jewels, maze);
   int endX = myGem.row * 2 - 1;
   int endY = myGem.col * 2 - 1;
   int begX = players.get(this.name).row * 2 - 1;
   int begY = players.get(this.name).col * 2 - 1;
   if (endX == begX && endY == begY) {
     return Direction.values()[1];
   }
   if (Maze.solve(begX, begY, endX, endY, maze) == true) {
     int finX = Maze.trace(begX, begY, endX, endY, maze).get(0);
     int finY = Maze.trace(begX, begY, endX, endY, maze).get(1);
     if (finX > begX) {
       return Direction.values()[2];
     }
     if (finX < begX) {
       return Direction.values()[3];
     }
     if (finY > begY) {
       return Direction.values()[0];
     }
     if (finY < begY) {
       return Direction.values()[1];
     }
   }
   return Direction.values()[1];
 }
예제 #3
0
  /**
   * Solves the maze for four robot starting positions
   *
   * @param draw
   */
  private boolean solve(boolean draw) {
    maze.draw();

    Point robotPositions[] = new Point[4];
    robotPositions[0] = new Point(1, 1); // bottom left
    robotPositions[1] = new Point(1, MAZE_SIZE); // top left
    robotPositions[2] = new Point(MAZE_SIZE, 1); // bottom right
    robotPositions[3] = new Point(MAZE_SIZE, MAZE_SIZE); // top right

    // one position for each robot
    boolean[] solved = {false, false, false, false};

    for (int i = 0; i < 4; i++) {
      if (robotPositions[i] != null) {
        solved[i] = maze.solve(robotPositions[i], maze.GetCentrePoint(), draw);
        // System.out.println("Robot " + (i + 1) + " path solved: " + solved[i]);
        if (draw) {
          StdDraw.clear();
          maze.draw();
        }
      }
    }

    return (solved[0] && solved[1] && solved[2] && solved[3]);
  }
 @Override
 public void initialize(BlockPosition aFrom, BlockPosition aTo) {
   super.initialize(aFrom, aTo);
   int lMazeWidth =
       (width - (borderThickness * 2 + wallThickness))
           / (corridorWidth + wallThickness); // rand + wände + gang
   int lMazeDepth =
       (depth - (borderThickness * 2 + wallThickness))
           / (corridorWidth + wallThickness); // rand + wände + gang
   int lMazeHeight =
       ((height - (borderThickness * 2 + (ceilingInTopLevel ? ceilingThickness : 0)))
           / (corridorHeight + ceilingThickness)); // rand + wände + gänge hoch
   fMat = Material.getMaterial(baseMaterial.toUpperCase());
   if (fMat == null) {
     fMat = Material.getMaterial(Integer.parseInt(baseMaterial));
   }
   fMaze = new Maze(lMazeWidth, lMazeHeight, lMazeDepth);
   fMaze.chanceForUpDown = chanceForUpDown;
   if (breakMoreWalls) {
     fMaze.chanceForBreakWalls = chanceForBreakWalls;
   }
   quest.log(
       "Lobster: cw="
           + corridorWidth
           + " ch="
           + corridorHeight
           + " wt="
           + wallThickness
           + " bt="
           + borderThickness);
   quest.log("Area: w=" + width + " h=" + height + " d=" + depth);
   quest.log("Maze: w=" + lMazeWidth + " h=" + lMazeHeight + " d=" + lMazeDepth);
 }
예제 #5
0
 @Test
 public void testCreateFieldGetFieldCorrect() throws Exception {
   Point point = maze.get(2, 3);
   assertThat(point.x, equalTo(2));
   assertThat(point.y, equalTo(3));
   point = maze.get(3, 4);
   assertThat(point.x, equalTo(3));
   assertThat(point.y, equalTo(4));
 }
  public static void main(String[] args) {
    Maze labyrinth = new Maze();
    System.out.println(labyrinth);

    if (labyrinth.traverse(0, 0)) System.out.println("The maze was successfully traversed!");
    else System.out.println("There is no possible path.");

    System.out.println(labyrinth);
  }
예제 #7
0
  public static void main(String[] args) throws FileNotFoundException {
    Maze f;
    f = new Maze("data3.dat", true); // true animates the maze.

    f.clearTerminal();
    f.solve();

    f.clearTerminal();
    System.out.println(f);
  }
 // a test client
 public static void main(String[] args) {
   // This line throws an index out bounds with any value in args ???
   // int N = Integer.parseInt(args[0]);
   // TODO: change this value to increase the complexity of the Maze
   int N = 10;
   Maze maze = new Maze(N);
   StdDraw.show(0);
   maze.draw();
   maze.solve();
 }
예제 #9
0
 /**
  * Generate the canonical Wumpus Maze of 20 rooms, each having 3 exits
  *
  * @return the Maze object
  */
 public static Maze DodecahedralMaze() {
   Maze M = new Maze(20);
   int[][] pts = {
     {1, 2}, {1, 5}, {1, 0}, {2, 3}, {2, 18}, {3, 4}, {3, 16}, {4, 5}, {4, 14}, {5, 6}, {6, 7},
     {6, 13}, {7, 8}, {7, 0}, {8, 9}, {8, 12}, {9, 10}, {9, 19}, {10, 11}, {10, 17}, {11, 12},
     {11, 15}, {12, 13}, {13, 14}, {14, 15}, {15, 16}, {16, 17}, {17, 18}, {18, 19}, {19, 0}
   };
   for (int i = 0; i < pts.length; i++) M.join2(pts[i][0], pts[i][1]);
   return M;
 }
예제 #10
0
파일: Maze.java 프로젝트: cwang324/MKS22X
  public static void main(String[] args) {

    Maze m = new Maze("data.dat", true);
    m.solve();

    if (m.debug) {
      System.out.println(m.maze.length + "/" + m.maze[0].length);
      System.out.println(m.maze[1][1]);
      System.out.println(m.startx);
    }
  }
예제 #11
0
 /**
  * Generate a Hypercubic Maze of 2, 3 or 4 dimensions
  *
  * @param dim 2,3 or 4
  * @return The Maze object
  */
 public static Maze HypercubeMaze(int dim) {
   // this should work for any dims,
   // However limiting to 4 'coz we constrain max #exits per room.
   if (dim > 4) throw new RuntimeException("dim too large");
   int nnodes = 2 << (dim - 1);
   Maze M = new Maze(nnodes);
   for (int i = 0; i < nnodes; i++) {
     for (int j = 0; j < dim; j++) {
       M.join(i, i ^ (1 << j));
     }
   }
   return M;
 }
예제 #12
0
 private void exitActionPerformed(java.awt.event.ActionEvent evt) {
   if (size14.isSelected()) {
     Maze.gridSize = 14;
     Maze.afterChoose();
     this.dispose();
   } else if (size19.isSelected()) {
     Maze.gridSize = 19;
     Maze.afterChoose();
     this.dispose();
   } else if (size20.isSelected()) {
     Maze.gridSize = 20;
     Maze.afterChoose();
     this.dispose();
   } else if (size21.isSelected()) {
     Maze.gridSize = 21;
     Maze.afterChoose();
     this.dispose();
   } else if (size27.isSelected()) {
     Maze.gridSize = 27;
     Maze.afterChoose();
     this.dispose();
   } else if (size28.isSelected()) {
     Maze.gridSize = 28;
     Maze.afterChoose();
     this.dispose();
   } else if (size29.isSelected()) {
     Maze.gridSize = 29;
     Maze.afterChoose();
     this.dispose();
   } else if (size30.isSelected()) {
     Maze.gridSize = 30;
     Maze.afterChoose();
     this.dispose();
   }
 }
예제 #13
0
  public static void main(String[] args) {
    Maze m = new Maze(4, 4);
    System.out.println(m.bits);
    System.out.println("\n\n\n");
    // m.bitsPrintMaze();
    // System.out.println("\n\n");
    // m.display();

    m.load(
        "111111111"
            + "1Y0010101"
            + "111010101"
            + "101000101"
            + "111110101"
            + "100000101"
            + "101111101"
            + "100000001"
            + "111111111");
    System.out.println("\n\n\n");
    m.display();
    System.out.println("\n\n\n");
    System.out.println(m.nesw(0, 0));
    System.out.println(m.findCell(0, 0));

    System.out.println("\n\n\n");
    System.out.println("Is there a wall between 1,2 and 1,1?");
    System.out.println(m.isWall(1, 2, 1, 1));
    System.out.println("\n\n\nSolve 0,0 to 3,0");
    System.out.println(m.solve(0, 0, 3, 0));

    m.trace(0, 0, 3, 0);
  }
예제 #14
0
파일: TestCases.java 프로젝트: ethanz/Maze
  public void run() {
    File compare = new File("CompareResults.txt");
    PrintWriter writer;
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int x = 0;
    int y = 0;
    int z = 0;
    int e = 0;
    int notReachable = 0;
    int counter = 0;
    int equal = 0;
    while (counter < 1000) {
      try {
        maze = new Maze(101);
        maze.init();
        search = new RepeatedAStar(maze);
        cells = this.maze.genStartGoal();
        writer = new PrintWriter(compare);
        writer.println("Start Cell: " + cells[0].getRow() + " " + cells[0].getColumn());
        writer.println("Goal Cell: " + cells[1].getRow() + " " + cells[1].getColumn());
        writer.println();
        a = runAStar(writer);
        // e = runAStar(writer);
        b = testTiebreak(writer);
        c = testBackward(writer);
        d = testAdaptive(writer);

        writer.close();
      } catch (FileNotFoundException ex) {
        System.out.println("File not found.");
      }
      if (a != 0) {
        if (b < a) {
          x++;
        }
        if (c < a) {
          y++;
        }
        if (d == a) {
          z++;
        }
        if (a == e) {
          equal++;
        }
      } else {
        notReachable++;
      }
      counter++;
    }
    System.out.println("Cannot reach target: " + notReachable);
    System.out.println("Small G faster: " + x);
    System.out.println("Backward faster: " + y);
    System.out.println("Adaptive the same as forward: " + z);
    // System.out.println("Equal: " + equal);
    System.out.println();
    System.out.println();
  }
예제 #15
0
파일: Robot.java 프로젝트: eric62451/aaaaa
  public void move() {
    int newRow = nextRow(row, direction);
    int newColumn = nextColumn(column, direction);
    if (maze.isValid(newRow, newColumn)) {
      maze.updateRobot(row, column, -1);

      row = newRow;
      column = newColumn;
      maze.updateRobot(row, column, +1);

      visited.add(getPosition());
      if (debug) {
        maze.print();
      }
    }
  }
예제 #16
0
파일: Robot.java 프로젝트: eric62451/aaaaa
 public Robot(Maze aMaze, int startingRow, int startingColumn) {
   maze = aMaze;
   row = startingRow;
   column = startingColumn;
   visited = new ArrayList<String>();
   visited.add(getPosition());
   maze.updateRobot(startingRow, startingColumn, 1);
 }
 // Method to find the closest legal gem's position
 public static MazePosition findClosestLegalGem(
     MazePosition self, ArrayList<MazePosition> jewels, MazeView maze) {
   MazePosition safety = new MazePosition(self.row, self.col);
   int min = maze.getDepth() * maze.getWidth();
   for (int i = 0; i < jewels.size(); i++) {
     int endX = jewels.get(i).row * 2 - 1;
     int endY = jewels.get(i).col * 2 - 1;
     int begX = self.row * 2 - 1;
     int begY = self.col * 2 - 1;
     if (Maze.solve(begX, begY, endX, endY, maze) == true) {
       if (Maze.trace(begX, begY, endX, endY, maze).size() < min) {
         min = Maze.trace(begX, begY, endX, endY, maze).size();
         safety = new MazePosition(jewels.get(i).row, jewels.get(i).col);
       }
     }
   }
   return safety;
 }
예제 #18
0
  /**
   * Move the client backward.
   *
   * @return <code>true</code> if move was successful, otherwise <code>false</code>.
   */
  protected boolean backup() {
    assert (maze != null);

    if (maze.moveClientBackward(this)) {
      notifyMoveBackward();
      return true;
    } else {
      return false;
    }
  }
예제 #19
0
 /**
  * Cache the {@link Line2D} objects making up the maze.
  *
  * @param x Initial x coordinate.
  * @param y Initial y coordinate.
  * @param width Width of a cell.
  * @param height Height of a cell.
  */
 private void buildWalls(double x, double y, double width, double height) {
   Point p = maze.getSize();
   wallList = new ArrayList(p.getX() * p.getY());
   for (int i = 0; i < p.getY(); i++) {
     for (int j = 0; j < p.getX(); j++) {
       Cell cell = maze.getCell(new Point(j, i));
       if (cell.isWall(Direction.North)) {
         wallList.add(
             new Line2D.Double(
                 x + j * width, y + (i + 1) * height, x + (j + 1) * width, y + (i + 1) * height));
       }
       if (cell.isWall(Direction.East)) {
         wallList.add(
             new Line2D.Double(
                 x + (j + 1) * width, y + i * height, x + (j + 1) * width, y + (i + 1) * height));
       }
     }
   }
 }
예제 #20
0
  /**
   * Update a projectile.
   *
   * @return <code>true</code> if a projectile was successfully launched, otherwise <code>false
   *     </code>.
   */
  protected boolean updateProjectile() {
    assert (maze != null);

    if (maze.updateProjectile(this)) {
      notifyUpdateProjectile();
      return true;
    } else {
      return false;
    }
  }
예제 #21
0
  /**
   * Fire a projectile.
   *
   * @return <code>true</code> if a projectile was successfully launched, otherwise <code>false
   *     </code>.
   */
  protected boolean fire() {
    assert (maze != null);

    if (maze.clientFire(this)) {
      notifyFire();
      return true;
    } else {
      return false;
    }
  }
예제 #22
0
  /**
   * Move the client forward.
   *
   * @return <code>true</code> if move was successful, otherwise <code>false</code>.
   */
  protected boolean forward() {
    assert (maze != null);

    if (maze.moveClientForward(this)) {
      notifyMoveForward();
      return true;
    } else {
      return false;
    }
  }
예제 #23
0
 protected void breakWall(int aMazeX, int aMazeY, int aMazeZ, int aMazeDirection) {
   int lDx = fMaze.getDeltaX(aMazeDirection);
   int lDy = fMaze.getDeltaY(aMazeDirection);
   int lDz = fMaze.getDeltaZ(aMazeDirection);
   // normaler gang?
   if (lDy == 0) {
     if (lDz == 0) {
       for (int wd = 0; wd < corridorWidth; wd++) {
         for (int w = 1; w <= wallThickness; w++) {
           for (int y = 0; y < corridorHeight; y++) {
             area.get(getX(aMazeX) + w * lDx, getY(aMazeY) + y, getZ(aMazeZ) + wd).id =
                 Material.AIR.getId();
           }
         }
       }
     } else {
       for (int wd = 0; wd < corridorWidth; wd++) {
         for (int w = 1; w <= wallThickness; w++) {
           for (int y = 0; y < corridorHeight; y++) {
             area.get(getX(aMazeX) + wd, getY(aMazeY) + y, getZ(aMazeZ) + w * lDz).id =
                 Material.AIR.getId();
           }
         }
       }
     }
   } else {
     if (upDownUseCorridorWidth) {
       for (int wz = 0; wz < corridorWidth; wz++) {
         for (int wx = 0; wx < corridorWidth; wx++) {
           for (int w = 1; w <= ceilingThickness; w++) {
             area.get(getX(aMazeX) + wx, getY(aMazeY) + lDy * w, getZ(aMazeZ) + wz).id =
                 Material.AIR.getId();
           }
         }
       }
     } else {
       for (int w = 1; w <= ceilingThickness; w++) {
         area.get(getX(aMazeX), getY(aMazeY) + lDy * w, getZ(aMazeZ)).id = Material.AIR.getId();
       }
     }
   }
 }
 /**
  * Push information on progress into maze such that UI can update progress bar
  *
  * @param partiters
  */
 private void updateProgressBar(int partiters) {
   // During maze generation, the most time consuming part needs to occasionally update the current
   // screen
   //
   if (maze.increasePercentage(partiters * 100 / expectedPartiters)) {
     // give main thread a chance to process keyboard events
     try {
       Thread.currentThread().sleep(10);
     } catch (Exception e) {
     }
   }
 }
예제 #25
0
 /**
  * Helper method for redraw to draw screen during phase of maze generation, screen is hard coded
  * only attribute percentdone is dynamic
  *
  * @param gc graphics is the off screen image
  */
 void redrawGenerating(Graphics gc) {
   gc.setColor(Color.yellow);
   gc.fillRect(0, 0, Constants.VIEW_WIDTH, Constants.VIEW_HEIGHT);
   gc.setFont(largeBannerFont);
   FontMetrics fm = gc.getFontMetrics();
   gc.setColor(Color.red);
   centerString(gc, fm, "Building maze", 150);
   gc.setFont(smallBannerFont);
   fm = gc.getFontMetrics();
   gc.setColor(Color.black);
   centerString(gc, fm, maze.getPercentDone() + "% completed", 200);
   centerString(gc, fm, "Hit escape to stop", 300);
 }
예제 #26
0
파일: TestCases.java 프로젝트: ethanz/Maze
 public int testTiebreak(PrintWriter writer) {
   int expanded = search.run(cells, false, true, false);
   maze.printMaze("PreferSmallG.txt");
   if (expanded == 0) {
     writer.println("Repeated A* by preferring smaller g value cannot reach the target.");
     writer.println();
   } else {
     writer.println("Repeated A* by preferring smaller g value have reached the target.");
     writer.println(expanded + " cells expanded.");
     writer.println();
   }
   return expanded;
 }
예제 #27
0
파일: TestCases.java 프로젝트: ethanz/Maze
 public int testAdaptive(PrintWriter writer) {
   int expanded = search.run(cells, true, true, true);
   maze.printMaze("AdaptiveAStar.txt");
   if (expanded == 0) {
     writer.println("Adaptive A* cannot reach the target.");
     writer.println();
   } else {
     writer.println("Adaptive A* have reached the target.");
     writer.println(expanded + " cells expanded.");
     writer.println();
   }
   return expanded;
 }
예제 #28
0
파일: TestCases.java 프로젝트: ethanz/Maze
 public int runAStar(PrintWriter writer) {
   int expanded = search.run(cells, true, true, false);
   maze.printMaze("RepeatedAStar.txt");
   if (expanded == 0) {
     writer.println("Repeated A* cannot reach the target.");
     writer.println();
   } else {
     writer.println("Repeated A* have reached the target.");
     writer.println(expanded + " cells expanded.");
     writer.println();
   }
   return expanded;
 }
예제 #29
0
파일: Demo2.java 프로젝트: jeshua/me.jeshua
  public static void main(String[] args) throws InterruptedException {

    Maze maze = new Maze(new int[][] {{0}, {0}, {0}, {0}});
    maze.setCell(0, 3, Maze.G); // put goal in bottom right

    // "real" world
    Random rand1 = new Random();
    DemoSim.maze = maze;
    DemoSim simReal = new DemoSim(rand1);
    DemoSim.num_actions = 2;

    // simulator for planning
    Random rand2 = new Random();
    DemoSim simPlan = new DemoSim(rand2);
    DemoSim.num_actions = 2;

    int trajectories = 200;
    int depth = 4;
    UCT planner = new UCT(simPlan, trajectories, depth, simPlan.getDiscountFactor(), rand2);
    planner.ucbScaler = 1;
    planner.planAndAct(simReal.getState());
    VisualizeUCT.vis(planner);
  }
예제 #30
0
  public static void main(String[] args) {
    for (int i = 0; i < Integer.parseInt(args[1]); i++) {
      int startR, startC, endR, endC;
      boolean[][] maze = RandomMaze.getMaze(Integer.parseInt(args[0]));

      do {
        startR = (int) (Math.random() * Integer.parseInt(args[0]));
        startC = (int) (Math.random() * Integer.parseInt(args[0]));
        endR = (int) (Math.random() * Integer.parseInt(args[0]));
        endC = (int) (Math.random() * Integer.parseInt(args[0]));
      } while (!maze[startR][startC] || !maze[endR][endC]);

      draw(maze, Maze.path(maze, startR, startC, endR, endC), startR, startC, endR, endC);
    }
  }