Пример #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!");
 }
Пример #2
0
 @Test
 public void test_example_c() {
   robot.place(1, 2, DirectionKind.EAST);
   robot.move();
   robot.move();
   robot.left();
   robot.move();
   org.junit.Assert.assertEquals("3,3,NORTH", robot.report());
 }
Пример #3
0
 @Test
 public void testTomoveRobotFacingEastAt00() {
   DIRECTIONS pos = DIRECTIONS.valueOf("EAST");
   xy.setXY(0, 0);
   robot.place(pos, xy);
   robot.move();
   assertEquals("1,0 EAST", robot.report());
 }
Пример #4
0
 @Test
 public void testTomoveRobotFacingSouthAt00() {
   DIRECTIONS pos = DIRECTIONS.valueOf("SOUTH");
   xy.setXY(0, 0);
   robot.place(pos, xy);
   robot.move();
   assertEquals("0,-1 SOUTH", robot.report());
 }
Пример #5
0
 public void moveDown() {
   try {
     robot.move(-1);
     updateHandlerProgress();
   } catch (BadMoveException e) {
     Log.v("debug", "Hey man, there's a wall!");
   }
 }
 public static void main(String[] args) {
   World.reset();
   World.setTrace(true);
   World.readWorld("stairworld.kwld");
   World.setVisible(true);
   Robot karel = new Robot(1, 1, North, 3);
   karel.move();
   karel.putBeeper();
   karel.turnLeft();
   karel.turnLeft();
   karel.turnLeft();
   karel.move();
   karel.turnLeft();
   karel.move();
   karel.turnLeft();
   karel.turnLeft();
   karel.turnLeft();
   karel.move();
   karel.turnLeft();
   karel.move();
   karel.turnLeft();
   karel.turnLeft();
   karel.turnLeft();
   karel.move();
   karel.turnLeft();
 }
  public static void main(String[] args) {
    Robot sam = new Robot();

    sam.speak("Hi I'm Sam");
    sam.jump(7);

    sam.move("West", 12.2); // type, order and number matters

    String greeting =
        "Hello there"; // greeting variable is like a sign that refers to string "hello there"
    sam.speak(greeting); // greeting -> "Hello there" <- text

    int value = 14; // value variable stores the data 14
    sam.jump(value); // value = 14 -> height
  }
Пример #8
0
 public void escape() {
   if (atExit()) {
     return;
   }
   turnRight();
   if (!canMove()) {
     turnLeft();
   }
   if (canMove()) {
     move();
   } else {
     turnLeft();
   }
   escape();
 }
Пример #9
0
 public boolean escape2() {
   {
     if (atExit()) {
       return true;
     }
     for (int i = 0; i < 4; i++) {
       turnRight();
       if (canMove()) {
         Robot cloned = clone();
         cloned.move();
         if (cloned.escape2()) {
           visited = cloned.getVisited();
           return true;
         }
       }
     }
     return false;
   }
 }
Пример #10
0
 public static void main(String[] args) {
   Robot robot = new Robot();
   robot.move(Direction.left);
 }
Пример #11
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);
  }
Пример #12
0
 @Test
 public void test_example_a() {
   robot.place(0, 0, DirectionKind.NORTH);
   robot.move();
   org.junit.Assert.assertEquals("0,1,NORTH", robot.report());
 }