67
GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two-dimensional grid. During a single step of the program, every occupant of the grid gets a chance to act. Each actor acts according to a clearly specified set of behaviors that can include moving, changing color, changing direction, or removing other actors from the grid. AP CS 2009 1

GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

Embed Size (px)

DESCRIPTION

The Classes of GridWorld AP CS ACTOR GRID INTERFACE ABSTRACT GRID LOCATION UNBOUNDED GRID BOUNDED GRID

Citation preview

Page 1: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

GridWorld Case Study

The case study is a program that simulates actions and interactions of objects in a two-dimensional grid.

During a single step of the program, every occupant of the grid gets a chance to act.

Each actor acts according to a clearly specified set of behaviors that can include moving, changing color, changing direction, or removing other actors from the grid.

AP CS 20091

Page 2: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Classes of GridWorld

AP CS 2009 2

ACTOR

FLOWER ROCK

CHAMELEONCRITTER

CRITTER

BOXBUG

BUG

Page 3: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Classes of GridWorld

AP CS 2009 3

ACTORGRID

INTERFACE

ABSTRACT GRID

LOCATION

UNBOUNDED GRID

BOUNDED GRID

Page 4: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Actors of GridWorld A rock does nothing A flower darkens its color A bug moves forward when it can. It can move

into any empty spot or onto a flower. When it moves, it deposits a flower in its previous location. If it moves to a location occupied by a flower, that flower is removed from the grid. A bug cannot move if it is blocked in front by either another (non-flower) actor or the edge of the grid. When it is prevented from moving, it turns 45 degrees to the right.

AP CS 2009 4

Page 5: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Actors (continued) A BoxBug moves like a bug. Additionally, if it

encounters no obstacles in its path, it traces out a square of flowers with a given side length. If a BoxBug is blocked from moving, it makes two right turns and starts again.

A Critter gets a list of its adjacent neighboring actors and processes them by “eating” each actor that is not a rock or another critter. It then randomly selects one of the empty neighboring locations and moves there. If there are not available empty locations, a critter does not move.

AP CS 2009 5

Page 6: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Actors (continued)

A ChameleonCritter gets a list of its adjacent neighbors, randomly picks one of them, and changes its color to that of the selected actor. The ChameleonCritter moves like a Critter but, additionally, it first changes its direction to face its new location before moving.

AP CS 2009 6

Page 7: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Location Class

Row and Column values representing positions on the grid.

Provides constants for compass directions and turn angles.

Provides methods for determining relationships between locations and compass directions.

AP CS 2009 7

Page 8: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Location Class Constant int ValueLocation.NORTH 0Location.EAST 90Location.SOUTH 180Location.WEST 270Location.NORTHEAST 45Location.SOUTHEAST 135Location.SOUTHWEST225Location.NORTHWEST 315

AP CS 2009 8

Page 9: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Location Class

Constant int Value Location.LEFT -90 Location.RIGHT 90

Location.HALF_LEFT -45Location.HALF_RIGHT 45

Location.FULL_CIRCLE 360Location.HALF_CIRCLE 180Location.AHEAD 0

AP CS 2009 9

Page 10: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Location Class

To get an actor to turnsetDirection(getDirection() + Location.RIGHT);

Methods of the Location classgetRow, getCol, getAdjacentLocation,

getDirectionToward, equals, compareTo, toString

AP CS 2009 10

Page 11: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Actor Class The Actor class is a superclass for every

creature that appears in the grid. An actor has a location, direction and color.

It can also change each of these instance variables using setColor, and setDirection.

It has access to it’s grid. It can putSelfInGrid and removeSelfFromGrid, thus changing its location.

It also has methods called moveTo, act, toString, getColor, getDirection and setGrid.

AP CS 2009 11

Page 12: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Rock and Flower Classes A rock acts by doing nothing. It has a default

constructor that creates a black rock and a second constructor that allows construction of a rock with a specified color. The act method is overridden – it has an empty body.

A flower acts by darkening its color. It has a default constructor that creates a pink flower, and a second constructor that allows a construction of a flower with a specified color. The overridden act method darkens the flower’s color.

AP CS 2009 12

Page 13: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Bug Class

A bug is an actor that moves forward in a straight line, turning only when it is blocked. A bug can be blocked by either the edge of the grid, or an actor that is not a flower.

As a bug moves, it steps on any flower in its path, causing the removal of that flower from the grid. After each step, the bug places a flower in its previous location.

AP CS 2009 13

Page 14: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

The Bug Class

The bug class contains the following methods: act turn move canMoveThe act method is overridden.

AP CS 2009 14

Page 15: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 15

GridWorldAn Introduction

Part 1

Page 16: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 16

