Padd Solutions

Converted by Falcon Hive

Initial Bug #1

9:37 AM 0 comments

/*
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * @author Cay Horstmann
 * @author Chris Nevison
 * @author Barbara Cloud Wells
 */

import info.gridworld.actor.Bug;

/**
 * A <code>BoxBug</code> traces out a square "box" of a given size. <br />
 * The implementation of this class is testable on the AP CS A and AB exams.
 */
public class iBoxBug extends Bug
{
    private int steps;
    private int sideLength;

    /**
     * Constructs a box bug that traces a square of a given side length
     * @param length the side length
     */
    public iBoxBug(int length)
    {
        steps = 0;
        sideLength = length;
    }

    /**
     * Moves to the next location of the square.
     */
    public void act()
    {
        if (steps < 3 && canMove())
        {
            setDirection(270);
            move();
            steps++;
        }
       
        else if (steps < 6)
        {
            setDirection(180);
            move();
            steps++;
        }
       
        else if (steps < 9)
        {
            setDirection(90);
            move();
            steps++;
        }
       
        else if (steps < 12)
        {
            setDirection(180);
            move();
            steps++;
        }
       
        else if (steps < 15)
        {
            setDirection(270);
            move();
            steps++;
        }
       
        else
        {
            turn();
        }
       
    }
}

(0) Comments

Post a Comment