-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRightHandRuleRobot.java
171 lines (159 loc) · 5.25 KB
/
RightHandRuleRobot.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
import java.io.FileNotFoundException;
/**
*
*
*
*/
public class RightHandRuleRobot extends Robot {
private char lastMove;
private boolean startCheckStatus=false;
public RightHandRuleRobot(Maze m) throws IllegalStateException
,FileNotFoundException {
super(m);
}
/**
* This method moves the robot one step according to the
* righ hand rule. In other words, the robot keeps the
* "right hand" on the wall and follows the wall until
* it reaches it's goal.
* If no walls are adjacent to the start position, the robot
* moves north until it reaches a wall.
* @throws Exception if no adjacent positions were movable.
*/
@Override
public void move() throws IllegalStateException {
int whileCount= 0;
boolean didMove=false;
if(startCheck()) {
while (!didMove && whileCount < 2) {
switch (lastMove) {
case 'N':
if (tryGoingEast()) {
didMove = true;
break;
}
case 'W':
if (tryGoingNorth()) {
didMove = true;
break;
}
case 'S':
if (tryGoingWest()) {
didMove = true;
break;
}
case 'E':
if (tryGoingSouth()) {
didMove = true;
break;
}
default:
lastMove = 'N';
whileCount++;
}
}
if (whileCount >= 2)
throw new IllegalStateException("No movables adjacent to " +
"current position");
}
else{
tryGoingNorth();
setLastMoved('S');
}
}
private boolean startCheck(){
if(!startCheckStatus){
try{
if(m.isWall(getNorth()))
startCheckStatus=true;
}catch(ArrayIndexOutOfBoundsException e){}
try{
if(m.isWall(getEast()))
startCheckStatus=true;
}catch(ArrayIndexOutOfBoundsException e){}
try{
if(m.isWall(getSouth()))
startCheckStatus=true;
}catch(ArrayIndexOutOfBoundsException e){}
try{
if(m.isWall(getWest()))
startCheckStatus = true;
}catch(ArrayIndexOutOfBoundsException e){}
}
return startCheckStatus;
}
/**
* method to try moving the robot north.
* @return boolean representing if the robot was able to move.
*/
private boolean tryGoingNorth(){
boolean ableToMove = false;
try {
if (isAValidMovable(getNorth())) {
setCurrentPosition(getNorth());
setLastMoved('N');
ableToMove = true;
}
} catch (ArrayIndexOutOfBoundsException Exception) {}
return ableToMove;
}
/**
* method to try moving the robot west.
* @return boolean representing if the robot was able to move.
*/
private boolean tryGoingWest(){
boolean ableToMove = false;
try {
if (isAValidMovable(getWest())){
setCurrentPosition(getWest());
setLastMoved('W');
ableToMove = true;
}
} catch (ArrayIndexOutOfBoundsException Exception) {}
return ableToMove;
}
/**
* method to try moving the robot south.
* @return boolean representing if the robot was able to move.
*/
private boolean tryGoingSouth(){
boolean ableToMove = false;
try {
if (isAValidMovable(getSouth())) {
setCurrentPosition(getSouth());
setLastMoved('S');
ableToMove=true;
}
} catch (ArrayIndexOutOfBoundsException Exception){}
return ableToMove;
}
/**
* method to try moving the robot east.
* @return boolean representing if the robot was able to move.
*/
private boolean tryGoingEast(){
boolean ableToMove = false;
try {
if (isAValidMovable(getEast())) {
setCurrentPosition(getEast());
setLastMoved('E');
ableToMove = true;
}
} catch (ArrayIndexOutOfBoundsException Exception) {}
return ableToMove;
}
/**
* Checks if position p is movable.
* @param p - position to check
* @return boolean representing result.
*/
private boolean isAValidMovable(Position p){
return m.isMovable(p)||m.isGoal(p);
}
/**
* A Method to keep track of the last move of the robot
* @param c - character representing which direction
* the robot has moved, (S,E,W,N)
*/
private void setLastMoved(char c){lastMove=c;}
}