330 likes | 512 Views
GridWorld. GridWorld - Part 2 Bug Variations. Bug. Bug. Bug extends Actor but differs from Actor in that a Bug actually moves from cell to cell.
E N D
GridWorld - Part 2 Bug Variations
Bug Bug extends Actor but differs from Actor in that a Bug actually moves from cell to cell. A Bug moves to the cell immediately in front if possible. If a move is not possible, the bug turns in 45 degree increments until it finds a spot to which it can move.
Bug ActorWorld world = new ActorWorld(); Bug dude = new Bug(); world.add(new Location(3,3), dude); Bug sally = new Bug(Color.GREEN); sally.setDirection(Location.SOUTHEAST); world.add(new Location(2,2), sally); Bug ali = new Bug(Color.ORANGE); ali.setDirection(Location.NORTHEAST); world.add(new Location(1,1), ali); world.show();
open bugone.java
demo boxBug.java
Set 2 Questions 1. What is the role of the instance variable sideLength? The sideLength instance variable defines the number of steps a BoxBug moves on each side of its box. 2. What is the role of the instance variable steps? The steps instance variable keeps track of how many steps a BoxBug has moved on the current side of its box. 3. Why is the turn method called twice when steps becomes equal to sideLength? When a BoxBug travels sideLength steps, it has to turn 90 degrees to travel along the next side of its box. The turn method only executes a 45 degree turn; therefore it takes two turn method calls to turn 90 degrees. 4. Why can the move method be called in the BoxBug class when there is no move method in the BoxBug code? The BoxBug class extends the Bug class, and the Bug class has a public move method. Since the BoxBug class is a subclass of the Bug class, it inherits the move() method from the Bug class.
Set 2 Questions 5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not? Yes. When a BoxBug is constructed, the side length is determined and cannot be changed by client code. 6. Can the path a BoxBug travels ever change? Why or why not? Yes. If another Actor, like a Rock or Bug, is in front of a BoxBug when it tries to move, the BoxBug will turn and start a new box path. 7. When will the value of steps be zero? Initially, the value of steps is set to zero when a BoxBug is constructed. After that, the value of steps will be set to zero when steps is equal to sideLength—meaning the BoxBug has completed one side of its box path, or when the BoxBug cannot move and turns instead to start a new box path.
Bug Methods in more detail
Bug What does a Bug do when its act() method is called ? What methods does the act() method appear to call?
Bug public void act() { if (canMove()) move(); else turn(); } Let’s look at the Bug class
canMove The bug act method looks to see if a move is possible by calling canMove. canMove looks at the location in front of this bug to see if it is empty or if it contains a flower. canMove returns true or false.
public boolean canMove() { Grid <Actor> gr = getGrid(); if (gr == null) return false; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (!gr.isValid(next)) return false; Actor neighbor = gr.get(next); return (neighbor == null) || (neighbor instanceof Flower); // ok to move into empty location or onto flower // not ok to move onto any other actor } instanceof keyword checks to see if object is a certain type
move The bug act method calls move if canMove returns true. move calls moveTo to move the bug to the location in front of this bug. move leaves a flower in the old location.
public void move() { Grid <Actor> gr = getGrid(); if (gr == null) return; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (gr.isValid(next)) moveTo(next); else removeSelfFromGrid(); Flower flower = new Flower(getColor()); flower.putSelfInGrid(gr,loc); }
turn The bug act method calls turn if canMove returns false. turn changes the direction of the bug by 45 degrees to the right.
public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); }
Bug ActorWorld world = new ActorWorld(); Bug dude = new Bug(Color.GREEN); dude.setDirection(Location.EAST); Location loc = new Location(5,5); world.add(loc , new Rock()); loc = new Location(2,5); world.add(loc, new Flower()); loc = new Location(2,7); world.add(loc, dude); world.show();
open bugtwo.java
Extending Bug
Extending Bug Questions you need to ask • How will the new bug differ from the original bug? • Can the new behavior be created using existing methods? • Which of the methods will be overridden? • Will new methods need to be added?
Tips for Extending Bug • Override Bug's act method • Each call to the move() method should be guarded by a call to the canMove() method • Add additional instance fields if needed • Add new methods to the subclass if needed • Constructors for the subclass call super() or super(someColor)
Extending Bug What has to change if you want the bug to go backwards instead of forwards? Use the GW quick reference!
Students partner to create BackwardBug class (5 minutes)
Extending Bug public class BackwardBug extends Bug { //constructor public void act() { } //other methods } Is this the only way to write this class? What methods could be changed?
discuss BackwardBug options
What have we learned about Bugs? • Bug class: - constructor method - act() - canMove() - move() - turn() • Grid class: - isValid() - get() - getNumRows() - getNumCols() • Location class: - getRow() - getCol() - getAdjacentLocation() • Actor class: - getGrid() - putSelfInGrid() - removeSelfFromGrid() - moveTo() • BoxBug class: - overriding act() - using instance variables to control movement of a bug
Start work on MultiSpiralBug class