Problem 10 p.98

class Fence : Robot
{

        Boolean isTopOfFence();
        Boolean isBottomOfFence();
        void turnAround();
        void goUpFence();
        void goDownFence();
        void moveToNextFence();
        void goOverOneFence();
        void runTheRace();
};

Boolean Fence :: IsTopOfFence()
{
        return !nextToABeeper();
}

Boolean Fence :: IsBottomOfFence()
{
        return !frontIsClear();
}

void Fence :: turnAround()
{
        turnLeft();
        turnLeft();
}

void Fence :: goUpFence()
{
        if (!isTopOfFence())
        {
                pickBeeper();
                move();
                if (!isTopOfFence())
                {
                        pickBeeper();
                        move();
                        if (!isTopOfFence())
                        {
                                pickBeeper();
                        }
                }
        }
}

void Fence :: goDownFence()
{
        if (!isBottomOfFence())
        {
                move();
                if (!isBottomOfFence())
                {
                        move();
                }
        }
        turnLeft();
}

void Fence :: moveToNextFence()
{
        move();
        turnLeft();
}

void Fence :: goOverOneFence()
{
        goUpFence();
        turnAround();
        goDownFence();
}

void Fence :: runTheRace()
{
        loop(8)
        {
                moveToNextFence();
                goOverOneFence();
        }
}

task
{
        Fence Joe (1, 1, East, 0);
        Joe.runTheRace();
        Joe.turnOff();
}