master
1/**
2 * Assignment 2, COMP268 Class: Robot.java
3 *
4 * @description A class used to represent the location of a robot on a grid.
5 * @author: mo khan Student ID: 3431709
6 * @date August 5, 2019
7 * @version 1.0
8 */
9package Q9;
10
11public class Robot {
12 public static final int UP = 1;
13 public static final int DOWN = 2;
14 public static final int LEFT = 3;
15 public static final int RIGHT = 4;
16 public static final int LEFT_UP_CORNER = 5;
17 public static final int LEFT_DOWN_CORNER = 6;
18 public static final int RIGHT_UP_CORNER = 7;
19 public static final int RIGHT_DOWN_CORNER = 8;
20
21 public static final int NORTH = UP;
22 public static final int NORTH_EAST = RIGHT_UP_CORNER;
23 public static final int EAST = RIGHT;
24 public static final int SOUTH_EAST = RIGHT_DOWN_CORNER;
25 public static final int SOUTH = DOWN;
26 public static final int SOUTH_WEST = LEFT_DOWN_CORNER;
27 public static final int WEST = LEFT;
28 public static final int NORTH_WEST = LEFT_UP_CORNER;
29 public static final String R1 = "1";
30 public static final String R2 = "2";
31 public static final String COLLISION = "X";
32 public static final String SPACE = " ";
33 public static final String SEPARATOR = "|";
34
35 private int x;
36 private int y;
37
38 /**
39 * Constructs an instance of a robot.
40 *
41 * @param x the x coordinate of the robot.
42 * @param y the y coordinate of the robot.
43 */
44 public Robot(int x, int y) {
45 this.x = x;
46 this.y = y;
47 }
48
49 /** @return the x coordinate */
50 public int getX() {
51 return x;
52 }
53
54 /** @return the y coordinate */
55 public int getY() {
56 return y;
57 }
58
59 /** @param x the x coordinate to move to. */
60 public void setX(int x) {
61 this.x = x;
62 }
63
64 /** @param y the y coordinate to move to. */
65 public void setY(int y) {
66 this.y = y;
67 }
68
69 /**
70 * @param x the x coordinate
71 * @param y the y coordinate
72 * @return true if the robot is at the given coordinate
73 */
74 public boolean atPosition(int x, int y) {
75 return getX() == x && getY() == y;
76 }
77
78 /**
79 * @param r1 robot one.
80 * @param r2 robot two.
81 * @return the grid with each of the robots printed.
82 */
83 public static String printGrid(Robot r1, Robot r2) {
84 String grid = "";
85
86 for (int row = 0; row < 10; row++) {
87 for (int column = 0; column < 10; column++) {
88 boolean r1InCell = r1.atPosition(row, column);
89 boolean r2InCell = r2.atPosition(row, column);
90
91 grid += SEPARATOR;
92 if (r1InCell && r2InCell) grid += COLLISION;
93 else if (r1InCell) grid += R1;
94 else if (r2InCell) grid += R2;
95 else grid += SPACE;
96 }
97 grid += String.format("%s%s", SEPARATOR, System.lineSeparator());
98 }
99 return grid;
100 }
101}