diff options
| author | mo khan <mo@mokhan.ca> | 2019-08-05 13:46:34 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2019-08-05 13:46:34 -0600 |
| commit | 775ea70300ff438c0cf4179691253f413dfa0871 (patch) | |
| tree | ef7a335efb15e769a748113b977d1cfec22d6ccd /src | |
| parent | 1f5a1647eb36793e06b9136c877941e626d4221b (diff) | |
move printGrid to Robot
Diffstat (limited to 'src')
| -rw-r--r-- | src/Q9/MovingRobot.java | 29 | ||||
| -rw-r--r-- | src/Q9/MovingRobotTest.java | 23 | ||||
| -rw-r--r-- | src/Q9/Robot.java | 19 | ||||
| -rw-r--r-- | src/Q9/RobotTest.java | 45 |
4 files changed, 70 insertions, 46 deletions
diff --git a/src/Q9/MovingRobot.java b/src/Q9/MovingRobot.java index 334041d..03d67c9 100644 --- a/src/Q9/MovingRobot.java +++ b/src/Q9/MovingRobot.java @@ -5,9 +5,11 @@ import java.util.*; public class MovingRobot extends Robot { private ArrayList<Integer> moves = new ArrayList<Integer>(); private int nextMove; + private Random rng; public MovingRobot(int x, int y) { super(x, y); + this.rng = new Random(); } public boolean validateNextMove() { @@ -31,34 +33,20 @@ public class MovingRobot extends Robot { case Robot.WEST: return canMoveWest(); case Robot.NORTH_WEST: - return canMoveWest() && canMoveNorth(); + return canMoveNorth() && canMoveWest(); default: return false; } } public int generateNextMove() { - return new Random().nextInt(7) + 1; + return this.rng.nextInt(7) + 1; } public static boolean sameSlot(Robot r1, Robot r2) { return r1.getX() == r2.getX() && r1.getY() == r2.getY(); } - public static String printGrid(Robot r1, Robot r2) { - String grid = ""; - for (int row = 0; row < 10; row++) { - for (int column = 0; column < 10; column++) { - if (r1.getX() == row && r1.getY() == column) grid += "|1"; - else if (r2.getX() == row && r2.getY() == column) grid += "|2"; - else grid += "| "; - } - grid += String.format("|%s", System.lineSeparator()); - } - - return grid; - } - public String printMoves() { ArrayList<String> printableMoves = new ArrayList<String>(); for (Integer move : this.moves) printableMoves.add(String.valueOf(move)); @@ -66,10 +54,11 @@ public class MovingRobot extends Robot { } public void move() { - this.nextMove = generateNextMove(); - if (!validateNextMove(this.nextMove)) this.move(); + int direction = generateNextMove(); + // if (!validateNextMove(direction)) this.move(); - this.move(this.nextMove); + // this.nextMove = direction; + this.move(direction); } public void move(int direction) { @@ -153,7 +142,7 @@ public class MovingRobot extends Robot { clear(); System.out.println( String.format("R1 (%d, %d), R2: (%d, %d)", r1.getX(), r1.getY(), r2.getX(), r2.getY())); - System.out.println(MovingRobot.printGrid(r1, r2)); + System.out.println(Robot.printGrid(r1, r2)); sleep(1000); } diff --git a/src/Q9/MovingRobotTest.java b/src/Q9/MovingRobotTest.java index 0ac68cb..0db0760 100644 --- a/src/Q9/MovingRobotTest.java +++ b/src/Q9/MovingRobotTest.java @@ -179,27 +179,4 @@ public class MovingRobotTest extends TestCase { subject.move(Robot.RIGHT); assertEquals(String.format("%d,%d", Robot.RIGHT, Robot.RIGHT), subject.printMoves()); } - - public void test_printGrid() { - Robot r1 = new MovingRobot(0, 0); - Robot r2 = new MovingRobot(9, 9); - - String expected = - "|1| | | | | | | | | |\n" - + "| | | | | | | | | | |\n" - + "| | | | | | | | | | |\n" - + "| | | | | | | | | | |\n" - + "| | | | | | | | | | |\n" - + "| | | | | | | | | | |\n" - + "| | | | | | | | | | |\n" - + "| | | | | | | | | | |\n" - + "| | | | | | | | | | |\n" - + "| | | | | | | | | |2|\n"; - String result = MovingRobot.printGrid(r1, r2); - - System.out.println(expected); - System.out.println(result); - - assertEquals(expected, result); - } } diff --git a/src/Q9/Robot.java b/src/Q9/Robot.java index 69ef0a5..ddefd36 100644 --- a/src/Q9/Robot.java +++ b/src/Q9/Robot.java @@ -1,9 +1,6 @@ package Q9; public class Robot { - private int x; - private int y; - public static final int UP = 1; public static final int DOWN = 2; public static final int LEFT = 3; @@ -22,6 +19,9 @@ public class Robot { public static final int WEST = LEFT; public static final int NORTH_WEST = LEFT_UP_CORNER; + private int x; + private int y; + public Robot(int x, int y) { this.x = x; this.y = y; @@ -42,4 +42,17 @@ public class Robot { public void setY(int y) { this.y = y; } + + public static String printGrid(Robot r1, Robot r2) { + String grid = ""; + for (int row = 0; row < 10; row++) { + for (int column = 0; column < 10; column++) { + if (r1.getX() == row && r1.getY() == column) grid += "|1"; + else if (r2.getX() == row && r2.getY() == column) grid += "|2"; + else grid += "| "; + } + grid += String.format("|%s", System.lineSeparator()); + } + return grid; + } } diff --git a/src/Q9/RobotTest.java b/src/Q9/RobotTest.java new file mode 100644 index 0000000..9290993 --- /dev/null +++ b/src/Q9/RobotTest.java @@ -0,0 +1,45 @@ +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 RobotTest extends TestCase { + private Robot subject; + + public RobotTest(String testName) { + super(testName); + this.subject = new Robot(0, 0); + } + + public static Test suite() { + return new TestSuite(RobotTest.class); + } + + public void test_printGrid() { + Robot r1 = new Robot(0, 0); + Robot r2 = new Robot(9, 9); + + String expected = + "|1| | | | | | | | | |\n" + + "| | | | | | | | | | |\n" + + "| | | | | | | | | | |\n" + + "| | | | | | | | | | |\n" + + "| | | | | | | | | | |\n" + + "| | | | | | | | | | |\n" + + "| | | | | | | | | | |\n" + + "| | | | | | | | | | |\n" + + "| | | | | | | | | | |\n" + + "| | | | | | | | | |2|\n"; + String result = Robot.printGrid(r1, r2); + + System.out.println(expected); + System.out.println(result); + + assertEquals(expected, result); + } +} |
