/**
  * 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];
 }
Example #2
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);
  }
 // 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;
 }