Presentation is loading. Please wait.

Presentation is loading. Please wait.

Critters A Study in Design Patterns. Design Patterns  Not specific algorithms or data structures  A general reusable solution to a common problem.

Similar presentations


Presentation on theme: "Critters A Study in Design Patterns. Design Patterns  Not specific algorithms or data structures  A general reusable solution to a common problem."— Presentation transcript:

1 Critters A Study in Design Patterns

2 Design Patterns  Not specific algorithms or data structures  A general reusable solution to a common problem

3 Design Pattern for Cooking  Find recipe  Collect ingredients  Prepare ingredients  Combine ingredients  Apply heat  Present

4 Critters  Have a very specific design pattern  Critter questions test to see if you know it and respect it  Also use ArrayLists

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

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

7 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)

8 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 getActors() { return getGrid().getNeighbors(getLocation()); }

9 The Default Critter Processing the neighbors: eats the ones that are not Critters or Rocks public void processActors(ArrayList actors) { for (Actor a : actors) { if (!(a instanceof Rock) && !(a instanceof Critter)) a.removeSelfFromGrid(); }

10 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(); buggy instanceof BoxBug true buggy instanceof Actor true buggy instanceof Critter false

11 The Default Critter  Get a list of next locations: pick the locations around the critter public ArrayList getMoveLocations() { return getGrid().getEmptyAdjacentLocations(getLocation()); }

12 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 locs) { int n = locs.size(); if (n == 0) return getLocation(); int r = (int) (Math.random() * n); return locs.get(r); }

13 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); }

14 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

15 The Chameleon Which methods must be overridden to do this?  getActors?  processActors?  getMoveLocations?  selectMoveLocation?  makeMove?

16 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

17 The Chameleon  Code for the chameleon just has these two methods overridden.

18 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

19 The Crab Which methods must be overridden to do this?  getActors?  processActors?  getMoveLocations?  selectMoveLocation?  makeMove?

20 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

21 The Crab  Code for the crab just has these three methods overridden.

22 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

23 The Possum Which methods must be overridden to do this?  getActors?  processActors?  getMoveLocations?  selectMoveLocation?  makeMove?

24 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

25 The Possum  Since this was the first Critter free response question, students were told which methods to override.

26 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

27 The StockPile Which methods must be overridden to do this?  getActors?  processActors?  getMoveLocations?  selectMoveLocation?  makeMove?

28 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

29 The StockPileCritter  No framework was given (this is common with GridWorld questions now).  Violating postconditions was costly.

30 Variations on the theme: The AttractiveCritter AttractiveCritters behave by:  Moving every other Actor in the grid toward them

31 The AttractiveCritter Which methods must be overridden to do this?  getActors?  processActors?  getMoveLocations?  selectMoveLocation?  makeMove?

32 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

33 The AttractiveCritter  Another free response with just a blank page.


Download ppt "Critters A Study in Design Patterns. Design Patterns  Not specific algorithms or data structures  A general reusable solution to a common problem."

Similar presentations


Ads by Google