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

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

Why not just use Arrays? Java ArrayLists.
GridWorld Case Study The Classes A Summary by Jim Mims.
GridWorld Case Study Part 3 GridWorld Classes and Interfaces A Summary by Jim Mims.
GridWorld Case Study Part 2 Bug Variations A Summary by Jim Mims.
GridWorld Case Study Part 4 Classes that Extend the Critter Interface A Summary by Jim Mims.
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
C9, a case study: Solitaire
© A+ Computer Science - GridWorld © A+ Computer Science -
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Java Review Interface, Casting, Generics, Iterator.
Getting to know Greenfoot
Chapter 2 - The First Program: Little Crab
Program: Little Crab Mr Gano.
Using a different image than normal Multiple images for a class Controlling the mouse click Controlling the keys pressed Launching an input dialog box.
And now for something completely different: recursion.
Stacks, Queues, and Deques
Chapter 13 – Aggregation, Composition, and Inheritance
COMP 103 Iterators and Iterable. RECAP  Maps and Queues TODAY  Queue Methods  Iterator and Iterable 2 RECAP-TODAY.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-x: Critters reading: HW9 Spec.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
GridWorld Case Study1 Barbara Ericson Georgia Tech Jan 2008.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
© A+ Computer Science - Row = 0 Column = 0.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Lecture 4. Greenfoot 1 Pablo Romero, Department of Informatics Greenfoot Programming simulations and games in Java.
GridWorld Case Study Barbara Ericson March 24, 2007.
More on Polymorphism. Ever have one of those days?
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© A+ Computer Science - Grid is an interface that details the behaviors expected of a grid. All its methods are abstract methods.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
The GridWorld Case Study Program Section 1 - Overview of the Class Hierarchies Section 2 - The Actor Class Section 3 - The Rock and Flower Classes Section.
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
Sit-In Lab 2 - OOP Restaurant.  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
GridWorld Case Study The case study is a program that simulates actions and interactions of objects in a two- dimensional grid. During a single step of.
Polymorphism. ArrayList boxbugs; ArrayList critters; ArrayList kingCrabs; ArrayList workerants; boxbugs.get(i).act(); critters.get(i).act(); kingCrabs.get(i).act();
Improving the Crab Mrs. C. Furman August 19, 2010.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Kakihijau.googlepages.com Introduction To Greenfoot Part-3.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
GridWorld.
Exposure Java 2012 APCS Edition Chapter 13 Slides
GridWorld Part 4 Meet the Critters.
Agenda About Quiz ChameleonCritter class CrabCritter class homework.
Barbara Ericson Georgia Tech Jan 2008
AP "Case Studies" A big program for AP students to understand
The Little Crab Scenario
null, true, and false are also reserved.
© A+ Computer Science - GridWorld © A+ Computer Science -
Building Java Programs
GridWorld Part 4 Meet the Critters.
© A+ Computer Science - GridWorld The GridWorld case study provides a graphical environment where visual objects inhabit and interact.
© A+ Computer Science - GridWorld © A+ Computer Science -
GridWorld Case Study.
© A+ Computer Science - GridWorld © A+ Computer Science -
Objects with ArrayLists as Attributes
© A+ Computer Science - GridWorld © A+ Computer Science -
Lec 21 More Fun with Arrays: For Loops
Presentation transcript:

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

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

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

The Default Critter  Get a list of next locations: pick the locations around the critter public ArrayList 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 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.