Commit 886238e
Changed files (2)
src
src/Q9/MovingRobot.java
@@ -53,6 +53,8 @@ public class MovingRobot extends Robot {
public void move() {
this.nextMove = generateNextMove();
+ if (!validateNextMove(this.nextMove)) this.move();
+
this.move(this.nextMove);
}
@@ -125,4 +127,21 @@ public class MovingRobot extends Robot {
private void moveLeft() {
this.setX(this.getX() - 1);
}
+
+ public static void main(String[] args) {
+ System.out.println("=== Question 9 ===");
+ MovingRobot r1 = new MovingRobot(0, 0);
+ MovingRobot r2 = new MovingRobot(9, 9);
+ while (!MovingRobot.sameSlot(r1, r2)) {
+ r1.move();
+ r2.move();
+ }
+
+ System.out.println(String.format("Collision at: (%d, %d)", r1.getX(), r1.getY()));
+ System.out.println("R1 Route:");
+ System.out.println(r1.printMoves());
+
+ System.out.println("R2 Route:");
+ System.out.println(r2.printMoves());
+ }
}
src/App.java
@@ -38,6 +38,9 @@ public class App {
case 8:
Q8.BadmintonScoring.main(args);
break;
+ case 9:
+ Q9.MovingRobot.main(args);
+ break;
default:
System.out.println("Bye");
System.exit(0);