/**
  * 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];
 }
Ejemplo n.º 2
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]);
  }
Ejemplo n.º 3
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);
  }
 // 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();
 }
Ejemplo n.º 5
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);
  }
Ejemplo n.º 6
0
  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);
    }
  }
 // 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;
 }