-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRightHandRobot.java
More file actions
99 lines (90 loc) · 3.06 KB
/
RightHandRobot.java
File metadata and controls
99 lines (90 loc) · 3.06 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
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
public class RightHandRobot extends Robot
{
private int orientation;
public RightHandRobot(int startX, int startY, int orientation)
{
super(startX, startY);
this.orientation = orientation;
}
public void move(Maze maze)
{
int currentYTemp = getRobotPos()[1];
int currentXTemp = getRobotPos()[0];
switch (possibleMove(maze)) {
case "Up":
currentYTemp--;
break;
case "Down":
currentYTemp++;
break;
case "Right":
currentXTemp++;
break;
case "Left":
currentXTemp--;
break;
}
setPos(currentXTemp, currentYTemp);
}
public String possibleMove(Maze maze)
{
boolean[][] temp = maze.getMaze();
int currentYTemp = getRobotPos()[1];
int currentXTemp = getRobotPos()[0];
for (int i = 0; i < 2; i++) {
if (orientation == 3)//South
{
if(currentYTemp + 1 != temp.length && temp[currentYTemp + 1][currentXTemp] && !temp[currentYTemp][currentXTemp - 1])
return "Down";
else if(currentXTemp + 1 != temp[0].length && !temp[currentYTemp][currentXTemp-1])
orientation++;
else {
orientation--;
return "Left";
}
}
if (orientation == 4)//East
{
if(currentXTemp + 1 != temp[0].length && temp[currentYTemp][currentXTemp + 1] && !temp[currentYTemp + 1][currentXTemp])
return "Right";
else if(currentYTemp != 0 && !temp[currentYTemp + 1][currentXTemp])
orientation = 1;
else {
orientation--;
return "Down";
}
}
if (orientation == 1)//North
{
if (currentYTemp != 0 && temp[currentYTemp - 1][currentXTemp] && !temp[currentYTemp][currentXTemp + 1])
return "Up";
else if(currentXTemp != 0 && !temp[currentYTemp][currentXTemp + 1])
orientation++;
else {
orientation = 4;
return "Right";
}
}
if(orientation == 2)//West
{
if (currentXTemp != 0 && temp[currentYTemp][currentXTemp - 1] && !temp[currentYTemp - 1][currentXTemp])
return "Left";
else if (currentYTemp + 1 != temp.length && !temp[currentYTemp - 1][currentXTemp])
orientation++;
else {
orientation--;
return "Up";
}
}
}
return "no move";
}
//North=1
//West=2
//South=3
//East=4
public int getOrientation()
{
return orientation;
}
}