-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex1.java
69 lines (59 loc) · 1.94 KB
/
Ex1.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
/*
Ex1
I first randomly selected a direction as the direction for the robot to move forward.
Then I will make a judgment. if the robot is facing a wall, I will Re-randomize direction until it is not facing a wall.
I separated the random direction selection into a separate function to reduce duplication of code.
*/
import uk.ac.warwick.dcs.maze.logic.IRobot;
public class Ex1 {
/**
* Generate a random direction
*
* @return Random direction
*/
private int randomDirection() {
return switch ((int) (Math.random() * 4)) { //Return the corresponding direction based on the value of Math.random()*4
case 0 -> IRobot.LEFT;
case 1 -> IRobot.RIGHT;
case 2 -> IRobot.BEHIND;
case 3 -> IRobot.AHEAD;
default -> -1;
};
}
/**
* Print logs
*
* @param robot Robot object
* @param direction Direction
*/
private void logPrinter(IRobot robot, int direction) {
int wallNum = 0;
for (int i = IRobot.AHEAD; i <= IRobot.LEFT; i++) if (robot.look(i) == IRobot.WALL) {
wallNum++; //Count the number of walls around the current position
}
System.out.println("I'm going " +
switch (direction) {
case IRobot.LEFT -> "left ";
case IRobot.RIGHT -> "right ";
case IRobot.BEHIND -> "backwards ";
case IRobot.AHEAD -> "forward ";
default -> "Error!!!";
} +
switch (wallNum) {
case 0 -> "at a crossroads";
case 1 -> "at a junction";
case 2 -> "down a corridor";
case 3 -> "at a deadend";
default -> "Error!!!";
}
);
}
public void controlRobot(IRobot robot) {
int direction = randomDirection(); //Randomly initialize directions
while (robot.look(direction) == IRobot.WALL) {
direction = randomDirection(); //Reselect direction when the robot is face to a wall
}
logPrinter(robot, direction);
robot.face(direction); //Face the direction
}
}