-
Notifications
You must be signed in to change notification settings - Fork 0
/
Broken.java
61 lines (47 loc) · 1.96 KB
/
Broken.java
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
import uk.ac.warwick.dcs.maze.logic.IRobot;
public class Broken {
private byte isTargetNorth(IRobot robot) {
if (robot.getLocation().y == robot.getTargetLocation().y) return 0;
else return (byte)(robot.getLocation().y > robot.getTargetLocation().y?1:-1);
}
private byte isTargetEast(IRobot robot) {
if (robot.getLocation().x == robot.getTargetLocation().x) return 0;
else return (byte)(robot.getLocation().x > robot.getTargetLocation().x?-1:1);
}
private int lookHeading(IRobot robot, int direction) {
return robot.look(switch (direction - robot.getHeading()) {
case -3,1 -> IRobot.RIGHT;
case -2,2 -> IRobot.BEHIND;
case -1,3 -> IRobot.LEFT;
default -> IRobot.AHEAD;
});
}
private int headingController(IRobot robot) {
/*
* When the value of direction equal to 0 means it's a wall
* When the value of direction less than 0 means it's not the first choice
*/
int direction[] = new int[]{IRobot.NORTH, IRobot.EAST, IRobot.SOUTH, IRobot.WEST};
int result = 0;
for (int i = 0; i < 4; i++) {
direction[i] *= (lookHeading(robot, direction[i]) == IRobot.WALL)?0:1;
}
direction[0] *= isTargetNorth(robot)*1;
direction[1] *= isTargetEast(robot)*1;
direction[2] *= isTargetNorth(robot)*-1;
direction[3] *= isTargetEast(robot)*-1;
for (int i = 0; i < 4; i++) if (direction[i] > 0) result++;
if (result == 0) for (int i = 0; i < 4; i++) direction[i] *= -1;//If all directions are either walls or second choices, make all second choices first choices
result = (int)(Math.random()*4);
while (direction[result] <= 0) result = (int)(Math.random()*4);//If the result not the first choise, re-randomize direction
return direction[result];
}
public void reset() {
ControlTest.printResults();
}
public void controlRobot(IRobot robot) {
int heading = headingController(robot);
ControlTest.test(heading, robot);
robot.setHeading(heading);
}
}