Skip to content

Commit 56a97a6

Browse files
committed
Merge branch 'master' of https://github.com/HarveyLeo/MDP-Grp2
2 parents 67991eb + b93d193 commit 56a97a6

File tree

22 files changed

+1497
-830
lines changed

22 files changed

+1497
-830
lines changed

Algorithm/map-descriptors/arena.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
000000000000000
22
000000000000000
3+
000000000000100
4+
000000001000000
5+
000000001000000
6+
000000001000000
37
000000000000000
8+
111000000000000
49
000000000000000
510
000000000000000
611
000000000000000
12+
000011111100000
13+
000011111100000
714
000000000000000
15+
000000000000010
816
000000000000000
17+
000000010000000
918
000000000000000
10-
000000000000000
11-
000000000000000
12-
000000000000000
13-
000000000000000
14-
000000000000000
15-
000000000000000
16-
000000000000000
17-
000000000000000
18-
000000000000000
19-
000000000000000
19+
000010000000000
2020
000000000000000

Algorithm/src/algorithms/AStarPathFinder.java

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import datatypes.Movement;
77
import datatypes.Orientation;
8+
import main.RobotSystem;
89
import simulator.arena.Arena;
910
import simulator.robot.Robot;
1011

@@ -123,7 +124,11 @@ private void init(int[][] mazeRef) {
123124
_robot = Robot.getInstance();
124125
}
125126

