
/**
 * Robot class for CS112 Project 1, Spring 2006.  This class
 * is used for the only racer for option 1, and the finishing
 * racer for option 2.  Instructor program
 * 
 * @author Russell C. Bjork
 * @version January 19, 2006
 */
import kareltherobot.*;

public class RacerRobot extends Robot
{
    /** Constructor
     *
     *  @param street the starting street for this robot
     *  @param avenue the starting avenue for this robot
     *  @param direction the direction this robot is initially facing
     *  @param beepers the number of beepers this robot has initially
     */
    public RacerRobot(int street, int avenue, Direction direction, int beepers)
    {
        super(street, avenue, direction, beepers);
    }
    
    /** Run the race
     * 
     */
    public void runRace()
    {
         while (! nextToABeeper())
         	advanceOneStep();
         pickBeeper();
    }
    
    /** Advance one step
     */
    public void advanceOneStep()
    {
    	if (frontIsClear())
            move();
        else
            climbOverHurdle();
    }
    
    /** Climb over a hurdle
     *
     */
    public void climbOverHurdle()
    {
        turnLeft();
        while (! rightIsClear())
            move();
        turnRight();
        move();
        while (! rightIsClear())
            move();
        turnRight();
        while (frontIsClear())
            move();
        turnLeft();
    }
    
    /** Test to see if right side of robot is clear
     * 
     *  @return true if right side is clear
     */
    public boolean rightIsClear()
    {
        turnRight();
        if (frontIsClear())
        {  
            turnLeft();
            return true;
        }
        else
        {
            turnLeft();
            return false;
        }
    }
    
    /** Turn right 90 degrees
     * 
     */
    public void turnRight()
    {
        turnLeft();
        turnLeft();
        turnLeft();
    }
}