1 / 12

Bug

Bug. Bug. Bug inherits from Actor. Bug is an Actor Bug inherits methods from Actor but it can create its own methods. Bug differs from actor in that a bug actually moves from cell to cell.

chet
Download Presentation

Bug

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

  2. Bug Bug inherits from Actor. Bug is an Actor Bug inherits methods from Actor but it can create its own methods. Bug differs from actor in that a bug actually moves from cell to cell. A bug moves to the cell immediately in front if it can. If a move is not possible, the bug turns in 45 degree increments until it finds a spot to which it can move.

  3. Constructor for Bug /** * Constructs a red bug. */ public Bug() { setColor(Color.RED); } /** * Constructs a bug of a given color. * @parambugColor the color for this bug */ public Bug(Color bugColor) { setColor(bugColor); } When a Bug is created with the default constructor it is Red, faces North. Direction and location are inherited from Actor. The overloaded constructor lets you select the color for the Bug, but it also faces North.

  4. Methods inherited from Actor

  5. Methods created in Bug • Bug class: - constructor method - act() - canMove() - move() - turn()

  6. canMove() The bug act method calls move if canMove returns true. canMove returns a boolean true/false if the location in front of the bug is empty or is a valid location. 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 to empty location or onto flower //not ok tomove onto any other actor. }

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

  8. turn The bug act method calls turn if canMove returns false. turn changes the direction of the bug by 45 degrees to the right. If the bug cannot move it will turn. public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); }

  9. act Act method will always be overriden in the extended classes. The bug act method will check to see if the bug can move and if so it will move (according to the move method). Else it will turn (according to the turn method)

  10. BugRunner BugRunner Class ActorWorldworld = new ActorWorld(); // must create world Bug dude = new Bug(Color.GREEN); // created a bug dude.setDirection(Location.EAST); // set the direction Location loc = new Location(5,5); // created a location world.add(loc , new Rock()); // added a rock to the location loc loc = new Location(2,5); // changed the location world.add(loc, new Flower()); // added Flower at the new loc loc = new Location(2,7); // changed loc world.add(loc, dude); // added bug at the loc world.show(); // show the world and actors

  11. Tips for Extending Bug • Create a new Class that extends Bug • Override the act method • Override other methods as needed • 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)

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

More Related