Critters

advertisement
Critters
GridWorld Part 4
Part 4: Interacting Objects
• Critters are actors that have a common pattern for behavior
– Use the same act() method
• When a critter acts it:
– gets a list of actors to process
• ArrayList<Actor> getActors()
– processes the actors
• void processActors(ArrayList<Actor> actors);
– generates a list of locations it can move to
• ArrayList<Location> getMoveLocations()
– selects a location
• Location selectMoveLocation(ArrayList<Location> locList)
– moves to the selected location
• void makeMove(Location loc)
GridWorld Case Study
2
Exercise Set 7
1. What methods are implemented in Critter? What
does each do?
2. What are the 5 basic actions common to all Critters
when they act?
3. Should subclasses of Critter override the getActors()
method?
4. Describe 3 ways a Critter could process actors?
5. What 3 methods must be invoked to make a Critter
move? Explain each method.
6. Why is there no Critter constructor?
GridWorld Case Study
3
Extending the Critter Class
• ChameleonCritter
– Overrides processActors
to pick a random actor
and change its current
color to the actor’s color
– Overrides makeMove to
also turn toward the
new location
GridWorld Case Study
4
Exercise Set 8
1. Why does act cause a ChameleonCritter to act
differently than a Critter even though act is not
overriden?
2. Why does the makeMove method of
ChameleonCritter call super.makeMove?
3. How would you make a ChameleonCritter drop a
flower in the old location when it moves?
4. Why doesn’t ChameleonCritter override the
getActors method?
5. Which class contains the getLocation method?
6. How can a Critter access its own grid?
GridWorld Case Study
5
Extending the Critter Class
• CrabCritter
– Overrides getActors to
get actors straight
ahead, diagonal left, and
diagonal right (from
front)
– Overrides
getMoveLocations to
only move to the left or
right
– Overrides makeMove so
that if it doesn’t move it
randomly turns left or
right
GridWorld Case Study
6
Exercise Set 9
1. Why doesn’t CrabCritter override the processActors method?
2. Describe the process CrabCritter uses to find and eat actors.
1. Does it always eat all neighboring actors?
2. Explain.
3. Why is the getLocationsInDirections method used in CrabCritter?
4. If a CrabCritter has location (3,4) and faces south what are the possible
locations for actors returned by getActors?
5. What are the similarities and differences between the movements of a
CrabCritter and a Critter?
6. How does a CrabCritter determine when it turns instead of moving?
7. Why don’t the CrabCitters objects eat each other?
GridWorld Case Study
7
Make a New Critter –
Group Activity Page 36
• Requirements: Working with a partner, specify a new
creature that extends Critter. Must specify
properties and behavior in detail. Write test cases.
• Design: Trade requirements with another pair and
design the newCritter class
• Code: Implement the critter you designed
• Test: Use test cases given by requirements writers
• When you finish, design and code a new Critter of
your own
GridWorld Case Study
8
Download