330 likes | 494 Views
Critters. A Study in Design Patterns. Design Patterns. Not specific algorithms or data structures A general reusable solution to a common problem. Design Pattern for Cooking. Find recipe Collect ingredients Prepare ingredients Combine ingredients Apply heat Present. Critters.
E N D
Critters A Study in Design Patterns
Design Patterns • Not specific algorithms or data structures • A general reusable solution to a common problem
Design Pattern for Cooking • Find recipe • Collect ingredients • Prepare ingredients • Combine ingredients • Apply heat • Present
Critters • Have a very specific design pattern • Critter questions test to see if you know it and respect it • Also use ArrayLists
Does it matter? 60% of GridWorld free response questions have been based on Critters • 2008: OpossumCritter • 2009: StockpileCritter • 2010: GridChecker • 2011: AttractiveCritter • 2012: RetroBug And GridWorld is 25% of free response.
The Critter Pattern Critters are Actors that do 5 steps: • Get a list of all neighbors • Process their neighbors • Get a list of possible locations to move to • Pick a location to move to • Move to that location In this order, always.
Big Things to Remember • Don’t override act • Critters can only change their state in processActors or makeMove • Critters can only change the state of others in processActors • Pay attention to postconditions (in the quick reference)
Specific Critters: Default Critter If you just put a Critter in a World, what does it do? • getActors: collects the neighbors, the (up to 8) Actors in surrounding cells public ArrayList<Actor> getActors() { return getGrid().getNeighbors(getLocation()); }
The Default Critter Processing the neighbors: eats the ones that are not Critters or Rocks public void processActors(ArrayList<Actor> actors) { for (Actor a : actors) { if (!(a instanceof Rock) && !(a instanceof Critter)) a.removeSelfFromGrid(); } }
An aside: instanceof? • A Java boolean function item instanceof type • Is the item a type? With inheritance, some items can be many things: BoxBug buggy = new BoxBug();
The Default Critter • Get a list of next locations: pick the locations around the critter public ArrayList<Location> getMoveLocations() { return getGrid().getEmptyAdjacentLocations(getLocation()); }
The Default Critter • Pick a next location from the list at random, if there is one. Otherwise, use your current location. public Location selectMoveLocation(ArrayList<Location> locs) { int n = locs.size(); if (n == 0) return getLocation(); int r = (int) (Math.random() * n); return locs.get(r); }
The Default Critter • Move to the selected location (and don’t do anything else, like leave a flower) or die if there is no location. public void makeMove(Location loc) { if (loc == null) removeSelfFromGrid(); else moveTo(loc); }
Variations on the theme: The ChameleonCritter Chameleons behave by: • Not eating neighbors • Changing color to match a neighbor if there is one • Facing in the direction they move
The Chameleon Which methods must be overridden to do this? • getActors? • processActors? • getMoveLocations? • selectMoveLocation? • makeMove?
The Chameleon Which methods must be overridden to do this? • getActors? No • processActors? Yes: change the color instead of eat • getMoveLocations? No • selectMoveLocation? No (see postconditions) • makeMove? Yes: change direction
The Chameleon • Code for the chameleon just has these two methods overridden.
Variations on the theme: The CrabCritter Crabs behave by: • Only eating neighbors in front of them • Just moving left or right • If they cannot move left or right, turning 90 degrees, left or right
The Crab Which methods must be overridden to do this? • getActors? • processActors? • getMoveLocations? • selectMoveLocation? • makeMove?
The Crab Which methods must be overridden to do this? • getActors? Yes: just the ones in front • processActors? No • getMoveLocations? Yes: just the right and left • selectMoveLocation? No • makeMove? Yes: turn if there is no move
The Crab • Code for the crab just has these three methods overridden.
Variations on the theme: The OpossumCritter Opossums behave by: • Playing dead if there are more foes than friends around it • Changing color to match a neighbor if there is one • Facing in the direction they move
The Possum Which methods must be overridden to do this? • getActors? • processActors? • getMoveLocations? • selectMoveLocation? • makeMove?
The Possum Which methods must be overridden to do this? • getActors? No • processActors? Yes: count friends and foe and change color • getMoveLocations? Yes • selectMoveLocation? No • makeMove? No
The Possum • Since this was the first Critter free response question, students were told which methods to override.
Variations on the theme: The StockPileCritter StockPile Critters behave by: • Using neighbors as energy • Removing itself from the Grid if it runs out of energy
The StockPile Which methods must be overridden to do this? • getActors? • processActors? • getMoveLocations? • selectMoveLocation? • makeMove?
The StockPile Which methods must be overridden to do this? • getActors? No • processActors? Yes: add the count to the total energy • getMoveLocations? No • selectMoveLocation? No • makeMove? Yes: die if no energy
The StockPileCritter • No framework was given (this is common with GridWorld questions now). • Violating postconditions was costly.
Variations on the theme: The AttractiveCritter AttractiveCritters behave by: • Moving every other Actor in the grid toward them
The AttractiveCritter Which methods must be overridden to do this? • getActors? • processActors? • getMoveLocations? • selectMoveLocation? • makeMove?
The AttractiveCritter Which methods must be overridden to do this? • getActors? Yes: evewry Actor in the Grid • processActors? Yes: move them • getMoveLocations? No • selectMoveLocation? No • makeMove? No
The AttractiveCritter • Another free response with just a blank page.