1 / 33

GridWorld

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.

neal
Download Presentation

GridWorld

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GridWorld

  2. GridWorld - Part 2 Bug Variations

  3. Bug

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

  5. import info.gridworld.actor.Bug;

  6. import info.gridworld.actor.Bug;

  7. 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();

  8. Bug

  9. open bugone.java

  10. demo boxBug.java

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

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

  13. Bug Methods in more detail

  14. Bug What does a Bug do when its act() method is called ? What methods does the act() method appear to call?

  15. Bug public void act() { if (canMove()) move(); else turn(); } Let’s look at the Bug class

  16. import info.gridworld.actor.Bug;

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

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

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

  20. 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); }

  21. turn The bug act method calls turn if canMove returns false. turn changes the direction of the bug by 45 degrees to the right.

  22. public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); }

  23. 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();

  24. open bugtwo.java

  25. Extending Bug

  26. 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?

  27. 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)

  28. Extending Bug What has to change if you want the bug to go backwards instead of forwards? Use the GW quick reference!

  29. Students partner to create BackwardBug class (5 minutes)

  30. 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?

  31. discuss BackwardBug options

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

  33. Start work on MultiSpiralBug class

More Related