Monday 14 February 2011

GRIDWORLD PART II

Gridworld: Part 2: Bug Variations: Set 2: Page 12




1. What is the role of the instance variablesideLength?
The number input will determine the number of moves the class makes until the bug turns. 
2. What is the role of the instance variablesteps?  
count the bug steps by incrementing it every move 
3. Why is the turn() method called twice when steps becomes equal to sideLength?  
because one turn method call only turns the bug halfway and in order to make it turn completley (90 degrees) we must call it twice. 
4. Why can the move() method be called in the BoxBug class when there is no move() method in the BoxBug code?  
Since it inherits the methods from its super class which contain this method. 
5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?
yes, because there is no method that changes the size of the squirrel, therefore the size will remain the same. 
6. Can the path a BoxBug travels ever change? Why or why not?  
Yes it can change,if the bug is obstructed by either a rock or the edge of the grid it will turn and change its path 
7. When will the value of steps be zero?  
I noticed it is zero right when the bug makes a turn, but there could me more options I am not sure. 


Gridworld: Part 2: Exercises: Page 13-15




1. Write a class CircleBug  that is identical to BoxBug,  except that in the act()  method the turn() method is called once instead of twice. How is its behavior different from aBoxBug?  

 public void act()
    {
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            turn();
            turn();
            steps = 0;
        }


it doesnt make 90 degree turns, but rather 45 degree turns. As a result it creates an octagon    
2. Write a class SpiralBug  that drops flowers in a spiral pattern.

Hint: Imitate BoxBug,  but adjust the side length when the bug turns. You may want to change the world to an UnboundedGrid  to see the spiral pattern more clearly.

 public void act()
    {
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            turn();
            turn();
            steps = 0;
            sideLength++;
        }
    }
I incremented the sidelength variable in order to form the spiral, it must increase every move to make sure that the bug does not hit its own trail of flowers and ruin flowers.
3. Write a class ZBug  to implement bugs that move in a “Z” pattern, starting in the top left corner. After completing one “Z” pattern, a ZBug  should stop moving.

In any step, if a ZBug  can’t move and is still attempting to complete its “Z” pattern, the ZBug  does not move and should not turn to start a new side. Supply the length of the “Z” as a parameter in the constructor. The following image shows a “Z” pattern of length 4.

Hint: Notice that a ZBug  needs to be facing east before beginning its “Z” pattern.  

    */
    public void act()
    {
        if (steps < sideLength && canMove())
        {
            move();
            steps++;
        }
        else
        {
            if (east)
                {
                    turn();
                    turn();
                    turn();
                    steps = 0;
                    east = false;
                }
            else
                {
                    turn();
                    turn();
                    turn();
                    turn();
                    turn();
                    steps = 0;
                    east = true;
I altered the act method in order to make sure the bug makes the correct number of moves to form the perfect Z. im awesome
4. Write a class DancingBug  that “dances” by making different turns before each move. The DancingBug  constructor has an integer array as parameter. The integer entries in the array represent how many times the bug turns before it moves.

For example, an array entry of 5 represents a turn of 225 degrees (recall one turn is 45 degrees). When a dancing bug acts, it should turn the number of times given by the current array entry, then act like a Bug.  In the next move, it should use the next entry in the array. After carrying out the last turn in the array, it should start again with the initial array value so that the dancing bug continually repeats the same turning pattern.

The DancingBugRunner  class should create an array and pass it as a parameter to the DancingBug  constructor.
?
5. Study the code for the BoxBugRunner  class. Summarize the steps you would use to add another BoxBug  actor to the grid.  
You would create a method that works by clicking on an empty cell
The method will consruct a bug and its parameters will be the sidelength variable

No comments:

Post a Comment