diff options
Diffstat (limited to 'src/Q9/MovingRobot.java')
| -rw-r--r-- | src/Q9/MovingRobot.java | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/Q9/MovingRobot.java b/src/Q9/MovingRobot.java index fa51410..21c46a1 100644 --- a/src/Q9/MovingRobot.java +++ b/src/Q9/MovingRobot.java @@ -1,3 +1,11 @@ +/** + * Assignment 2, COMP268 Class: MovingRobot.java + * + * @description A class used to move a robot on a 9x9 grid. + * @author: mo khan Student ID: 3431709 + * @date August 5, 2019 + * @version 1.0 + */ package Q9; import java.util.*; @@ -6,11 +14,23 @@ public class MovingRobot extends Robot { private ArrayList<Integer> moves = new ArrayList<Integer>(); private Random rng; + /** + * Creates an instance of a moving robot. + * + * @param x the x coordinate on the 9x9 grid + * @param y the y coordinate on the 9x9 grid + */ public MovingRobot(int x, int y) { super(x, y); this.rng = new Random(); } + /** + * Checks to see if a robot can advance in the direction provided. + * + * @param the direction that a robot wants to advance to. + * @return returns true if the robot can advance to the given direction. + */ public boolean validateNextMove(int direction) { switch (direction) { case Robot.NORTH: @@ -34,26 +54,43 @@ public class MovingRobot extends Robot { } } + /** + * @return a random direction. + */ public int generateNextMove() { return this.rng.nextInt(7) + 1; } + /** + * @return true if the two robots are in the same coordinate. + */ public static boolean sameSlot(Robot r1, Robot r2) { return r1.getX() == r2.getX() && r1.getY() == r2.getY(); } + /** + * @return The list of moves made by the robot. + */ public String printMoves() { ArrayList<String> printableMoves = new ArrayList<String>(); for (Integer move : this.moves) printableMoves.add(String.valueOf(move)); return String.join(",", printableMoves); } + /** + * Moves the robot in a random direction. + */ public void move() { int direction = generateNextMove(); while (!validateNextMove(direction)) direction = generateNextMove(); this.move(direction); } + /** + * An overload of the move method that attempts to move in the direction + * specified. + * @param direction the direction to move the robot towards. + */ public void move(int direction) { if (!validateNextMove(direction)) return; @@ -124,6 +161,10 @@ public class MovingRobot extends Robot { this.setX(this.getX() - 1); } + /** + * The entry point to the console application. + * @param args the argument vector provided to the program. + */ public static void main(String[] args) { MovingRobot r1 = new MovingRobot(0, 0); MovingRobot r2 = new MovingRobot(9, 9); |
