
/**
 * Robot class based on CS112 Project 1 Spring, 2008.  This class
 * is a robot that can run a real steeplechase against other
 * robots.
 * 
 * @author Russell C. Bjork
 * @version January 29, 2008
 */
import kareltherobot.*;

public class RacerRobot extends Robot implements Runnable
{
    /** 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 run()
    {
         while (! nextToABeeper())
            advanceOneStep();
         pickBeeper();
    }
    
    /** Minimum time to perform one basic operation (in ms)
     */
    public static final int MIN_OPERATION_MS = 10;
    
    /** Maximum time to perfor one basic operation (in ms)
     */
    public static final int MAX_OPERATION_MS = 500;
    
    /** Advance one step
     */
    private void advanceOneStep()
    {
        if (frontIsClear())
            move();
        else
        {
            // Climb over a hurdle
            climbUp();
            climbOverTop();
            climbDownOtherSide();
        }
    }
    
    /** Climb up a hurdle
     */
    private void climbUp()
    {
        turnLeft();
        while (! rightIsClear())
            move();
    }
    
    /** Climb over the top of a hurdle
     */
    private void climbOverTop()
    {
        turnRight();
        move();
        while (! rightIsClear())
            move();
        turnRight();
    }
    
    /** Climb down the other side of a hurdle
     */
    private void climbDownOtherSide()
    {
        while (frontIsClear())
            move();
        turnLeft();
    }
    
    /** Test to see if right side of robot is clear
     * 
     *  @return true if right side is clear
     */
    private boolean rightIsClear()
    {
        turnRight();
        if (frontIsClear())
        {  
            turnLeft();
            return true;
        }
        else
        {
            turnLeft();
            return false;
        }
    }
    
    /** Turn right 90 degrees
     * 
     */
    private void turnRight()
    {
        turnLeft();
        turnLeft();
        turnLeft();
    }
    
    // The following override the basic movement methods inherited from Robot
    // by injecting a random delay into each move.  They must be public because
    // they override a public method
       
   public void move()
    {
        RobotRace.randomPause();
        super.move();
    }
    
    public void turnLeft()
    {
        RobotRace.randomPause();
        super.turnLeft();
    }
}