blob: 6f9d2c07d0e9ae22d696fdb089281dfaff54f483 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/**
* 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;
}
}
|