summaryrefslogtreecommitdiff
path: root/src/Q9/MovingRobot.java
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2019-08-05 12:20:49 -0600
committermo khan <mo@mokhan.ca>2019-08-05 12:20:49 -0600
commit6eced4703a3e6e71c374af65975a1fe1d760841c (patch)
tree8899dda2b7eaf2bfe607497feb876e80b7f28685 /src/Q9/MovingRobot.java
parent40285a06ec3f5ca67e4bf504b716b5828035c0e9 (diff)
move robots on a grid
Diffstat (limited to 'src/Q9/MovingRobot.java')
-rw-r--r--src/Q9/MovingRobot.java114
1 files changed, 107 insertions, 7 deletions
diff --git a/src/Q9/MovingRobot.java b/src/Q9/MovingRobot.java
index 958c757..a7b462c 100644
--- a/src/Q9/MovingRobot.java
+++ b/src/Q9/MovingRobot.java
@@ -2,27 +2,127 @@ package Q9;
import java.util.*;
-public class MovingRobot {
+public class MovingRobot extends Robot {
private ArrayList<Integer> moves = new ArrayList<Integer>();
private int nextMove;
- public MovingRobot(int x, int y) {}
+ public MovingRobot(int x, int y) {
+ super(x, y);
+ }
public boolean validateNextMove() {
- return false;
+ return validateNextMove(nextMove);
+ }
+
+ public boolean validateNextMove(int direction) {
+ switch (direction) {
+ case Robot.UP:
+ return canMoveUp();
+ case Robot.RIGHT_UP_CORNER:
+ return canMoveUp() && canMoveRight();
+ case Robot.RIGHT:
+ return canMoveRight();
+ case Robot.RIGHT_DOWN_CORNER:
+ return canMoveDown() && canMoveRight();
+ case Robot.DOWN:
+ return canMoveDown();
+ case Robot.LEFT_DOWN_CORNER:
+ return canMoveDown() && canMoveLeft();
+ case Robot.LEFT:
+ return canMoveLeft();
+ case Robot.LEFT_UP_CORNER:
+ return canMoveLeft() && canMoveUp();
+ default:
+ return false;
+ }
}
public int generateNextMove() {
- return 0;
+ return new Random().nextInt(7) + 1;
}
public static boolean sameSlot(Robot r1, Robot r2) {
- return false;
+ return r1.getX() == r2.getX() && r1.getY() == r2.getY();
}
public String printMoves() {
- return "";
+ String printer = "";
+ for (Integer move : this.moves) printer += String.format("%d", move);
+ return printer;
+ }
+
+ public void move() {
+ this.nextMove = generateNextMove();
+ this.move(this.nextMove);
+ }
+
+ public void move(int direction) {
+ if (!validateNextMove(direction)) return;
+
+ switch (direction) {
+ case Robot.UP:
+ moveUp();
+ break;
+ case Robot.RIGHT_UP_CORNER:
+ moveUp();
+ moveRight();
+ break;
+ case Robot.RIGHT:
+ moveRight();
+ break;
+ case Robot.RIGHT_DOWN_CORNER:
+ moveDown();
+ moveRight();
+ break;
+ case Robot.DOWN:
+ moveDown();
+ break;
+ case Robot.LEFT_DOWN_CORNER:
+ moveDown();
+ moveLeft();
+ break;
+ case Robot.LEFT:
+ moveLeft();
+ break;
+ case Robot.LEFT_UP_CORNER:
+ moveLeft();
+ moveUp();
+ break;
+ default:
+ return;
+ }
+ moves.add(direction);
+ }
+
+ private boolean canMoveUp() {
+ return this.getY() > 0;
+ }
+
+ private boolean canMoveDown() {
+ return this.getY() < 9;
}
- public void move() {}
+ private boolean canMoveRight() {
+ return this.getX() < 9;
+ }
+
+ private boolean canMoveLeft() {
+ return this.getX() > 0;
+ }
+
+ private void moveUp() {
+ this.setY(this.getY() - 1);
+ }
+
+ private void moveDown() {
+ this.setY(this.getY() + 1);
+ }
+
+ private void moveRight() {
+ this.setX(this.getX() + 1);
+ }
+
+ private void moveLeft() {
+ this.setX(this.getX() - 1);
+ }
}