master
  1/**
  2 * Assignment 2, COMP268 Class: MovingRobot.java
  3 *
  4 * @description A class used to move a robot on a 9x9 grid.
  5 * @author: mo khan Student ID: 3431709
  6 * @date August 5, 2019
  7 * @version 1.0
  8 */
  9package Q9;
 10
 11import java.util.*;
 12
 13public class MovingRobot extends Robot {
 14  private ArrayList<Integer> moves = new ArrayList<Integer>();
 15  private Random rng;
 16
 17  /**
 18   * Creates an instance of a moving robot.
 19   *
 20   * @param x the x coordinate on the 9x9 grid
 21   * @param y the y coordinate on the 9x9 grid
 22   */
 23  public MovingRobot(int x, int y) {
 24    super(x, y);
 25    this.rng = new Random();
 26  }
 27
 28  /**
 29   * Checks to see if a robot can advance in the direction provided.
 30   *
 31   * @param direction the direction that a robot wants to advance to.
 32   * @return returns true if the robot can advance to the given direction.
 33   */
 34  public boolean validateNextMove(int direction) {
 35    switch (direction) {
 36      case Robot.NORTH:
 37        return canMoveNorth();
 38      case Robot.NORTH_EAST:
 39        return canMoveNorth() && canMoveEast();
 40      case Robot.EAST:
 41        return canMoveEast();
 42      case Robot.SOUTH_EAST:
 43        return canMoveSouth() && canMoveEast();
 44      case Robot.SOUTH:
 45        return canMoveSouth();
 46      case Robot.SOUTH_WEST:
 47        return canMoveSouth() && canMoveWest();
 48      case Robot.WEST:
 49        return canMoveWest();
 50      case Robot.NORTH_WEST:
 51        return canMoveNorth() && canMoveWest();
 52      default:
 53        return false;
 54    }
 55  }
 56
 57  /** @return a random direction. */
 58  public int generateNextMove() {
 59    return this.rng.nextInt(7) + 1;
 60  }
 61
 62  /**
 63   * @param r1 robot 1
 64   * @param r2 robot 2
 65   * @return true if the two robots are in the same coordinate.
 66   */
 67  public static boolean sameSlot(Robot r1, Robot r2) {
 68    return r1.getX() == r2.getX() && r1.getY() == r2.getY();
 69  }
 70
 71  /** @return The list of moves made by the robot. */
 72  public String printMoves() {
 73    ArrayList<String> printableMoves = new ArrayList<String>();
 74    for (Integer move : this.moves) printableMoves.add(String.valueOf(move));
 75    return String.join(",", printableMoves);
 76  }
 77
 78  /** Moves the robot in a random direction. */
 79  public void move() {
 80    int direction = generateNextMove();
 81    while (!validateNextMove(direction)) direction = generateNextMove();
 82    this.move(direction);
 83  }
 84
 85  /**
 86   * An overload of the move method that attempts to move in the direction specified.
 87   *
 88   * @param direction the direction to move the robot towards.
 89   */
 90  public void move(int direction) {
 91    if (!validateNextMove(direction)) return;
 92
 93    switch (direction) {
 94      case Robot.NORTH:
 95        moveNorth();
 96        break;
 97      case Robot.NORTH_EAST:
 98        moveNorth();
 99        moveEast();
100        break;
101      case Robot.EAST:
102        moveEast();
103        break;
104      case Robot.SOUTH_EAST:
105        moveSouth();
106        moveEast();
107        break;
108      case Robot.SOUTH:
109        moveSouth();
110        break;
111      case Robot.SOUTH_WEST:
112        moveSouth();
113        moveWest();
114        break;
115      case Robot.WEST:
116        moveWest();
117        break;
118      case Robot.NORTH_WEST:
119        moveWest();
120        moveNorth();
121        break;
122      default:
123        return;
124    }
125    moves.add(direction);
126  }
127
128  private boolean canMoveNorth() {
129    return this.getY() > 0;
130  }
131
132  private boolean canMoveSouth() {
133    return this.getY() < 9;
134  }
135
136  private boolean canMoveEast() {
137    return this.getX() < 9;
138  }
139
140  private boolean canMoveWest() {
141    return this.getX() > 0;
142  }
143
144  private void moveNorth() {
145    this.setY(this.getY() - 1);
146  }
147
148  private void moveSouth() {
149    this.setY(this.getY() + 1);
150  }
151
152  private void moveEast() {
153    this.setX(this.getX() + 1);
154  }
155
156  private void moveWest() {
157    this.setX(this.getX() - 1);
158  }
159
160  /**
161   * The entry point to the console application.
162   *
163   * @param args the argument vector provided to the program.
164   */
165  public static void main(String[] args) {
166    MovingRobot r1 = new MovingRobot(0, 0);
167    MovingRobot r2 = new MovingRobot(9, 9);
168
169    while (!MovingRobot.sameSlot(r1, r2)) {
170      r1.move();
171      r2.move();
172
173      clear();
174      System.out.println("==== Question  9 ====");
175      System.out.println(Robot.printGrid(r1, r2));
176      System.out.println(String.format("R1 (%d, %d): [%s]", r1.getX(), r1.getY(), r1.printMoves()));
177      System.out.println(String.format("R2 (%d, %d): [%s]", r2.getX(), r2.getY(), r2.printMoves()));
178      sleep(250);
179    }
180  }
181
182  private static void clear() {
183    try {
184      if (System.getProperty("os.name").contains("Windows")) Runtime.getRuntime().exec("cls");
185      else {
186        System.out.print("\033[H\033[2J");
187        System.out.flush();
188      }
189    } catch (Exception e) {
190    }
191  }
192
193  private static void sleep(int milliseconds) {
194    try {
195      Thread.sleep(milliseconds);
196    } catch (InterruptedException e) {
197    }
198  }
199}