18

Actor

  • Upload
    stella

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Actor
Page 2: Actor
Page 3: Actor

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

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

Page 5: Actor

Actor Methods

Page 6: Actor

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.

Page 7: Actor

The Actor class• The following accessor methods of

the Actor class provide information about the state of the actor.

Page 8: Actor

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.

Page 9: Actor

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.

Page 10: Actor

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.

Page 11: Actor

Actor class

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

Page 12: Actor

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. * @param newColor the new color */ public void setColor(Color newColor) { color = newColor; }

Bug b = new Bug();

b.setColor(Color.ORANGE);

b.getColor();

Page 13: Actor

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

Bug b = new Bug();

getDirection()

Used to change the direction of a bug.

setDirection(getDirection() + Location.RIGHT);

setDirection(Location.EAST);

Page 14: Actor

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

Page 15: Actor

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

Page 16: Actor

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

Page 17: Actor

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

Page 18: Actor

/** * 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 * @param newLocation 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); }