Overview Case Study contains 5 Parts:

Part 1: Provides experiments to observe the attributes and behavior of the actors.

Part 2: Defines Bug variations. Part 3: Explores the code that is needed to

understand and create actors. Part 4: Defines classes that extend the Critter class.

Part 5: (CS AB only) Explains grid data structures.

Page 17: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 17

Part 1: Observing and Experimenting with GridWorld The first look at GridWorld

Exploring Actor state and behavior Exploring Bug state and behavior Exploring Flower state and behavior Exploring Rock state and behavior

Demo: BugRunner EXPLORATION time

Page 18: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 18

BugRunner(code)

public static void main(String[] args) { ActorWorld world = new ActorWorld(); world.add(new Bug()); world.add(new Rock()); world.show(); }

Page 19: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 19

BugRunner Exploration You can add a new Bug, Rock, Flower, or Actor An Actor can be added to the Grid if any Actor

subclass has been added in the BugRunner class.

Remember the IS-A relationship of Inheritance; a Bug IS-A Actor a Rock IS-A Actor a Flower IS-A Actor

Page 20: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 20

GridWorldPart 2

Page 21: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 21

Part 2: Bug Variations

Simple Inheritance

Demo: BoxBug (testable code) BoxBugRunner

Page 22: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 22

BoxBugRunner

public static void main(String[] args) { ActorWorld world = new ActorWorld(); BoxBug alice = new BoxBug(6); alice.setColor(Color.ORANGE); BoxBug bob = new BoxBug(3); world.add(new Location(7, 8), alice); world.add(new Location(5, 5), bob); world.show(); }

Page 23: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 23

BoxBug Exploration

Run the program What kind of objects can you add to the grid?

Create a world with a Bug and a BoxBug Right click on a Bug. Right click on a BoxBug.

Do you see any differences?

Page 24: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 24

BoxBug

You will need to understand Bug constructors Act method

You will need to use canMove turn move You will create a new type of Bug by modifying BoxBug

code

Page 25: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 25

BoxBug Codepublic class BoxBug extends Bug{ private int steps; private int sideLength;

public BoxBug(int length) { super();

steps = 0; sideLength = length; }

public void act(){ if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); turn(); steps = 0; } }}

Page 26: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 26

Extending the Bug class

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)

Page 27: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 27

Extending the Bug classCircleBug SpiralBug (unbounded grid)

UnboundedGrid<Actor> grid = new UnboundedGrid<Actor>();ActorWorld world = new ActorWorld(grid);

Page 28: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 28

BoundedGrid vs. UnboundedGrid ActorWorld world = new ActorWorld();

world.add(new Bug());world.add(new Rock());

UnboundedGrid<Actor> grid = new UnboundedGrid<Actor>();

ActorWorld world = new ActorWorld(grid);

SpiralBug sp = new SpiralBug(3);

world.add(new Location(11,9),sp);

Page 29: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 29

Extending the Bug classZBug DancingBug (array)

Page 30: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 30

GridWorldPart 3

Page 31: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 31

Part 3: GridWorld Classes and Interfaces A grid contains Actors

Two types of grids bounded unbounded

Locations in the grid are represented by objects of type Location An Actor knows what grid it lives in and its location within that grid.

Page 32: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 32

Location

implements Comparable interface Built in compass directions

public static final int SOUTH = 180; public static final int WEST = 270; public static final int NORTHEAST = 45;

Built in turn directions public static final int RIGHT = 90; public static final int HALF_LEFT = -45; public static final int HALF_RIGHT = 45;

Page 33: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 33

Location

accessorsgetRow()getCol()

Page 34: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 34

Location

public Location getAdjacentLocation(int direction)returns the adjacent location in the compass direction that is closest to direction

public int getDirectionToward(Location target)returns the closest compass direction from this location toward target

Page 35: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 35

LocationLocation loc1 = new Location(4, 3); Location loc2 = new Location(3, 4);

Location loc3 = loc2.getAdjacentLocation(Location.SOUTH);int dir = loc1.getDirectionToward(new Location(6, 5));

Page 36: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 36

Grid interface

The interface Grid<E> specifies the methods for any grid that contains objects of the type E. Two classes, BoundedGrid<E> and UnboundedGrid<E> implement the interface.

Page 37: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 37

Grid interfaceboolean isValid(Location loc)returns true if loc is valid in this grid, false otherwise Precondition: loc is not null

E put(Location loc, E obj)puts obj at location loc in this grid and returns the object previously at that

location (or null if the location was previously unoccupied)Precondition: (1) loc is valid in this grid (2) obj is not null

E remove(Location loc)removes the object at location loc and returns it (or null if the location is

unoccupied)Precondition: loc is valid in this grid

