Java Reference
In-Depth Information
if
(command.indexOf("ROTATERIGHT") > -1) {
double
angle
#
Math.abs(Double.parseDouble(
command.trim().substring(12).trim()));
numRotSteps
#
angle / rotStep;
orientation
#
1;
running
#
true
;
}
else if
(command.indexOf("ROTATELEFT") > -1) {
double
angle
#
Math.abs(Double.parseDouble(
command.trim().substring(11).trim()));
numRotSteps
#
angle / rotStep;
orientation
#
-1;
running
#
true
;
}
else if
(command.indexOf("MOVEFW") > -1) {
double
distance
#
Math.abs(Double.parseDouble(
command.trim().substring(7).trim()));
numMoveSteps
#
distance / moveStep;
orientation
#
1;
running
#
true
;
}
else if
(command.indexOf("MOVEBW") > -1) {
double
distance
#
Math.abs(Double.parseDouble(
command.trim().substring(7).trim()));
numMoveSteps
#
distance / moveStep;
orientation
#
-1;
}
else
writeOut("DECLINED"); // invalid command
}
// The nextStep() method implements the elemental steps of
// each command
public void
nextStep() {
if
(! running)
return
;
if
(numRotSteps > 0.0) {
// executes a rotation step
if
(numRotSteps < 1.0)
robotPos.rototrans(0.0, 0.0,
numRotSteps*orientation*rotStep);
else
robotPos.rototrans(0.0, 0.0, orientation*rotStep);
// updates the robot's position
robot.writePosition(robotPos);
// repaints the graphical interface
environment.repaint();
numRotSteps-
#
1.0;
}