package Q9; import java.util.*; public class MovingRobot extends Robot { private ArrayList moves = new ArrayList(); private int nextMove; public MovingRobot(int x, int y) { super(x, y); } public boolean validateNextMove() { return validateNextMove(nextMove); } public boolean validateNextMove(int direction) { switch (direction) { case Robot.NORTH: return canMoveNorth(); case Robot.NORTH_EAST: return canMoveNorth() && canMoveEast(); case Robot.EAST: return canMoveEast(); case Robot.SOUTH_EAST: return canMoveSouth() && canMoveEast(); case Robot.SOUTH: return canMoveSouth(); case Robot.SOUTH_WEST: return canMoveSouth() && canMoveWest(); case Robot.WEST: return canMoveWest(); case Robot.NORTH_WEST: return canMoveWest() && canMoveNorth(); default: return false; } } public int generateNextMove() { return new Random().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 x, Robot y) { String grid = ""; for (int row = 0; row < 10; row++) { for (int column = 0; column < 10; column++) { if (x.getX() == row && x.getY() == column) grid += "|X"; else if (y.getX() == row && y.getY() == column) grid += "|Y"; else grid += "| "; } grid += String.format("|%s", System.lineSeparator()); } return grid; } public String printMoves() { ArrayList printableMoves = new ArrayList(); for (Integer move : this.moves) printableMoves.add(String.valueOf(move)); return String.join(",", printableMoves); } public void move() { this.nextMove = generateNextMove(); if (!validateNextMove(this.nextMove)) this.move(); this.move(this.nextMove); } public void move(int direction) { if (!validateNextMove(direction)) return; switch (direction) { case Robot.NORTH: moveNorth(); break; case Robot.NORTH_EAST: moveNorth(); moveEast(); break; case Robot.EAST: moveEast(); break; case Robot.SOUTH_EAST: moveSouth(); moveEast(); break; case Robot.SOUTH: moveSouth(); break; case Robot.SOUTH_WEST: moveSouth(); moveWest(); break; case Robot.WEST: moveWest(); break; case Robot.NORTH_WEST: moveWest(); moveNorth(); break; default: return; } moves.add(direction); } private boolean canMoveNorth() { return this.getY() > 0; } private boolean canMoveSouth() { return this.getY() < 9; } private boolean canMoveEast() { return this.getX() < 9; } private boolean canMoveWest() { return this.getX() > 0; } private void moveNorth() { this.setY(this.getY() - 1); } private void moveSouth() { this.setY(this.getY() + 1); } private void moveEast() { this.setX(this.getX() + 1); } private void moveWest() { this.setX(this.getX() - 1); } public static void main(String[] args) { MovingRobot r1 = new MovingRobot(0, 0); MovingRobot r2 = new MovingRobot(9, 9); while (!MovingRobot.sameSlot(r1, r2)) { r1.move(); r2.move(); clear(); System.out.println(MovingRobot.printGrid(r1, r2)); sleep(1000); } System.out.println("=== Question 9 ==="); System.out.println(String.format("Collision at: (%d, %d)", r1.getX(), r1.getY())); System.out.println(String.format("R1 Route: [%s]", r1.printMoves())); System.out.println(String.format("R2 Route: [%s]", r2.printMoves())); } private static void clear() { try { if (System.getProperty("os.name").contains("Windows")) Runtime.getRuntime().exec("cls"); else { System.out.print("\033[H\033[2J"); System.out.flush(); } } catch (Exception e) { } } private static void sleep(int milliseconds) { try { Thread.sleep(milliseconds); } catch (InterruptedException e) { } } }