Chapter4Part2 - tjapcomputerscience

Download Report

Transcript Chapter4Part2 - tjapcomputerscience

Default Critter Behavior
Pages 30-33
Moving
• Before moving, critters process other actors
– Examine them
– Move them
– Eat them
Critter process
• 2 steps involved in critter process:
– 1. Determination of which actors should be process.
– 2. Determination of how they should be process.
getActors methods of the Critter
class
• Gets a list of all neighboring actor.
• This behavior can be inherited in subclass of
Critter.
• A subclass can decide to process a different set of
actors by overriding the getActors method.
processActors method in the Critter
Class
• Eat (remove) actors that are not rocks or critters.
• This behavior is either inherited or overridden in
subclass.
Critter has completed process of
actors
• 3 steps process when the critter has completed
processing actors.
– Determination of which locations are candidates for the move.
– Selection of one of the candidates
– Make the move
• These steps are implemented in a separate method so
subclasses can change each behavior separately.
Critter implementation of the
getMoveLocations methods
• Returns all empty adjacent locations.
• In a subclass, you may want to compute a
different set of locations.
Critter implementation of the
selectMoveLocation method
• Select a random location generated from
getMoveLocations.
• Other critters may want to work harder in
picking the location they consider best.
– Most food
– Closest to their friend
– Etc…
makeMove method of the Critter
class
• Calls moveTo
• Can override the makeMove to make critters
turn, drop rocks or other actions.
Postcondition
• Postconditions of the 5 Critter methods should be
maintain if overridding these methods.
Extending the Critter Class
• The Chameleon Critter
– Defines a new type of critter that gets the same
neighboring actors as a Critter.
• Unlike the Critter Chameleon Critter
– Doesn’t process actors by eating them.
– It randomly selects one and changes its own color to
the color of the selected actor
ChameleonCritter
• The ChameleonCritter overrides the
makeMove method of the Critter class
• When a chameleonCritter moves, it turns
towards the new location.
Relationship among Actor, Critter,
ChameleonCritter, CrabCritter
Actor
extends
Critter
act
getActors
processActors
getMoveLocations
selectMoveLocation
makeMove
Extends
ChameleonCritter
CrabCritter
processActors
makeMove
getActors
getMoveLocations
makeMove
Do You Know
Set 8
Page 33 - 34 (1-6)