126-
public Orientation moveRobotAlongFastestPath(Path fastestPath, Orientation currentOrientation, boolean isExploring) {
127+
public Orientation moveRobotAlongFastestPath(Path fastestPath, Orientation currentOrientation) {
128+
return moveRobotAlongFastestPath(fastestPath, currentOrientation, false, false);
129+
}
130+
131+
public Orientation moveRobotAlongFastestPath(Path fastestPath, Orientation currentOrientation, boolean isExploring, boolean hasCalibration) {
127132

128133
int[] tempPosition = new int[2];
129134
int[] robotPosition;
@@ -135,7 +140,7 @@ public Orientation moveRobotAlongFastestPath(Path fastestPath, Orientation curre
135140
robotPosition = fastestPath.getStep(0);
136141

137142
int count = 0;
138-
143+
139144
for (int i = 0; i < steps.size() - 1; i++) {
140145

141146
tempPosition[0] = steps.get(i).getX();
@@ -152,37 +157,25 @@ public Orientation moveRobotAlongFastestPath(Path fastestPath, Orientation curre
152157

153158
if (isExploring) {
154159
for (int v = 0; v < count; v++) {
155-
robotPosition = explorer.updateRobotPositionAfterMF(currentOrientation, robotPosition);
156-
explorer.setIsExplored(robotPosition, currentOrientation);
157160
_robot.moveForward();
161+
robotPosition = explorer.updateRobotPositionAfterMF(currentOrientation, robotPosition);
162+
explorer.setIsExplored(robotPosition, currentOrientation, hasCalibration);
158163
}
159164
} else {
160165
_robot.moveForward(count);
161166
}
162167
count = 1;
163-
Movement move = ChangeRobotOrientation(currentOrientation, nextOrientation);
164-
Orientation ori;
165-
if (isExploring) {
166-
if (move == Movement.TURN_RIGHT_TWICE) {
167-
ori = explorer.updateRobotOrientation(Movement.TURN_RIGHT);
168-
explorer.setIsExplored(robotPosition, ori);
169-
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
170-
explorer.setIsExplored(robotPosition, ori);
171-
} else {
172-
ori = explorer.updateRobotOrientation(move);
173-
explorer.setIsExplored(robotPosition, ori);
174-
}
175-
}
168+
ChangeRobotOrientation(currentOrientation, nextOrientation, isExploring, hasCalibration);
176169
}
177170
currentOrientation = nextOrientation;
178171
}
179172

180173

181174
if (isExploring) {
182175
for (int v = 0; v < count; v++) {
183-
robotPosition = explorer.updateRobotPositionAfterMF(currentOrientation, robotPosition);
184-
explorer.setIsExplored(robotPosition, currentOrientation);
185176
_robot.moveForward();
177+
robotPosition = explorer.updateRobotPositionAfterMF(currentOrientation, robotPosition);
178+
explorer.setIsExplored(robotPosition, currentOrientation, hasCalibration);
186179
}
187180
} else {
188181
_robot.moveForward(count);
@@ -194,61 +187,110 @@ public Orientation moveRobotAlongFastestPath(Path fastestPath, Orientation curre
194187

195188

196189

197-
private Movement ChangeRobotOrientation(Orientation curOri, Orientation nextOri) {
190+
private void ChangeRobotOrientation(Orientation curOri, Orientation nextOri, boolean isExploring, boolean hasCalibration) {
191+
MazeExplorer explorer = MazeExplorer.getInstance();
192+
198193
switch (curOri) {
199194
case NORTH:
200195
if (nextOri == Orientation.EAST) {
201196
_robot.turnRight();
202-
return Movement.TURN_RIGHT;
197+
if (isExploring) {
198+
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
199+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
200+
}
203201
} else if (nextOri == Orientation.WEST) {
204202
_robot.turnLeft();
205-
return Movement.TURN_LEFT;
203+
if (isExploring) {
204+
explorer.updateRobotOrientation(Movement.TURN_LEFT);
205+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
206+
}
206207
} else if (nextOri == Orientation.SOUTH) {
207208
_robot.turnRight();
209+
if (isExploring) {
210+
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
211+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
212+
}
208213
_robot.turnRight();
209-
return Movement.TURN_RIGHT_TWICE;
214+
if (isExploring) {
215+
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
216+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
217+
}
210218
}
211219
break;
212220
case SOUTH:
213221
if (nextOri == Orientation.EAST) {
214222
_robot.turnLeft();
215-
return Movement.TURN_LEFT;
223+
if (isExploring) {
224+
explorer.updateRobotOrientation(Movement.TURN_LEFT);
225+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
226+
}
216227
} else if (nextOri == Orientation.WEST) {
217228
_robot.turnRight();
218-
return Movement.TURN_RIGHT;
229+
if (isExploring) {
230+
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
231+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
232+
}
219233
} else if (nextOri == Orientation.NORTH) {
220234
_robot.turnRight();
235+
if (isExploring) {
236+
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
237+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
238+
}
221239
_robot.turnRight();
222-
return Movement.TURN_RIGHT_TWICE;
240+
if (isExploring) {
241+
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
242+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
243+
}
223244
}
224245
break;
225246
case EAST:
226247
if (nextOri == Orientation.NORTH) {
227248
_robot.turnLeft();
228-
return Movement.TURN_LEFT;
249+
if (isExploring) {
250+
explorer.updateRobotOrientation(Movement.TURN_LEFT);
251+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
252+
}
229253
} else if (nextOri == Orientation.SOUTH) {
230254
_robot.turnRight();
231-
return Movement.TURN_RIGHT;
255+
if (isExploring) {
256+
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
257+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
258+
}
232259
} else if (nextOri == Orientation.WEST) {
233260
_robot.turnRight();
261+
if (isExploring) {
262+
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
263+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
264+
}
234265
_robot.turnRight();
235-
return Movement.TURN_RIGHT_TWICE;
266+
if (isExploring) {
267+
explorer.updateRobotOrientation(Movement.TURN_RIGHT);
268+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
269+
}
236270
}
237271
break;
238272
case WEST:
239273
if (nextOri == Orientation.NORTH) {
240274
_robot.turnRight();
241-
return Movement.TURN_RIGHT;
275+
if (isExploring) {
276+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
277+
}
242278
} else if (nextOri == Orientation.SOUTH) {
243279
_robot.turnLeft();
244-
return Movement.TURN_LEFT;
280+
if (isExploring) {
281+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
282+
}
245283
} else if (nextOri == Orientation.EAST) {
246284
_robot.turnRight();
285+
if (isExploring) {
286+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
287+
}
247288
_robot.turnRight();
248-
return Movement.TURN_RIGHT_TWICE;
289+
if (isExploring) {
290+
explorer.setIsExplored(explorer.getRobotPosition(), explorer.getRobotOrientation(), hasCalibration);
291+
}
249292
}
250293
}
251-
return null;
252294
}
253295

254296
private Orientation getOrientationIfMoveToNeighbor(Orientation curOri, int curX, int curY, int nextX, int nextY) {

0 commit comments

Comments
 (0)