Commit 775ea70

mo khan <mo@mokhan.ca>
2019-08-05 19:46:34
move printGrid to Robot
1 parent 1f5a164
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);
     }
 
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);
-  }
 }
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;
+  }
 }
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);
+  }
+}