E get(Location loc)returns the object at location loc (or null if the location is unoccupied)

Precondition: loc is valid in this grid

Page 38: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 38

Grid interfaceArrayList<Location> getOccupiedLocations()returns all occupied locations in a grid. 

ArrayList<Location> getValidAdjacentLocations(Location loc)returns all valid locations adjacent to loc in this grid

Precondition: loc is valid in this grid

ArrayList<Location> getEmptyAdjacentLocations(Location loc)returns all valid empty locations adjacent to loc in this grid

Precondition: loc is valid in this grid

ArrayList<Location> getOccupiedAdjacentLocations(Location loc)returns all valid occupied locations adjacent to loc in this grid

Precondition: loc is valid in this grid

ArrayList<E> getNeighbors(Location loc)returns all objects in the occupied locations adjacent to loc in this grid

Precondition: loc is valid in this grid

Page 39: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 39

Grid interface

How can you obtain a count of the objects in a grid? How can you obtain a count of the empty locations in a bounded grid?

Page 40: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 40

The Actor class Accessor methods

public Color getColor() public int getDirection() public Grid<Actor> getGrid() public Location getLocation()

Other Actor Methods public void putSelfInGrid(Grid<Actor> gr,

Location loc)establishes the actor’s location as well as the grid in which it is

placed

public void removeSelfFromGrid()removes the actor from its grid and makes the actor’s grid and

location both null.

Page 41: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 41

The Actor class

When adding or removing actors, do not use the put and remove methods of the Grid interface. Those methods do not update the location and

grid instance variables of the actor. That is a problem since most actors behave incorrectly if they do not know their location. To ensure correct actor behavior, always use the putSelfInGrid and removeSelfFromGrid methods of the Actor class.

Page 42: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 42

The Actor class

The moveTo method allows the actor to move to any valid location. If the actor calls moveTo for a location that contains another actor, the other one removes itself from the grid and this actor moves into that location.

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

Page 43: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 43

The Actor class

Can an actor put itself into a grid twice without first removing itself?

Can an actor remove itself from a grid twice?

Can an actor be placed into a grid, remove itself, and then put itself back?

Try it out. What happens?

Page 44: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 44

Rock, Flower, Bug

Time to look at code! Rock behavior? Flower behavior? Bug behavior?

Page 45: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 45

Group Activity: Jumper

Each group creates a class called Jumper. This actor can move forward two cells in each move. It “jumps” over rocks and flowers. It does not leave anything behind it when it jumps.

Page 46: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 46

Group Activity: Jumper

1. What will a Jumper do if the location in front of it is empty, but the location two cells in front contains a flower or a rock?

2. What will a Jumper do if the location two cells in front of the Jumper is out of the grid?

3. What will a Jumper do if it is facing an edge of the grid?

4. What will a Jumper do if another actor (not a flower or a rock) is in the cell that is two cells in front of the jumper?

5. What will a Jumper do if it encounters another Jumper in its path?

6. Are there any other tests the Jumper needs to make?

Page 47: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 47

Group Activity: Jumper

1. Which class should Jumper extend? 2. Is there an existing class that is similar to the

Jumper class? 3. Should there be a constructor? If yes, what

parameters should be specified for the constructor?

4. Which methods should be overridden? 5. What methods, if any, should be added? 6. What is the plan for testing the class?

Page 48: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 48

GridWorldPart 4

Page 49: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 49

Introduces the Critter class (testable code) Uses template method, act

It is usually not a good idea to override the act method in a Critter subclass. The Critter class was designed to represent actors that process other actors and then move. If you find the act method unsuitable for your actors, you should consider extending Actor, not Critter.

The act method calls five methods accomplishing the following tasks: find neighbors (getActors) process neighbors (processActors) find candidates for move locations

(getMoveLocations) select a move location (selectMoveLocation) make a move (makeMove)

Page 50: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 50

Critters demo:

Critter ChameleonCritter (testable code) CrabCritter (extra demo code)

Page 51: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 51

act method

public void act() { if (getGrid() == null) return; ArrayList<Actor> actors = getActors(); processActors(actors); ArrayList<Location> moveLocs = getMoveLocations(); Location loc = selectMoveLocation(moveLocs); makeMove(loc); }

Page 52: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 52

getActors method

public ArrayList<Actor> getActors()

Gets the actors for processing. Implemented to return the actors that occupy the neighboring grid locations. Override this method for subclasses that look elsewhere for actors to process. Returns a list of actors that are neighbors of this critter.

Postcondition: The state of all actors is unchanged.

Page 53: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 53

processActors method

public void processActors(ArrayList<Actor> actors)Processes the actors. This method is implemented to "eat"

