import cs1705.*; // ------------------------------------------------------------------------- /** * This robot adds extra behaviors to a VPIRobot that give it powerful * navigation abilities. * * @author Stephen Edwards * @version 2003.09.21 */ public class NavigatorBot extends VPIRobot { //~ Constructors .......................................................... // ---------------------------------------------------------- /** * Construct a navigator robot by specifying its starting state. * The parameters are just passed to the corresponding VPIRobot * constructor. */ public NavigatorBot( int street, int avenue, Direction facing, int numBeepers ) { super( street, avenue, facing, numBeepers ); } //~ Methods ............................................................... // ---------------------------------------------------------- /** * Make a 270-degree turn left. */ public void turnRight() { turnLeft(); turnLeft(); turnLeft(); } // ---------------------------------------------------------- /** * Turn until the robot is facing north. */ public void faceNorth() { while ( !facingNorth() ) { turnLeft(); } } // ---------------------------------------------------------- /** * Turn until the robot is facing south. */ public void faceSouth() { while ( !facingSouth() ) { turnLeft(); } } // ---------------------------------------------------------- /** * Turn until the robot is facing east. */ public void faceEast() { while ( !facingEast() ) { turnLeft(); } } // ---------------------------------------------------------- /** * Turn until the robot is facing west. */ public void faceWest() { while ( !facingWest() ) { turnLeft(); } } // ---------------------------------------------------------- /** * Move the specified number of steps forward. * * @param distance the number of steps to take */ public void move( int distance ) { int count = 0; while ( count < distance ) { move(); count++; } } // ---------------------------------------------------------- /** * Move to the specified intersection. * * @param street the street number of the destination * @param avenue the avenue number of the destination */ public void moveTo( int street, int avenue ) { moveToStreet( street ); moveToAvenue( avenue ); } // ---------------------------------------------------------- /** * Move to the specified street. * * @param desiredStreet the street number of the destination */ public void moveToStreet( int desiredStreet ) { if ( desiredStreet < street() ) { faceSouth(); move( street() - desiredStreet ); } else { faceNorth(); move( desiredStreet - street() ); } } // ---------------------------------------------------------- /** * Move to the specified avenue. * * @param desiredAvenue the avenue number of the destination */ public void moveToAvenue( int desiredAvenue ) { if ( desiredAvenue < avenue() ) { faceWest(); move( avenue() - desiredAvenue ); } else { faceEast(); move( desiredAvenue - avenue() ); } } }