Tuesday, 1 February 2011

Gridworld Problems

 Page 6 Questions
1. Does the bug always move to a new location? Explain.
    No, it can turn.

2. In which direction does the bug move?
    Straight if it can, unless it hits a rock or a wall and must turn clockwise.

3. What does the bug do if it does not move?
    It turns it's direction (if something is in its path).

4. What does a bug leave behind when it moves?
    It leaves behind a big flower that takes up the cell.

5. What happens when the bug is at an edge of the grid? (Consider whether the bug is facing the edge as well as whether the bug is facing some other direction when answering this question.)
    It turns 90 degrees clockwise.

6. What happens when a bug has a rock in the location immediately in front of it?
    It turns 45 degrees clockwise (that's to say it turns diagonally).

7. Does a flower move?
    Nope it's position is static.

8. What behavior does a flower have?
    It doesn't move, but for each step it's color becomes darker until it's pitch black.

9. Does a rock move or have any other behavior?
    No it doesn't move or act, it simply remains in its place and acts as a block to the bugs.

10. Can more than one actor (bug, flower, rock) be in the same location in the grid at the same time?
    No, only one object can take up a cell on the grid.

Page 8 Questions
1. Test the setDirection method with the following inputs and complete the table, giving the compass direction each input represents.
0 - North
45 - NE
90 - East
135 - SE
180 - South
225 - SW
270 - West
315 - NW
360 - North

2. Move a bug to a different location using the moveTo method. In which directions can you move it? How far can you move it? What happens if you try to move the bug outside the grid?
2. You can place it on any place on the board, but it keeps its direction. If you try to place it outside you get an error.

Change the color of a bug, a flower, and a rock. Which method did you use?
3. I used the setColor() method

4. Move a rock on top of a bug and then move the rock again. What happened to the bug?
4. The bug vanished as though it died. A blank cell remained.

Questions on Page 12
1. What's the role of the instance variable sideLength?
1. It serves as a step limit on one side. If you say your sideLength is 2, and assuming there are no rocks around, the bug will walk in a box of length to. Furthermore, it will walk two steps on each side and then turn, and then another two steps, and then turn again, etc. For this reason the class is called BoxBug, it walks in a box shape so to speak.

2. What is the role of the instance variable steps?
2. The steps serve as a count to know how many steps the bug can take until he must turn due to the sideLength variable.

3. Why is the turn method called twice when steps becomes equal to sideLength?
3. The turn method is called twice when steps is equal to sideLength because the BoxBug again is made to walk in a box, and each point in a box is 90 degrees. Since the turn method turns the bug 45 degrees it must be called twice to make the bug turn 90 degrees to complete the box pattern. Also the BoxBug is made to turn 90 degrees not only in the box pattern. If it encounters a rock it will also call the turn method twice.

4. Why can the move method be called in the BoxBug class when there is no move method in the BoxBug code?
4. It can be called because the move method is inherited from the Bug class. Since it didn't have to be changed it wasn't overriden in the BoxBug class and that's the only reason why it isn't in the BoxBug code.

5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?
5. It won't be the same given that other objects are on the grid. If the BoxBug cannot move, and still hasn't completed all the steps on one side, it will still turn and break the pattern.

6. Can the path a BoxBug travels ever change? Why or why not?
As was mentioned earlier, it will not change unless other objects exist on the grid, which will affect it's turning.

7. When will the value of steps be zero?
7. The value of steps will be zero whenever the BoxBug turns, and at first when it's constructed.

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 a BoxBug?
1. 
/**
 * Write a description of class CircleBug here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class CircleBug extends BoxBug
{
    /**
     * Constructor for objects of class CircleBug
     */
    public CircleBug(int length)
    {
        super(length);
    }

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


2. Write a class SpiralBug  that drops flowers in a spiral pattern.
2. /**
 * Write a description of class CircleBug here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class SpiralBug extends BoxBug
{
    /**
     * Constructor for objects of class CircleBug
     */
    public SpiralBug(int length)
    {
        super(length);
    }

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

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.

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.  

/**
 * Write a description of class CircleBug here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class ZBug extends BoxBug
{
    /**
     * Constructor for objects of class CircleBug
     */
    public ZBug(int length)
    {
        super(length);
        turn();
        turn();
    }

    public void break()
    {
        //didn't know how to implement, but this method is simply supposed to make the bug break all his operations
    }
   
    public void act()
    {
        boolean first = true;
        boolean stop = false;
        if(stop || !canMove())
        {
            break();
        }
        if(steps < sideLength )
        {
            move();
            steps++;
        }
        else if(first)
        {
            turn();
            turn();
            turn();
            steps = 0;
            first = false;
        }
        else if(!first)
        {
            turn();
            turn();
            turn();
            turn();
            turn();
            steps = 0;
            stop = true;
        }
    }
    }

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.
4. 

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.

import java.util.Random;
/**
 * Write a description of class DancingBug here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class DancingBug extends Bug
{
    private int[] turns;
    /**
     * Constructor for objects of class DancingBug
     */
    public DancingBug(int length)
    {
        turns = new int[length];
        Random R = new Random();
        for(int i = 0; i<turns.length; i++)
        {
            turns[i] = R.nextInt(10);
        }
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    public void act()
    {
        int z = 0;
        if(z = turn.length)
           z = 0;
        if(canMove() )
        {
          while(turn[z] > 0)
          {
              turn();
              turn[z]--;
            }
          move();
        }
        else
        {
          while(turn[z] > 0)
              turn();
              turn[z]--;
        }
    }
}

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.
5. To add another BoxBug to the grid would take two steps. First we must initialize the BoxBug with the parameters it takes. Then we would have to add it to the grid. After that, the BoxBug that we have created will appear on the grid.

1 comment:

  1. Jackpot Party Casino Review, Bonus & Promo Code - JTHub
    Read our full review and review of Jackpot Party Casino. It covers the sports, casino, 경상남도 출장마사지 live casino, live/table games, and much more. Get 경산 출장안마 the 상주 출장마사지 full review here!Games: Slots, Blackjack, Roulette, Video SlotsLive: Yes! Rating: 3.8 · 거제 출장안마 ‎Review by JT Hub 경기도 출장안마

    ReplyDelete