-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomRobot.java
More file actions
executable file
·50 lines (42 loc) · 1.52 KB
/
RandomRobot.java
File metadata and controls
executable file
·50 lines (42 loc) · 1.52 KB
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
import java.util.*;
public class RandomRobot extends Robot {
public RandomRobot(int startX, int startY) {
super(startX, startY);
}
public void move(Maze m) {
int currentYTemp = getRobotPos()[1];
int currentXTemp = getRobotPos()[0];
ArrayList<String> moves = possibleMoves(m);
Random gen = new Random();
switch (moves.get(gen.nextInt(moves.size()))) {
case "Up":
currentYTemp--;
break;
case "Down":
currentYTemp++;
break;
case "Right":
currentXTemp++;
break;
case "Left":
currentXTemp--;
break;
}
setPos(currentXTemp, currentYTemp);
}
public ArrayList<String> possibleMoves(Maze m) {
boolean [][] temp = m.getMaze();
ArrayList<String> moves = new ArrayList<String>();
int currentYTemp = getRobotPos()[1];
int currentXTemp = getRobotPos()[0];
if (currentYTemp != temp.length - 1 && temp[currentYTemp + 1][currentXTemp])
moves.add("Down");
if (currentYTemp != 0 && temp[currentYTemp - 1][currentXTemp])
moves.add("Up");
if(currentXTemp != temp[0].length && temp[currentYTemp][currentXTemp + 1])
moves.add("Right");
if(currentXTemp != 0 && temp[currentYTemp][currentXTemp - 1])
moves.add("Left");
return moves;
}
}