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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/**
* Assignment 2, COMP268 Class: MovingRobot.java
*
* @description A class used to move a robot on a 9x9 grid.
* @author: mo khan Student ID: 3431709
* @date August 5, 2019
* @version 1.0
*/
package Q9;
import java.util.*;
public class MovingRobot extends Robot {
private ArrayList<Integer> moves = new ArrayList<Integer>();
private Random rng;
/**
* Creates an instance of a moving robot.
*
* @param x the x coordinate on the 9x9 grid
* @param y the y coordinate on the 9x9 grid
*/
public MovingRobot(int x, int y) {
super(x, y);
this.rng = new Random();
}
/**
* Checks to see if a robot can advance in the direction provided.
*
* @param the direction that a robot wants to advance to.
* @return returns true if the robot can advance to the given direction.
*/
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 canMoveNorth() && canMoveWest();
default:
return false;
}
}
/**
* @return a random direction.
*/
public int generateNextMove() {
return this.rng.nextInt(7) + 1;
}
/**
* @return true if the two robots are in the same coordinate.
*/
public static boolean sameSlot(Robot r1, Robot r2) {
return r1.getX() == r2.getX() && r1.getY() == r2.getY();
}
/**
* @return The list of moves made by the robot.
*/
public String printMoves() {
ArrayList<String> printableMoves = new ArrayList<String>();
for (Integer move : this.moves) printableMoves.add(String.valueOf(move));
return String.join(",", printableMoves);
}
/**
* Moves the robot in a random direction.
*/
public void move() {
int direction = generateNextMove();
while (!validateNextMove(direction)) direction = generateNextMove();
this.move(direction);
}
/**
* An overload of the move method that attempts to move in the direction
* specified.
* @param direction the direction to move the robot towards.
*/
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);
}
/**
* The entry point to the console application.
* @param args the argument vector provided to the program.
*/
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("==== Question 9 ====");
System.out.println(Robot.printGrid(r1, r2));
System.out.println(String.format("R1 (%d, %d): [%s]", r1.getX(), r1.getY(), r1.printMoves()));
System.out.println(String.format("R2 (%d, %d): [%s]", r2.getX(), r2.getY(), r2.printMoves()));
sleep(250);
}
}
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) {
}
}
}
|