(i.e. remove) all actors that are not rocks or critters. Override it in subclasses that process neighbors in a different way.

Postconditions: (1) The state of all actors in the grid other than this critter

and the elements of actors is unchanged. (2) The location of this critter is unchanged.

Page 54: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 54

getMoveLocations method

public ArrayList<Location> getMoveLocations()

Get the possible locations for the next move. Implemented to return the empty neighboring locations. Override this method for subclasses that look elsewhere for move locations. Returns a list of possible locations for the next move.

Postcondition: The state of all actors is unchanged.

Page 55: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 55

selectMoveLocation method public Location selectMoveLocation(ArrayList<Location>

locs)

Selects the location for the next move. Implemented to randomly pick one of the possible locations, or to return the current location if locs has size 0. Override this method for subclasses that have another mechanism for selecting the next move location.

Postconditions: (1) The returned location is an element of locs, this

critter's current location, or null. (2) The state of all actors is unchanged.

Page 56: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 56

makeMove methodpublic void makeMove(Location loc)

Moves this critter to the given location loc, or removes this critter from its grid if loc is null. An actor may be added to the old location. If there is a different actor at location loc, that actor is removed from the grid. Override this method in subclasses that want to carry out other actions (for example, turning this critter or adding an occupant in its previous location).

Postconditions: (1) getLocation() == loc. (2) The state of all actors other than those at the old

and new locations is unchanged.

Page 57: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 57

Override one or more of these methods when creating a new Critter subclass.

The Narrative states:It is usually not a good idea to override the act method in a

Critter subclass. The Critter class was designed to represent actors that process other actors and then move. If you find the act method unsuitable for your actors, you should consider extending Actor, not Critter.

Page 58: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 58

Design issues

The postconditions help to ensure that subclasses implement behavior that is consistent with the purpose of each piece.

Page 59: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 59

ChameleonCritter

The ChameleonCritter class defines a new type of critter that gets the same neighboring actors as a Critter. However, unlike a Critter, a ChameleonCritter doesn’t eat them. Instead, a ChameleonCritter randomly selects one and changes its own color to the color of the selected actor. When a ChameleonCritter moves, it turns toward the new location.

Page 60: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 60

ChameleonCritter (testable code) ChameleonCritter overrides which methods????

Randomly selects a neighbor and changes this critter's color to be the same as that neighbor's. If there are no neighbors, no action is taken.

Turns towards the new location as it moves

getActors processActors getMoveLocations selectMoveLocation

makeMove

Page 61: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 61

ChameleonCritter (testable code) ChameleonCritter overrides processActors and makeMove.

Randomly selects a neighbor and changes this critter's color to be the same as that neighbor's. If there are no neighbors, no action is taken.

Turns towards the new location as it moves

Page 62: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 62

CrabCritter

A CrabCritter is a critter that eats whatever is found in the locations immediately in front, to the right-front, or to the left-front of it. It will not eat a rock or another critter. A CrabCritter can move only to the right or to the left. If both locations are empty, it randomly selects one. If a CrabCritter cannot move, then it turns 90 degrees, randomly to the left or right.

Page 63: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 63

CrabCritter A crab gets the actors in the three locations immediately

in front, to its front-right and to its front-left possible move locations are immediately to the right and

to the left If the crab critter doesn't move, it randomly turns left or

right Which methods does the CrabCritter override?getActors processActors getMoveLocations selectMoveLocation

makeMove

Page 64: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 64

CrabCritter

CrabCritter overrides getActors, getMoveLocations, and makeMove.

A crab gets the actors in the three locations immediately in front, to its front-right and to its front-left

possible move locations are immediately to the right and to the left

If the crab critter doesn't move, it randomly turns left or right

Page 65: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 65

Creative Critters…

A ChameleonKid changes its color to the color of one of the actors immediately in front or behind. If there is no actor in either of these locations, then the ChameleonKid darkens like the modified ChameleonCritter.

A RockHound gets the actors to be processed in the same way as a Critter. It removes any rocks in that list from the grid. A RockHound moves like a Critter.

Page 66: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 66

Creative Critters…

Create a class KingCrab that extends CrabCritter. A KingCrab gets the actors to be processed in the same way a CrabCritter does. A KingCrab causes each actor that it processes to move one location further away from the KingCrab. If the actor cannot move away, the KingCrab removes it from the grid. When the KingCrab has completed processing the actors, it moves like a CrabCritter.

Page 67: GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of

AP CS 2009 67

Overview of GridWorld Creatures Actors

Bugs extending Bug: override Act

Rocks Flowers Critters

DO NOT OVERRIDE ACT!!! extending Critter overrides one or more of the 5

methods