diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Q9/MovingRobot.java | 114 | ||||
| -rw-r--r-- | src/Q9/MovingRobotTest.java | 136 | ||||
| -rw-r--r-- | src/Q9/README.md | 20 |
3 files changed, 253 insertions, 17 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); + } } diff --git a/src/Q9/MovingRobotTest.java b/src/Q9/MovingRobotTest.java new file mode 100644 index 0000000..68feddd --- /dev/null +++ b/src/Q9/MovingRobotTest.java @@ -0,0 +1,136 @@ +package ca.mokhan.test; + +import Q9.*; +import java.io.*; +import java.text.*; +import java.util.*; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class MovingRobotTest extends TestCase { + private MovingRobot subject; + + public MovingRobotTest(String testName) { + super(testName); + this.subject = new MovingRobot(0, 0); + } + + public static Test suite() { + return new TestSuite(MovingRobotTest.class); + } + + public void test_generateNextMove() { + for (int i = 0; i < 100; i++) { + int move = subject.generateNextMove(); + assertTrue(move > 0); + assertTrue(move < 9); + } + } + + public void test_validateNextMove_whenAtZeroZero() { + this.subject = new MovingRobot(0, 0); + assertFalse(subject.validateNextMove(Robot.UP)); + assertFalse(subject.validateNextMove(Robot.RIGHT_UP_CORNER)); + assertTrue(subject.validateNextMove(Robot.RIGHT)); + assertTrue(subject.validateNextMove(Robot.RIGHT_DOWN_CORNER)); + assertTrue(subject.validateNextMove(Robot.DOWN)); + assertFalse(subject.validateNextMove(Robot.LEFT_DOWN_CORNER)); + assertFalse(subject.validateNextMove(Robot.LEFT)); + assertFalse(subject.validateNextMove(Robot.LEFT_UP_CORNER)); + } + + public void test_validateNextMove_whenAtZeroOne() { + this.subject = new MovingRobot(0, 1); + + assertTrue(subject.validateNextMove(Robot.UP)); + assertTrue(subject.validateNextMove(Robot.RIGHT_UP_CORNER)); + assertTrue(subject.validateNextMove(Robot.RIGHT)); + assertTrue(subject.validateNextMove(Robot.RIGHT_DOWN_CORNER)); + assertTrue(subject.validateNextMove(Robot.DOWN)); + assertFalse(subject.validateNextMove(Robot.LEFT_DOWN_CORNER)); + assertFalse(subject.validateNextMove(Robot.LEFT)); + assertFalse(subject.validateNextMove(Robot.LEFT_UP_CORNER)); + } + + public void test_validateNextMove_whenAtNineZero() { + this.subject = new MovingRobot(9, 0); + + assertFalse(subject.validateNextMove(Robot.UP)); + assertFalse(subject.validateNextMove(Robot.RIGHT_UP_CORNER)); + assertFalse(subject.validateNextMove(Robot.RIGHT)); + assertFalse(subject.validateNextMove(Robot.RIGHT_DOWN_CORNER)); + assertTrue(subject.validateNextMove(Robot.DOWN)); + assertTrue(subject.validateNextMove(Robot.LEFT_DOWN_CORNER)); + assertTrue(subject.validateNextMove(Robot.LEFT)); + assertFalse(subject.validateNextMove(Robot.LEFT_UP_CORNER)); + } + + public void test_validateNextMove_whenAtNineNine() { + this.subject = new MovingRobot(9, 9); + assertTrue(subject.validateNextMove(Robot.UP)); + assertFalse(subject.validateNextMove(Robot.RIGHT_UP_CORNER)); + assertFalse(subject.validateNextMove(Robot.RIGHT)); + assertFalse(subject.validateNextMove(Robot.RIGHT_DOWN_CORNER)); + assertFalse(subject.validateNextMove(Robot.DOWN)); + assertFalse(subject.validateNextMove(Robot.LEFT_DOWN_CORNER)); + assertTrue(subject.validateNextMove(Robot.LEFT)); + assertTrue(subject.validateNextMove(Robot.LEFT_UP_CORNER)); + } + + public void test_validateNextMove_whenAtZeroNine() { + this.subject = new MovingRobot(0, 9); + assertTrue(subject.validateNextMove(Robot.UP)); + assertTrue(subject.validateNextMove(Robot.RIGHT_UP_CORNER)); + assertTrue(subject.validateNextMove(Robot.RIGHT)); + assertFalse(subject.validateNextMove(Robot.RIGHT_DOWN_CORNER)); + assertFalse(subject.validateNextMove(Robot.DOWN)); + assertFalse(subject.validateNextMove(Robot.LEFT_DOWN_CORNER)); + assertFalse(subject.validateNextMove(Robot.LEFT)); + assertFalse(subject.validateNextMove(Robot.LEFT_UP_CORNER)); + } + + public void test_move_whenAtZeroZero() { + subject = new MovingRobot(0, 0); + + for (int i = 0; i < 9; i++) { + subject.move(Robot.RIGHT); + assertEquals(i + 1, subject.getX()); + assertEquals(0, subject.getY()); + } + + for (int i = 0; i < 9; i++) { + subject.move(Robot.DOWN); + assertEquals(9, subject.getX()); + assertEquals(i + 1, subject.getY()); + } + + for (int i = 9; i > 0; i--) { + subject.move(Robot.LEFT); + assertEquals(i - 1, subject.getX()); + assertEquals(9, subject.getY()); + } + + for (int i = 9; i > 0; i--) { + subject.move(Robot.UP); + assertEquals(0, subject.getX()); + assertEquals(i - 1, subject.getY()); + } + + subject.move(Robot.UP); + assertEquals(0, subject.getX()); + assertEquals(0, subject.getY()); + + subject.move(Robot.LEFT_UP_CORNER); + assertEquals(0, subject.getX()); + assertEquals(0, subject.getY()); + + subject.move(Robot.LEFT); + assertEquals(0, subject.getX()); + assertEquals(0, subject.getY()); + + subject.move(Robot.LEFT_DOWN_CORNER); + assertEquals(0, subject.getX()); + assertEquals(0, subject.getY()); + } +} diff --git a/src/Q9/README.md b/src/Q9/README.md index 8bfdfbf..b6a8a1f 100644 --- a/src/Q9/README.md +++ b/src/Q9/README.md @@ -9,16 +9,16 @@ | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | - | 0 | [0, 0] | [0, 1] | [0, 2] | [0, 3] | [0, 4] | [0, 5] | [0, 6] | [0, 7] | [0, 8] | [0, 9] | - | 1 | [1, 0] | [1, 1] | [1, 2] | [1, 3] | [1, 4] | [1, 5] | [1, 6] | [1, 7] | [1, 8] | [1, 9] | - | 2 | [2, 0] | [2, 1] | [2, 2] | [2, 3] | [2, 4] | [2, 5] | [2, 6] | [2, 7] | [2, 8] | [2, 9] | - | 3 | [3, 0] | [3, 1] | [3, 2] | [3, 3] | [3, 4] | [3, 5] | [3, 6] | [3, 7] | [3, 8] | [3, 9] | - | 4 | [4, 0] | [4, 1] | [4, 2] | [4, 3] | [4, 4] | [4, 5] | [4, 6] | [4, 7] | [4, 8] | [4, 9] | - | 5 | [5, 0] | [5, 1] | [5, 2] | [5, 3] | [5, 4] | [5, 5] | [5, 6] | [5, 7] | [5, 8] | [5, 9] | - | 6 | [6, 0] | [6, 1] | [6, 2] | [6, 3] | [6, 4] | [6, 5] | [6, 6] | [6, 7] | [6, 8] | [6, 9] | - | 7 | [7, 0] | [7, 1] | [7, 2] | [7, 3] | [7, 4] | [7, 5] | [7, 6] | [7, 7] | [7, 8] | [7, 9] | - | 8 | [8, 0] | [8, 1] | [8, 2] | [8, 3] | [8, 4] | [8, 5] | [8, 6] | [8, 7] | [8, 8] | [8, 9] | - | 9 | [9, 0] | [9, 1] | [9, 2] | [9, 3] | [9, 4] | [9, 5] | [9, 6] | [9, 7] | [9, 8] | [9, 9] | + | 0 | [0, 0] | [1, 0] | [2, 0] | [3, 0] | [4, 0] | [5, 0] | [6, 0] | [7, 0] | [8, 0] | [9, 0] | + | 1 | [0, 1] | [1, 1] | [2, 1] | [3, 1] | [4, 1] | [5, 1] | [6, 1] | [7, 1] | [8, 1] | [9, 1] | + | 2 | [0, 2] | [1, 2] | [2, 2] | [3, 2] | [4, 2] | [5, 2] | [6, 2] | [7, 2] | [8, 2] | [9, 2] | + | 3 | [0, 3] | [1, 3] | [2, 3] | [3, 3] | [4, 3] | [5, 3] | [6, 3] | [7, 3] | [8, 3] | [9, 3] | + | 4 | [0, 4] | [1, 4] | [2, 4] | [3, 4] | [4, 4] | [5, 4] | [6, 4] | [7, 4] | [8, 4] | [9, 4] | + | 5 | [0, 5] | [1, 5] | [2, 5] | [3, 5] | [4, 5] | [5, 5] | [6, 5] | [7, 5] | [8, 5] | [9, 5] | + | 6 | [0, 6] | [1, 6] | [2, 6] | [3, 6] | [4, 6] | [5, 6] | [6, 6] | [7, 6] | [8, 6] | [9, 6] | + | 7 | [0, 7] | [1, 7] | [2, 7] | [3, 7] | [4, 7] | [5, 7] | [6, 7] | [7, 7] | [8, 7] | [9, 7] | + | 8 | [0, 8] | [1, 8] | [2, 8] | [3, 8] | [4, 8] | [5, 8] | [6, 8] | [7, 8] | [8, 8] | [9, 8] | + | 9 | [0, 9] | [1, 9] | [2, 9] | [3, 9] | [4, 9] | [5, 9] | [6, 9] | [7, 9] | [8, 9] | [9, 9] | Assume that a robot is placed in position [0, 0]. Now randomly generate a move. The move could take the robot to one of the eight possible adjacent slots: |
