Critters Grid World Part 4 Part 4 Interacting








- Slides: 8
Critters Grid. World 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 • Array. List<Actor> get. Actors() – processes the actors • void process. Actors(Array. List<Actor> actors); – generates a list of locations it can move to • Array. List<Location> get. Move. Locations() – selects a location • Location select. Move. Location(Array. List<Location> loc. List) – moves to the selected location • void make. Move(Location loc) Grid. World 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 get. Actors() 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? Grid. World Case Study 3
Extending the Critter Class • Chameleon. Critter – Overrides process. Actors to pick a random actor and change its current color to the actor’s color – Overrides make. Move to also turn toward the new location Grid. World Case Study 4
Exercise Set 8 1. Why does act cause a Chameleon. Critter to act differently than a Critter even though act is not overriden? 2. Why does the make. Move method of Chameleon. Critter call super. make. Move? 3. How would you make a Chameleon. Critter drop a flower in the old location when it moves? 4. Why doesn’t Chameleon. Critter override the get. Actors method? 5. Which class contains the get. Location method? 6. How can a Critter access its own grid? Grid. World Case Study 5
Extending the Critter Class • Crab. Critter – Overrides get. Actors to get actors straight ahead, diagonal left, and diagonal right (from front) – Overrides get. Move. Locations to only move to the left or right – Overrides make. Move so that if it doesn’t move it randomly turns left or right Grid. World Case Study 6
Exercise Set 9 1. Why doesn’t Crab. Critter override the process. Actors method? 2. Describe the process Crab. Critter uses to find and eat actors. 1. Does it always eat all neighboring actors? 2. Explain. 3. Why is the get. Locations. In. Directions method used in Crab. Critter? 4. If a Crab. Critter has location (3, 4) and faces south what are the possible locations for actors returned by get. Actors? 5. What are the similarities and differences between the movements of a Crab. Critter and a Critter? 6. How does a Crab. Critter determine when it turns instead of moving? 7. Why don’t the Crab. Citters objects eat each other? Grid. World 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 new. Critter 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 Grid. World Case Study 8








