Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
CS&IT 2008 1
GridWorldAn Introduction
Frances P. Trees, Drew UniversityBarbara Ericson, Georgia Tech____________________________Donald Allen, Troy High SchoolAnn Shen, Bishop Strachan SchoolLaurie White, Mercer University
CS&IT 2008 2
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 3
AP Overview Framework derived from MBS (GNU license!)Fewer classes/interfaces: 4 (A) / 7 (AB) implementations, 5 API'sLayers: use framework at multiple points in your courseMuch easier to add your own classes
public classes
A AAPI API
API
API API
AB
AB AB
BoxBug ChameleonCritterA A
CS&IT 2008 4
OverviewLayers of Information
Layer 1: ObjectsLayer 2: Simple InheritanceLayer 3: Interacting ObjectsLayer 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 5
Layer 1: Objects Turtle-like graphics: Bugdrops flowers
Direct Manipulation (like in BlueJ)
Can be used as an early introduction to objects
CS&IT 2008 7
Layer 2: Simple InheritancePart 2: Bug Variations
Simple Inheritance
Demo:BoxBug (testable code)BoxBugRunner
CS&IT 2008 8
BoxBug
Students need to understandBug constructorsAct method
Students will need to usecanMoveturnmove
Students can create a new type of Bug by modifying BoxBug code
CS&IT 2008 9
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 10
New Bug Classes
CircleBugLike BoxBug but only one turn
45 degree turn
SpiralBugLike BoxBug but the sideLength gets longer after each turn
90 degree turn
CS&IT 2008 11
New Bug Classes
ZBugDraws a Z and then stops moving
DancingBugTakes an array of integers which are the number of turns it makes when asked to actThen it acts like a Bug
CS&IT 2008 12
Some easy, student-designed bugs…
MirrorBugThis bug, when turning, would turn left instead of right.
BoostBugThis 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 15
Part 3: GridWorld Classes and InterfacesA grid contains ActorsTwo types of grids
boundedunbounded
Locations in the grid are represented by objects of type LocationAn Actor knows what grid it lives in and its location within that grid.
CS&IT 2008 16
The Actor class
When adding or removing actors, do not use the put and remove methods of the Gridinterface.
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 putSelfInGridand removeSelfFromGrid methods of the Actorclass.
CS&IT 2008 17
The Actor class
The moveTo method allows the actor to move to any valid location. If the actor calls moveTofor 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 20
Layer 3: Interacting Objects(Part 4)
Introduces the Critter class (testable code)Uses template method, actIt 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 21
Critters
Get all neighborsat 8 surrounding locations
Eat neighbors that are not rocks and not critters
at 8 surrounding locationsCan move to any empty adjacent location
picked at randomMoves to the new location
doesn't turn
CS&IT 2008 22
ChameleonCritter (testable code)
ChameleonCritteroverrides 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 23
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-leftpossible move locations are immediately to the right and to the leftIf the crab critter doesn't move, it randomly turns left or right
CS&IT 2008 24
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 25
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 27
Overview of GridWorld Creatures
ActorsBugs
extending Bug: override ActRocksFlowersCritters
DO NOT OVERRIDE ACT!!!extending Critter overrides one or more of the 5 methods
CS&IT 2008 30
Layer 4: Data Structures (Part 5) AB onlyGrid Data StructuresTwo concrete implementations of the Grid interface are provided,
one for a bounded grid that has a fixed number of rows and columnsone 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 33
Mice in a MazeAdded a Wall, Mouse, and Smart Mouse
inherit from ActorAdded WallDisplay, MouseDisplay, and SmartMouseDisplay
inherit from AbstractDisplayThe mice are trying to get to the cheese
exit when find cheeseCan add stacks and queues for "smarter" mice
depth-first searchbreadth-first search
CS&IT 2008 34
School Time…………
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.
CS&IT 2008 38
Beyond GridWorldGreenfoot is a free Java framework www.greenfoot.orgYou can do GridWorldin GreenfootYou can also move beyond fitting everything in the grid
do games like Breakout
CS&IT 2008 39
Available GridWorld Materials
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 GridWorldInstallation Guide (.pdf/172KB)Appendixes (Quick Reference document) (145KB)Solutions Manual (.pdf/374KB)
GridWorld Sample Exam QuestionsGridWorld Sample Questions (.pdf/238KB) New!XBug Sample Code (.zip/3KB) New!Drop Game Sample Code (.zip/3KB) New!
http://apcentral.collegeboard.com/apc/public/courses/teachers_corner/151155.html
CS&IT 2008 40
What's tested?
AP Quick Reference Guide Appendix A includes a table with this informationhttp://apcentral.collegeboard.com/apc/members/repos
itory/ap07_gridworld_quickref_v2.pdf;jsessionid=G1QQPDMPd0pYfMyV9xYMJfSgQhTY475Zyp2GydkN33Y1F5zB54Ms!-28784328
CS&IT 2008 41
Helpful/Interesting URLs
http://www.apcomputerscience.com/gridworld/Dave Wittry's Board GamesReg Hahne's Save A Heart(apteacher,specialfocus)
http://www.apluscompsci.com/gridworld.htmTreeBoundedGrid, Othello, Sudoku, AntFarmRobert Glenn Martin's GridWorld materials (abbott)
http://www.cs.sbu.edu/dlevine/RolePlay/roleplay.html
Dave Levine's Role Play
CS&IT 2008 42
Helpful/Interesting URLshttp://horstmann.com/gridworld/
http://horstmann.com/gridworld/api-notes.htmlCay Horstmann's GridWorld site
http://csta.villanova.edu/http://www.dare.k12.nc.us/langner/gridworld/FreeResponseRewritten4Gridworld.doc
free response questions rewritten in GridWorldhttp://www.dare.k12.nc.us/langner/gridworld/GridWorldFRSolutions.doc - solutions
http://www.greenfoot.org/Place to download Greenfoot
http://csta.acm.org/index.htmlCSTA : Join and download and contribute resources here