From 6eced4703a3e6e71c374af65975a1fe1d760841c Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 5 Aug 2019 12:20:49 -0600 Subject: move robots on a grid --- src/Q9/MovingRobot.java | 114 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 7 deletions(-) (limited to 'src/Q9/MovingRobot.java') 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 moves = new ArrayList(); 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); + } } -- cgit v1.2.3