Beispiel #1
0
 // This is where the shit really goes down
 public static void moveThroughMaze(Room room, Robot robby, Picture pic) {
   while (!(robby.get_xpos() == destX) || !(robby.get_ypos() == destY)) {
     if (destX > robby.get_xpos()) {
       robby.face_right();
       if (robby.ahead_is_colour(room, Room.WHITE)) {
         robby.move();
         pic.draw(room, robby);
       } else {
         if (destY > robby.get_ypos()) {
           robby.face_up();
           if (!robby.ahead_is_colour(room, Room.WHITE)) {
             robby.face_left();
             if (!robby.ahead_is_colour(room, Room.WHITE)) {
               robby.face_down();
               robby.move();
               pic.draw(room, robby);
             } else {
               robby.move();
               pic.draw(room, robby);
             }
           } else {
             robby.move();
             pic.draw(room, robby);
           }
         }
       }
     } else {
       if (destY > robby.get_ypos()) {
         robby.face_up();
       }
     }
   }
   System.out.println("Success!");
 }
Beispiel #2
0
  public static void aStar(Room room, Robot robby, Picture pic) {
    boolean[][] openClosed = new boolean[20][20];
    openClosed[1][1] = true; // open is true, closed if false
    while (!robby.ahead_is_colour(room, Room.GREEN)) {
      int[][] fScore = new int[20][20];
      // Set values of robby x and y
      int x = robby.get_xpos();
      int y = robby.get_ypos();

      // Check node to N of robby
      if (room.cell_state(x, y + 1) == Room.WHITE /*|| room.cell_state(x, y + 1) == Room.YELLOW*/) {
        openClosed[x][y + 1] = true;
        fScore[x][y + 1] = 10 + calcGH(x, y + 1); // G + H
      } else {
        openClosed[x][y + 1] = false;
        fScore[x][y + 1] = 100000;
      }
      // Check node to NE of robby
      if (room.cell_state(x + 1, y + 1)
          == Room.WHITE /*|| room.cell_state(x + 1, y + 1) == Room.YELLOW*/) {
        openClosed[x + 1][y + 1] = true;
        fScore[x + 1][y + 1] = 100000;
        // fScore[x+1][y+1] = 14 + Math.abs(((destX + destY) - ((x+1) + (y+1)))*10); //G + H
      } else {
        openClosed[x + 1][y + 1] = false;
        fScore[x + 1][y + 1] = 100000;
      }
      // Check node to E of robby
      if (room.cell_state(x + 1, y) == Room.WHITE /*|| room.cell_state(x + 1, y) == Room.YELLOW*/) {
        openClosed[x + 1][y] = true;
        fScore[x + 1][y] = 10 + calcGH(x + 1, y); // G + H
      } else {
        openClosed[x + 1][y] = false;
        fScore[x + 1][y] = 100000;
      }
      // Check node to SE of robby
      if (room.cell_state(x + 1, y - 1)
          == Room.WHITE /*|| room.cell_state(x + 1, y - 1) == Room.YELLOW*/) {
        openClosed[x + 1][y - 1] = true;
        fScore[x + 1][y - 1] = 100000;
        // fScore[x+1][y-1] = 14 + Math.abs(((destX + destY) - ((x+1) + (y-1)))*10); //G + H
      } else {
        openClosed[x + 1][y - 1] = false;
        fScore[x + 1][y - 1] = 100000;
      }
      // Check node to S of robby
      if (room.cell_state(x, y - 1) == Room.WHITE /*|| room.cell_state(x, y - 1) == Room.YELLOW*/) {
        openClosed[x][y - 1] = true;
        fScore[x][y - 1] = 10 + calcGH(x, y - 1); // G + H
      } else {
        openClosed[x][y - 1] = false;
        fScore[x][y - 1] = 100000;
      }
      // Check node to SW of robby
      if (room.cell_state(x - 1, y - 1)
          == Room.WHITE /*|| room.cell_state(x - 1, y - 1) == Room.YELLOW*/) {
        openClosed[x - 1][y - 1] = true;
        fScore[x - 1][y - 1] = 100000;
        // fScore[x-1][y-1] = 14 + Math.abs(((destX + destY) - ((x-1) + (y-1)))*10); //G + H
      } else {
        openClosed[x - 1][y - 1] = false;
        fScore[x - 1][y - 1] = 100000;
      }
      // Check node to W of robby
      if (room.cell_state(x - 1, y) == Room.WHITE /*|| room.cell_state(x - 1, y) == Room.YELLOW*/) {
        openClosed[x - 1][y] = true;
        fScore[x - 1][y] = 10 + calcGH(x - 1, y); // G + H
      } else {
        openClosed[x - 1][y] = false;
        fScore[x - 1][y] = 100000;
      }
      // Check node to NW of robby
      if (room.cell_state(x - 1, y + 1)
          == Room.WHITE /*|| room.cell_state(x - 1, y + 1) == Room.YELLOW*/) {
        openClosed[x - 1][y + 1] = true;
        fScore[x - 1][y + 1] = 100000;
        // fScore[x-1][y+1] = 14 + Math.abs(((destX + destY) - ((x-1) + (y+1)))*10); //G + H
      } else {
        openClosed[x - 1][y + 1] = false;
        fScore[x - 1][y + 1] = 100000;
      }
      openClosed[x][y] = false;

      // Find node with lowest F value
      int[][] smallestFValue = lowestF(openClosed, fScore, x, y);

      room.setPath(smallestFValue[0][0], smallestFValue[0][1]);
      pic.draw(room, robby);
      if (smallestFValue[0][0] > x) {
        robby.face_right();
        robby.move();
        pic.draw(room, robby);
      }
      if (smallestFValue[0][0] < x) {
        robby.face_left();
        robby.move();
        pic.draw(room, robby);
      }
      if (smallestFValue[0][1] > y) {
        robby.face_up();
        robby.move();
        pic.draw(room, robby);
      }
      if (smallestFValue[0][1] < y) {
        robby.face_down();
        robby.move();
        pic.draw(room, robby);
      }
    }
    robby.move();
    pic.draw(room, robby);
  }