1 / 24

GridWorld

GridWorld. Analyzing the subclasses of Actor. Actor class (all AP testable!). Definition: An actor is an entity with a color and direction that can act. The actor is the GridWorld super class. Behavior: ????. Fields: Grid<Actor> grid Location location int direction Color color.

nanji
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 Analyzing the subclasses of Actor

  2. Actor class (all AP testable!) Definition: An actor is an entity with a color and direction that can act. The actor is the GridWorld super class. Behavior: ???? Fields: Grid<Actor> grid Location location int direction Color color

  3. Questions about an Actor’s behavior • What does an Actor know about itself? • What does an Actor do when acts? • What class does Actor extend? What interface does it implement? • How does an Actor relate to objects of other classes such as Grid objects? • What are the pre-conditions on the methods of an Actor and what do these mean for its behavior? • What are the post-conditions on the methods of an Actor and what do these mean for its behavior?

  4. Constructor /** * Constructs a blue actor that is facing north. */ public Actor() { color = Color.BLUE; direction = Location.NORTH; grid = null; location = null; }

  5. Accessors /** * Gets the color of this actor. * @return the color of this actor */ public Color getColor() { return color; } /** * Gets the current direction of this actor. * @return the direction of this actor, an angle between 0 and 359 degrees */ public int getDirection() { return direction; } /** * Gets the grid in which this actor is located. * @return the grid of this actor, or <code>null</code> if this actor is * not contained in a grid */ public Grid<Actor> getGrid() { return grid; } /** * Gets the location of this actor. * @return the location of this actor, or <code>null</code> if this actor is * not contained in a grid */ public Location getLocation() { return location; }

  6. Mutators /** * Sets the color of this actor. * @param newColor the new color */ public void setColor(Color newColor) { color = newColor; } /** * Sets the current direction of this actor. * @param newDirection the new direction. The direction of this actor is set * to the angle between 0 and 359 degrees that is equivalent to * <code>newDirection</code>. */ public void setDirection(int newDirection) { direction = newDirection % Location.FULL_CIRCLE; if (direction < 0) direction += Location.FULL_CIRCLE; }

  7. Mutators dealing with Grid /** * Puts this actor into a grid. If there is another actor at the given * location, it is removed. <br /> * Precondition: (1) This actor is not contained in a grid (2) * <code>loc</code> is valid in <code>gr</code> * @paramgr the grid into which this actor should be placed * @param loc the location into which the actor should be placed */ public void putSelfInGrid(Grid<Actor> gr, Location loc) { if (grid != null) throw new IllegalStateException( "This actor is already contained in a grid."); Actor actor = gr.get(loc); if (actor != null) actor.removeSelfFromGrid(); gr.put(loc, this); grid = gr; location = loc; } /** * Removes this actor from its grid. <br /> * Precondition: This actor is contained in a grid */ public void removeSelfFromGrid() { if (grid == null) throw new IllegalStateException( "This actor is not contained in a grid."); if (grid.get(location) != this) throw new IllegalStateException( "The grid contains a different actor at location " + location + "."); grid.remove(location); grid = null; location = null; }

  8. Mutators dealing with moving /** * Moves this actor to a new location. If there is another actor at the * given location, it is removed. <br /> * Precondition: (1) This actor is contained in a grid (2) * <code>newLocation</code> is valid in the grid of this actor * @paramnewLocation the new location */ public void moveTo(LocationnewLocation) { if (grid == null) throw new IllegalStateException("This actor is not in a grid."); if (grid.get(location) != this) throw new IllegalStateException( "The grid contains a different actor at location " + location + "."); if (!grid.isValid(newLocation)) throw new IllegalArgumentException("Location " + newLocation + " is not valid."); if (newLocation.equals(location)) return; grid.remove(location); Actor other = grid.get(newLocation); if (other != null) other.removeSelfFromGrid(); location = newLocation; grid.put(location, this); } /** * Reverses the direction of this actor. Override this method in subclasses * of <code>Actor</code> to define types of actors with different behavior * */ public void act() { setDirection(getDirection() + Location.HALF_CIRCLE); }

  9. Other methods /** * Creates a string that describes this actor. * @return a string with the location, direction, and color of this actor */ public String toString() { return getClass().getName() + "[location=" + location + ",direction=" + direction + ",color=" + color + "]"; }

  10. Questions about an Actor’s behavior • What does an Actor know about itself? • What does an Actor do when acts? • What class does Actor extend? What interface does it implement? • How does an Actor relate to objects of other classes such as Grid objects? • What are the pre-conditions on the methods of an Actor and what do these mean for its behavior? • What are the post-conditions on the methods of an Actor and what do these mean for its behavior?

  11. Questions about an Bug’s behavior • What does a Bug know about itself? • What does a Bug do when acts? • What class does Bug extend? What interface does it implement? • How does a Bug relate to objects of other classes? • What are the pre-conditions on the methods of a Bug and what do these mean for its behavior? • What are the post-conditions on the methods of a Bug and what do these mean for its behavior? • How does the Bug’s behavior differ from its super class Actor?

  12. Bug class (all AP testable!) Definition: A Bug is an actor that can move and turn. It drops flowers as it moves. Behavior: ???? Fields:inherited from Actor: Grid<Actor> grid Location location int direction Color coloradditional fields: None

  13. Constructors /** * Constructs a red bug. */ public Bug() { setColor(Color.RED); } /** * Constructs a bug of a given color. * @param bugColor the color for this bug */ public Bug(Color bugColor){ setColor(bugColor); }

  14. Constructors /** * Constructs a red bug. */ public Bug() { super(); //Java puts this in by default setColor(Color.RED); } /** * Constructs a bug of a given color. * @param bugColor the color for this bug */ public Bug(Color bugColor){ super(); //Java puts this in by default setColor(bugColor); }

  15. ASIDE: Rules about super in constructors

  16. Accessors Inherited from Actor: public Color getColor() public int getDirection() Grid<Actor> getGrid() public Location getLocation() Inherited from Actor: public Color setColor() public void setDirection() public void putSelfInGrid(Grid<Actor> gr, Location loc) public void removeSelfFromGrid() public void moveTo(Location newLocation) public void act() Additional methods: public void act() public void turn() public void move() Mutators Other public boolean canMove()

  17. Additional Methods /** * Moves if it can move, turns otherwise. */ public void act() { if (canMove()) move(); else turn(); } /** * Turns the bug 45 degrees to the right without changing its location. */ public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); }

  18. Additional Methods /** * Moves the bug forward, putting a flower into the location it previously * occupied. */ 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); } /** * Tests whether this bug can move forward into a location that is empty or * contains a flower. * @return true if this bug can move. */ 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 }

  19. Questions about an Bug’s behavior • What does a Bug know about itself? • What does a Bug do when acts? • What class does Bug extend? What interface does it implement? • How does a Bug relate to objects of other classes? • What are the pre-conditions on the methods of a Bug and what do these mean for its behavior? • What are the post-conditions on the methods of a Bug and what do these mean for its behavior? • How does the Bug’s behavior differ from its super class Actor?

  20. BoxBug • Ask questions about behavior and show code

  21. CircleBug, SpiralBug, • Ask questions about behavior and show code needed to back up your answers.

  22. Flower, Rock • Ask questions about behavior and show code needed to back up your answers.

  23. Zbug, Dancing Bug • Ask questions about behavior and show code needed to back up your answers.

  24. Your Bug??? • Ask questions about behavior and show code needed to back up your answers.

More Related