GridWorld An Introduction Frances P. Trees, Drew University Barbara Ericson, Georgia Tech ____________________________ Donald Allen, Troy High School Ann Shen, Bishop Strachan School Laurie White, Mercer University CS&IT 2008 1 Why Use Case Studies? Allow students the benefits of apprenticeship. Make large programs accessible to students. Particularly well suited for active learning and team work. Encourage students to reflect on solutions. The least gender-biased of all types of AP Free Response questions. CS&IT 2008 2 AP Overview Framework derived from MBS (GNU license!) Fewer classes/interfaces: 4 (A) / 7 (AB) implementations, 5 API's Layers: use framework at multiple points in your course Much easier to add your own classes API public classes API API API AB API A AB A AB ChameleonCritter BoxBug A A CS&IT 2008 3 Overview Layers of Information Layer 1: Objects Layer 2: Simple Inheritance Layer 3: Interacting Objects Layer 4: Data Structures Narrative (Student Manual) Part 1: Provides experiments to observe the attributes and behavior of the actors. Part 2: Defines Bug variations. Part 3: Explores the code that is needed to understand and create actors. Part 4: Defines classes that extend the Critter class. Part 5: (CS AB only) Explains grid data structures. CS&IT 2008 4 Layer 1: Objects Can be used as an early introduction to objects Turtle-like graphics: Bug drops flowers Direct Manipulation (like in BlueJ) CS&IT 2008 5 GridWorld Part 2 Bug Variations CS&IT 2008 6 Layer 2: Simple Inheritance Part 2: Bug Variations Simple Inheritance Demo: BoxBug (testable code) BoxBugRunner CS&IT 2008 7 BoxBug Students need to understand Bug constructors Act method Students will need to use canMove turn move Students can create a new type of Bug by modifying BoxBug code CS&IT 2008 8 Extending the Bug class Override Bug's act method Each call to the move method should be guarded by a call to the canMove method Add additional instance fields if needed Add new methods to the subclass if needed Constructors for the subclass call super() or super(someColor) CS&IT 2008 9 New Bug Classes CircleBug Like BoxBug but only one turn 45 degree turn SpiralBug Like BoxBug but the sideLength gets longer after each turn 90 degree turn CS&IT 2008 10 New Bug Classes ZBug Draws a Z and then stops moving DancingBug Takes an array of integers which are the number of turns it makes when asked to act Then it acts like a Bug CS&IT 2008 11 Some easy, student-designed bugs… MirrorBug This bug, when turning, would turn left instead of right. BoostBug This bug works like a power-up in a video game. If a bug moves to a location containing a flower, it eats that flower and it turns 2 times instead of once. CS&IT 2008 12 Cool bugs! Fruit Flies !!! CS&IT 2008 13 GridWorld Part 3 Explore GridWorld Classes CS&IT 2008 14 Part 3: GridWorld Classes and Interfaces A grid contains Actors Two types of grids bounded unbounded Locations in the grid are represented by objects of type Location An Actor knows what grid it lives in and its location within that grid. CS&IT 2008 15 The Actor class When adding or removing actors, do not use the put and remove methods of the Grid interface. Those methods do not update the location and grid instance variables of the actor. That is a problem since most actors behave incorrectly if they do not know their location. To ensure correct actor behavior, always use the putSelfInGrid and removeSelfFromGrid methods of the Actor class. CS&IT 2008 16 The Actor class The moveTo method allows the actor to move to any valid location. If the actor calls moveTo for a location that contains another actor, the other one removes itself from the grid and this actor moves into that location. The act method of the Actor class reverses the direction of the actor. You override this method in subclasses of Actor to define actors with different behavior. CS&IT 2008 17 Cool programs dealing with actors: CS&IT 2008 18 GridWorld Part 4 The Critter class and Extensions CS&IT 2008 19 Layer 3: Interacting Objects (Part 4) Introduces the Critter class (testable code) Uses template method, act It is usually not a good idea to override the act method in a Critter subclass. The Critter class was designed to represent actors that process other actors and then move. If you find the act method unsuitable for your actors, you should consider extending Actor, not Critter. The act method calls five methods accomplishing the following tasks: find neighbors (getActors) process neighbors (processActors) find candidates for move locations (getMoveLocations) select a move location (selectMoveLocation) make a move (makeMove) CS&IT 2008 20 Critters Get all neighbors Eat neighbors that are not rocks and not critters at 8 surrounding locations Can move to any empty adjacent location at 8 surrounding locations picked at random Moves to the new location doesn't turn CS&IT 2008 21 ChameleonCritter (testable code) ChameleonCritter overrides processActors and makeMove. Randomly selects a neighbor and changes this critter's color to be the same as that neighbor's. If there are no neighbors, no action is taken. Turns towards the new location as it moves CS&IT 2008 22 CrabCritter CrabCritter overrides getActors, getMoveLocations, and makeMove. A crab gets the actors in the three locations immediately in front, to its front-right and to its front-left possible move locations are immediately to the right and to the left If the crab critter doesn't move, it randomly turns left or right CS&IT 2008 23 Creative Critters… A ChameleonKid changes its color to the color of one of the actors immediately in front or behind. If there is no actor in either of these locations, then the ChameleonKid darkens like the modified ChameleonCritter. A RockHound gets the actors to be processed in the same way as a Critter. It removes any rocks in that list from the grid. A RockHound moves like a Critter. CS&IT 2008 24 Creative Critters… Create a class KingCrab that extends CrabCritter. A KingCrab gets the actors to be processed in the same way a CrabCritter does. A KingCrab causes each actor that it processes to move one location further away from the KingCrab. If the actor cannot move away, the KingCrab removes it from the grid. When the KingCrab has completed processing the actors, it moves like a CrabCritter. CS&IT 2008 25 Fun Critters CS&IT 2008 FruitCritters 26 Overview of GridWorld Creatures Actors Bugs extending Bug: override Act Rocks Flowers Critters DO NOT OVERRIDE ACT!!! extending Critter overrides one or more of the 5 methods CS&IT 2008 27 Don't like bugs and critters???? CS&IT 2008 28 GridWorld Layer 5 Data Structures – AB only CS&IT 2008 29 Layer 4: Data Structures (Part 5) AB only Grid Data Structures Two concrete implementations of the Grid interface are provided, one for a bounded grid that has a fixed number of rows and columns one for an unbounded grid, for which any row and column values are valid. Rather than have repeated code in two classes, the AbstractGrid class defines five methods of the Grid interface that are common to both implementations. CS&IT 2008 30 Grid Hierarchy CS&IT 2008 31 Extentions… CS&IT 2008 32 Mice in a Maze Added a Wall, Mouse, and Smart Mouse Added WallDisplay, MouseDisplay, and SmartMouseDisplay inherit from AbstractDisplay The mice are trying to get to the cheese inherit from Actor exit when find cheese Can add stacks and queues for "smarter" mice depth-first search breadth-first search CS&IT 2008 33 School Time………… CS&IT 2008 School busses must be able to collect students form the bus stops, bring them to schools, and return them to the correct bus stop at the end of the day. 34 An Extension - GameOfLife CS&IT 2008 35 An Extension - Sudoku CS&IT 2008 36 Extensions – Tile Games http://www.apcomputerscience.com/gridworld/ CS&IT 2008 37 Beyond GridWorld Greenfoot is a free Java framework www.greenfoot.org You can do GridWorld in Greenfoot You can also move beyond fitting everything in the grid do games like Breakout CS&IT 2008 38 Available GridWorld Materials http://apcentral.collegeboard.com/apc/public/courses/teachers_corner/151155.html Code (.zip/238KB) Student Manual (Narrative and Appendixes as a Single Document) Student Manual (.pdf/633KB) Narrative as individual files: Part 1: Observing and Experimenting with GridWorld (.pdf/211KB) Part 2: Bug Variations (.pdf/285KB) Part 3: GridWorld Classes and Interfaces (.pdf/156KB) Part 4: Interacting Objects (.pdf/224KB) Part 5: Grid Data Structures (CS AB only) (.pdf/150KB) Support material for GridWorld Installation Guide (.pdf/172KB) Appendixes (Quick Reference document) (145KB) Solutions Manual (.pdf/374KB) GridWorld Sample Exam Questions GridWorld Sample Questions (.pdf/238KB) New! XBug Sample Code (.zip/3KB) New! Drop Game Sample Code (.zip/3KB) New! CS&IT 2008 39 What's tested? AP Quick Reference Guide Appendix A includes a table with this information http://apcentral.collegeboard.com/apc/members/repos itory/ap07_gridworld_quickref_v2.pdf;jsessionid=G1 QQPDMPd0pYfMyV9xYMJfSgQhTY475Zyp2Gydk N33Y1F5zB54Ms!-28784328 CS&IT 2008 40 Helpful/Interesting URLs http://www.apcomputerscience.com/gridworld/ http://www.apluscompsci.com/gridworld.htm Dave Wittry's Board Games Reg Hahne's Save A Heart (apteacher,specialfocus) TreeBoundedGrid, Othello, Sudoku, AntFarm Robert Glenn Martin's GridWorld materials (abbott) http://www.cs.sbu.edu/dlevine/RolePlay/rolepla y.html Dave Levine's Role Play CS&IT 2008 41 Helpful/Interesting URLs http://horstmann.com/gridworld/ http://csta.villanova.edu/ http://www.dare.k12.nc.us/langner/gridworld/FreeRespon seRewritten4Gridworld.doc free response questions rewritten in GridWorld http://www.dare.k12.nc.us/langner/gridworld/GridWorldFRSolutio ns.doc - solutions http://www.greenfoot.org/ http://horstmann.com/gridworld/api-notes.html Cay Horstmann's GridWorld site Place to download Greenfoot http://csta.acm.org/index.html CSTA : Join and download and contribute resources here CS&IT 2008 42