-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex3.java
174 lines (138 loc) · 4.87 KB
/
Ex3.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
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
import uk.ac.warwick.dcs.maze.logic.IRobot;
public class Ex3 {
private RobotData robotData = new RobotData();
private int pollRun = 0;
public void controlRobot(IRobot robot) {
int direction = IRobot.AHEAD;
int exits = nonwallExits(robot);
if ((robot.getRuns() == 0) && (pollRun == 0))
robotData.init();
this.pollRun++;
switch (exits) {
case 1 -> direction = deadEnd(robot);
case 2 -> direction = corridor(robot);
case 3, 4 -> direction = junctionAndCrossroad(robot);
default -> System.err.println("Invalid exits value");
}
robotData.updateLocation(robot.getLocation().x, robot.getLocation().y);
robot.face(direction);
}
public void reset() {
robotData.init();
//System.out.println("Reset");
}
private int nonwallExits(IRobot robot) {
int exits = 0;
for (int i = IRobot.AHEAD; i <= IRobot.LEFT; i++)
if (robot.look(i) != IRobot.WALL)
exits++;
return exits;
}
private int deadEnd(IRobot robot) {
int direction = IRobot.BEHIND;
robotData.markPath(robot.getLocation().x, robot.getLocation().y);
if (robot.look(direction) == IRobot.WALL) // Handling the case of facing a wall at the start
for (int i = IRobot.AHEAD; i <= IRobot.LEFT; i++)
direction = (robot.look(i) == IRobot.WALL)?direction:i;
return direction;
}
private int corridor(IRobot robot) {
int direction = IRobot.AHEAD;
robotData.markPath(robot.getLocation().x, robot.getLocation().y);
if (robot.look(direction) == IRobot.WALL) // Handling the case of being at a corner
direction = (robot.look(IRobot.LEFT) == IRobot.WALL)?IRobot.RIGHT:IRobot.LEFT;
return direction;
}
private int junctionAndCrossroad(IRobot robot) {
robotData.markJunction(robot.getLocation().x, robot.getLocation().y);
if (areAllPathsExceptEntranceUnmarked(robot)) // Only the entrance that just came from is marked
return randomDirection(robot, 0); // Pick an arbitrary unmarked entrance
else if ((robotData.getMark(robotData.getLastX(), robotData.getLastY()) < 2)&&
(robotData.getMark(robotData.getLastX(), robotData.getLastY()) > 0))
return IRobot.BEHIND; // Pick the entrance that just came from
else {
if (randomDirection(robot, 0) != -1)
return randomDirection(robot, 0); // Pick the entrance with 0 mark
else if (randomDirection(robot, 1) != -1)
return randomDirection(robot, 1); // Pick the entrance with 1 mark
else if ((randomDirection(robot, -1) != -1))
return randomDirection(robot, -1); // Pick the entrance that is junction
}
System.err.println("No optional branches");
return IRobot.AHEAD;
}
private boolean areAllPathsExceptEntranceUnmarked(IRobot robot) {
int rules[][] = {{0,-1},{1,0},{0,1},{-1,0}};
int x, y;
for (int i = 0; i < 4; i++) {
x = robot.getLocation().x + rules[i][0];
y = robot.getLocation().y + rules[i][1];
if ((robotData.getLastX() != x)&&(robotData.getLastY() != y))
if (robotData.getMark(x, y) != 0)
return false;
}
return true;
}
private int randomDirection(IRobot robot, int mark) {
int rules[][] = {{0,-1},{1,0},{0,1},{-1,0}};
int x, y;
int numOfOptional = 0;
int optionalDirection[] = new int[4];
for (int i = 0; i < 4; i++) {
x = robot.getLocation().x + rules[i][0];
y = robot.getLocation().y + rules[i][1];
if (robotData.getMark(x, y) == mark)
if (robot.look(absToRel(robot, IRobot.NORTH + i)) != IRobot.WALL)
optionalDirection[numOfOptional++] = absToRel(robot, IRobot.NORTH + i);
}
if (numOfOptional == 0) // No suitable choice
return -1;
else
return optionalDirection[(int)(Math.random()*numOfOptional)];
}
private int absToRel(IRobot robot, int abs) {
return switch(robot.getHeading() - abs) {
case 0 -> IRobot.AHEAD;
case -1, 3 -> IRobot.RIGHT;
case -2, 2 -> IRobot.BEHIND;
case -3, 1 -> IRobot.LEFT;
default -> {
System.err.println("Invalid absolute direction");
yield -1;
}
};
}
}
class RobotData {
private static int MAX_WIDTH = 505;
private int mazeMap[][];
private int lastX;
private int lastY;
public void init() {
this.mazeMap = new int[RobotData.MAX_WIDTH][RobotData.MAX_WIDTH];
this.lastX = 0;
this.lastY = 0;
}
public void updateLocation(int x, int y) {
this.lastX = x;
this.lastY = y;
}
public void markPath(int x, int y) {
this.mazeMap[x][y]++;
}
public void markJunction(int x, int y) {
this.mazeMap[x][y] = -1;
}
public int getLastX() {
return this.lastX;
}
public int getLastY() {
return this.lastY;
}
public int getMark(int x, int y) {
if ((x > RobotData.MAX_WIDTH)||(y > RobotData.MAX_WIDTH)) {
System.err.println("X or Y out of Bounds");
}
return this.mazeMap[x][y];
}
}