/** * Assignment 2, COMP268 Class: Robot.java * * @description A class used to represent the location of a robot on a grid. * @author: mo khan Student ID: 3431709 * @date August 5, 2019 * @version 1.0 */ package Q9; public class Robot { public static final int UP = 1; public static final int DOWN = 2; public static final int LEFT = 3; public static final int RIGHT = 4; public static final int LEFT_UP_CORNER = 5; public static final int LEFT_DOWN_CORNER = 6; public static final int RIGHT_UP_CORNER = 7; public static final int RIGHT_DOWN_CORNER = 8; public static final int NORTH = UP; public static final int NORTH_EAST = RIGHT_UP_CORNER; public static final int EAST = RIGHT; public static final int SOUTH_EAST = RIGHT_DOWN_CORNER; public static final int SOUTH = DOWN; public static final int SOUTH_WEST = LEFT_DOWN_CORNER; public static final int WEST = LEFT; public static final int NORTH_WEST = LEFT_UP_CORNER; public static final String R1 = "1"; public static final String R2 = "2"; public static final String COLLISION = "X"; public static final String SPACE = " "; public static final String SEPARATOR = "|"; private int x; private int y; /** * Constructs an instance of a robot. * * @param x the x coordinate of the robot. * @param y the y coordinate of the robot. */ public Robot(int x, int y) { this.x = x; this.y = y; } /** @return the x coordinate */ public int getX() { return x; } /** @return the y coordinate */ public int getY() { return y; } /** @param x the x coordinate to move to. */ public void setX(int x) { this.x = x; } /** @param y the y coordinate to move to. */ public void setY(int y) { this.y = y; } /** * @param x the x coordinate * @param y the y coordinate * @return true if the robot is at the given coordinate */ public boolean atPosition(int x, int y) { return getX() == x && getY() == y; } /** * @param r1 robot one. * @param r2 robot two. * @return the grid with each of the robots printed. */ public static String printGrid(Robot r1, Robot r2) { String grid = ""; for (int row = 0; row < 10; row++) { for (int column = 0; column < 10; column++) { boolean r1InCell = r1.atPosition(row, column); boolean r2InCell = r2.atPosition(row, column); grid += SEPARATOR; if (r1InCell && r2InCell) grid += COLLISION; else if (r1InCell) grid += R1; else if (r2InCell) grid += R2; else grid += SPACE; } grid += String.format("%s%s", SEPARATOR, System.lineSeparator()); } return grid; } }