1 / 18

Actor

Actor. What is GridWorld?. Be thoroughly familiar with each of the actors above. Know how they move and act . you must know the inheritance relationships between the various actors.

stella
Download Presentation

Actor

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

  2. What is GridWorld?

  3. Be thoroughly familiar with each of the actors above. Know how they move and act. • you must know the inheritance relationships between the various actors. • On the AP exam you are likely to be asked to write subclasses of Bug or Critter, or to write modified methods of some given superclass. • Become familiar with the Quick Reference guide so that by the time you get to the AP exam it should be second nature to you to use the Quick Reference guide.

  4. Actor Actor is the basic object from which all other GridWorld actors will be built. Each of the new actors created will extend the original actor class.

  5. Actor Methods

  6. Instance variables from Actor public class Actor { private Grid<Actor> grid; private Location location; private int direction; private Color color; /** * Constructs a blue actor that is facing north. */ public Actor() { color = Color.BLUE; direction = Location.NORTH; grid = null; location = null; } An actor is blue when it is created. It faces North and is placed in a random location. It has a color, a location and a direction.

  7. The Actor class • The following accessor methods of the Actor class provide information about the state of the actor.

  8. The Actor class • One method enables an actor to add itself to a grid; another enables the actor to remove itself from the grid. • The putSelfInGrid method establishes the actor’s location as well as the gride in which it is placed. • The removeSelfFromGrid method removes the actor from its grid and makes the actor’s grid and location both null.

  9. The Actor class • To move an actor to a different location, use the following method. • The moveTo method allows the actor move to any valid location. • If the actor calls moveTo for a location that contains another actor, the other one remove itself from the grid and this actor moves into that location.

  10. The Actor class • You can change the direction or color of an actor with the methods below. • These Actor methods provide the tools to implement behavior for an actor. • Any class that extends Actor defines its behavior by overriding the act method.

  11. Actorclass • The act method of the Actor class reverses the direction of the actor. • You override this method in subclasses of Actor to define actors with different behaviors. • If you extend Actor without specifying an act method in the subclass, or if you add an Actor object to the grid, you can observe that the actor flips back and forth with every step.

  12. getColor() method /** * Gets the color of this actor. * @return the color of this actor */ public Color getColor() { return color; } /** * Sets the color of this actor. * @paramnewColor the new color */ public void setColor(Color newColor) { color = newColor; } Bug b = new Bug(); b.setColor(Color.ORANGE); b.getColor();

  13. Bug b = new Bug(); getDirection() Used to change the direction of a bug. setDirection(getDirection() + Location.RIGHT); setDirection(Location.EAST); /** * 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; } /** * Sets the current direction of this actor. * @paramnewDirection 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; }

  14. /** * 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 LocationgetLocation() { return location; }

  15. /** * 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; }

  16. /** * 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> * @param gr the grid into which this actor should be placed * @paramloc 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; }

  17. /** * 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; }

  18. /** * 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(Location newLocation) { 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); }

More Related