Download presentation
Presentation is loading. Please wait.
Published byDella Lucas Modified over 9 years ago
1
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 the program, every occupant of the grid gets a chance to act. Each actor acts according to a clearly specified set of behaviors that can include moving, changing color, changing direction, or removing other actors from the grid. AP CS 2009 1
2
The Classes of GridWorld AP CS 2009 2 ACTOR FLOWERROCK CHAMELEON CRITTER CRITTER BOXBUG BUG
3
The Classes of GridWorld AP CS 2009 3 ACTOR GRID INTERFACE ABSTRACT GRID LOCATION UNBOUNDED GRID BOUNDED GRID
4
The Actors of GridWorld A rock does nothing A flower darkens its color A bug moves forward when it can. It can move into any empty spot or onto a flower. When it moves, it deposits a flower in its previous location. If it moves to a location occupied by a flower, that flower is removed from the grid. A bug cannot move if it is blocked in front by either another (non-flower) actor or the edge of the grid. When it is prevented from moving, it turns 45 degrees to the right. AP CS 2009 4
5
The Actors (continued) A BoxBug moves like a bug. Additionally, if it encounters no obstacles in its path, it traces out a square of flowers with a given side length. If a BoxBug is blocked from moving, it makes two right turns and starts again. A Critter gets a list of its adjacent neighboring actors and processes them by “eating” each actor that is not a rock or another critter. It then randomly selects one of the empty neighboring locations and moves there. If there are not available empty locations, a critter does not move. AP CS 2009 5
6
The Actors (continued) A ChameleonCritter gets a list of its adjacent neighbors, randomly picks one of them, and changes its color to that of the selected actor. The ChameleonCritter moves like a Critter but, additionally, it first changes its direction to face its new location before moving. AP CS 2009 6
7
The Location Class Row and Column values representing positions on the grid. Provides constants for compass directions and turn angles. Provides methods for determining relationships between locations and compass directions. AP CS 2009 7
8
The Location Class Constantint Value Location.NORTH0 Location.EAST90 Location.SOUTH180 Location.WEST270 Location.NORTHEAST45 Location.SOUTHEAST135 Location.SOUTHWEST225 Location.NORTHWEST315 AP CS 2009 8
9
The Location Class Constantint Value Location.LEFT-90 Location.RIGHT 90 Location.HALF_LEFT-45 Location.HALF_RIGHT 45 Location.FULL_CIRCLE360 Location.HALF_CIRCLE180 Location.AHEAD0 AP CS 2009 9
10
The Location Class To get an actor to turn setDirection(getDirection() + Location.RIGHT); Methods of the Location class getRow, getCol, getAdjacentLocation, getDirectionToward, equals, compareTo, toString AP CS 2009 10
11
The Actor Class The Actor class is a superclass for every creature that appears in the grid. An actor has a location, direction and color. It can also change each of these instance variables using setColor, and setDirection. It has access to it’s grid. It can putSelfInGrid and removeSelfFromGrid, thus changing its location. It also has methods called moveTo, act, toString, getColor, getDirection and setGrid. AP CS 2009 11
12
The Rock and Flower Classes A rock acts by doing nothing. It has a default constructor that creates a black rock and a second constructor that allows construction of a rock with a specified color. The act method is overridden – it has an empty body. A flower acts by darkening its color. It has a default constructor that creates a pink flower, and a second constructor that allows a construction of a flower with a specified color. The overridden act method darkens the flower’s color. AP CS 2009 12
13
The Bug Class A bug is an actor that moves forward in a straight line, turning only when it is blocked. A bug can be blocked by either the edge of the grid, or an actor that is not a flower. As a bug moves, it steps on any flower in its path, causing the removal of that flower from the grid. After each step, the bug places a flower in its previous location. AP CS 2009 13
14
The Bug Class The bug class contains the following methods: act turn move canMove The act method is overridden. AP CS 2009 14
15
AP CS 200915 GridWorld An Introduction Part 1
16
AP CS 2009 16 Overview Case Study contains 5 Parts: 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.
17
AP CS 2009 17 Part 1: Observing and Experimenting with GridWorld The first look at GridWorld Exploring Actor state and behavior Exploring Bug state and behavior Exploring Flower state and behavior Exploring Rock state and behavior Demo: BugRunner EXPLORATION time
18
AP CS 2009 18 BugRunner (code) public static void main(String[] args) { ActorWorld world = new ActorWorld(); world.add(new Bug()); world.add(new Rock()); world.show(); }
19
AP CS 2009 19 BugRunner Exploration You can add a new Bug, Rock, Flower, or Actor An Actor can be added to the Grid if any Actor subclass has been added in the BugRunner class. Remember the IS-A relationship of Inheritance; a Bug IS-A Actor a Rock IS-A Actor a Flower IS-A Actor
20
AP CS 200920 GridWorld Part 2
21
AP CS 2009 21 Part 2: Bug Variations Simple Inheritance Demo: BoxBug (testable code) BoxBugRunner
22
AP CS 2009 22 BoxBugRunner public static void main(String[] args) { ActorWorld world = new ActorWorld(); BoxBug alice = new BoxBug(6); alice.setColor(Color.ORANGE); BoxBug bob = new BoxBug(3); world.add(new Location(7, 8), alice); world.add(new Location(5, 5), bob); world.show(); }
23
AP CS 2009 23 BoxBug Exploration Run the program What kind of objects can you add to the grid? Create a world with a Bug and a BoxBug Right click on a Bug. Right click on a BoxBug. Do you see any differences?
24
AP CS 2009 24 BoxBug You will need to understand Bug constructors Act method You will need to use canMove turn move You will create a new type of Bug by modifying BoxBug code
25
AP CS 2009 25 BoxBug Code public class BoxBug extends Bug { private int steps; private int sideLength; public BoxBug(int length) { super(); steps = 0; sideLength = length; } public void act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); steps = 0; }
26
AP CS 2009 26 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)
27
AP CS 2009 27 Extending the Bug class CircleBugSpiralBug (unbounded grid) UnboundedGrid grid = new UnboundedGrid (); ActorWorld world = new ActorWorld(grid);
28
AP CS 2009 28 BoundedGrid vs. UnboundedGrid ActorWorld world = new ActorWorld(); world.add(new Bug()); world.add(new Rock()); UnboundedGrid grid = new UnboundedGrid (); ActorWorld world = new ActorWorld(grid); SpiralBug sp = new SpiralBug(3); world.add(new Location(11,9),sp);
29
AP CS 2009 29 Extending the Bug class ZBugDancingBug (array)
30
AP CS 200930 GridWorld Part 3
31
AP CS 2009 31 Part 3: GridWorld Classes and Interfaces A grid contains Actor s 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.
32
AP CS 2009 32 Location implements Comparable interface Built in compass directions public static final int SOUTH = 180; public static final int WEST = 270; public static final int NORTHEAST = 45; Built in turn directions public static final int RIGHT = 90; public static final int HALF_LEFT = -45; public static final int HALF_RIGHT = 45;
33
AP CS 2009 33 Location accessors getRow() getCol()
34
AP CS 2009 34 Location public Location getAdjacentLocation(int direction) returns the adjacent location in the compass direction that is closest to direction public int getDirectionToward(Location target) returns the closest compass direction from this location toward target
35
AP CS 2009 35 Location Location loc1 = new Location(4, 3); Location loc2 = new Location(3, 4); Location loc3 = loc2.getAdjacentLocation(Location.SOUTH); int dir = loc1.getDirectionToward(new Location(6, 5));
36
AP CS 2009 36 Grid interface The interface Grid specifies the methods for any grid that contains objects of the type E. Two classes, BoundedGrid and UnboundedGrid implement the interface.
37
AP CS 2009 37 Grid interface boolean isValid(Location loc) returns true if loc is valid in this grid, false otherwise Precondition: loc is not null E put(Location loc, E obj) puts obj at location loc in this grid and returns the object previously at that location (or null if the location was previously unoccupied) Precondition: (1) loc is valid in this grid (2) obj is not null E remove(Location loc) removes the object at location loc and returns it (or null if the location is unoccupied) Precondition: loc is valid in this grid E get(Location loc) returns the object at location loc (or null if the location is unoccupied) Precondition: loc is valid in this grid
38
AP CS 2009 38 Grid interface ArrayList getOccupiedLocations() returns all occupied locations in a grid. ArrayList getValidAdjacentLocations(Location loc) returns all valid locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList getEmptyAdjacentLocations(Location loc) returns all valid empty locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList getOccupiedAdjacentLocations(Location loc) returns all valid occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid ArrayList getNeighbors(Location loc) returns all objects in the occupied locations adjacent to loc in this grid Precondition: loc is valid in this grid
39
AP CS 2009 39 Grid interface How can you obtain a count of the objects in a grid? How can you obtain a count of the empty locations in a bounded grid?
40
AP CS 2009 40 The Actor class Accessor methods public Color getColor() public int getDirection() public Grid getGrid() public Location getLocation() Other Actor Methods public void putSelfInGrid(Grid gr, Location loc) establishes the actor’s location as well as the grid in which it is placed public void removeSelfFromGrid() removes the actor from its grid and makes the actor’s grid and location both null.
41
AP CS 2009 41 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.
42
AP CS 2009 42 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.
43
AP CS 2009 43 The Actor class Can an actor put itself into a grid twice without first removing itself? Can an actor remove itself from a grid twice? Can an actor be placed into a grid, remove itself, and then put itself back? Try it out. What happens?
44
AP CS 2009 44 Rock, Flower, Bug Time to look at code! Rock behavior? Flower behavior? Bug behavior?
45
AP CS 2009 45 Group Activity: Jumper Each group creates a class called Jumper. This actor can move forward two cells in each move. It “jumps” over rocks and flowers. It does not leave anything behind it when it jumps.
46
AP CS 2009 46 Group Activity: Jumper 1. What will a Jumper do if the location in front of it is empty, but the location two cells in front contains a flower or a rock? 2. What will a Jumper do if the location two cells in front of the Jumper is out of the grid? 3. What will a Jumper do if it is facing an edge of the grid? 4. What will a Jumper do if another actor (not a flower or a rock) is in the cell that is two cells in front of the jumper? 5. What will a Jumper do if it encounters another Jumper in its path? 6. Are there any other tests the Jumper needs to make?
47
AP CS 2009 47 Group Activity: Jumper 1. Which class should Jumper extend? 2. Is there an existing class that is similar to the Jumper class? 3. Should there be a constructor? If yes, what parameters should be specified for the constructor? 4. Which methods should be overridden? 5. What methods, if any, should be added? 6. What is the plan for testing the class?
48
AP CS 200948 GridWorld Part 4
49
AP CS 2009 49 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 )
50
AP CS 2009 50 Critters demo: Critter ChameleonCritter (testable code) CrabCritter (extra demo code)
51
AP CS 2009 51 act method public void act() { if (getGrid() == null) return; ArrayList actors = getActors(); processActors(actors); ArrayList moveLocs = getMoveLocations(); Location loc = selectMoveLocation(moveLocs); makeMove(loc); }
52
AP CS 2009 52 getActors method public ArrayList getActors() Gets the actors for processing. Implemented to return the actors that occupy the neighboring grid locations. Override this method for subclasses that look elsewhere for actors to process. Returns a list of actors that are neighbors of this critter. Postcondition: The state of all actors is unchanged.
53
AP CS 2009 53 processActors method public void processActors(ArrayList actors) Processes the actors. This method is implemented to "eat" (i.e. remove) all actors that are not rocks or critters. Override it in subclasses that process neighbors in a different way. Postconditions: (1) The state of all actors in the grid other than this critter and the elements of actors is unchanged. (2) The location of this critter is unchanged.
54
AP CS 2009 54 getMoveLocations method public ArrayList getMoveLocations() Get the possible locations for the next move. Implemented to return the empty neighboring locations. Override this method for subclasses that look elsewhere for move locations. Returns a list of possible locations for the next move. Postcondition: The state of all actors is unchanged.
55
AP CS 2009 55 selectMoveLocation method public Location selectMoveLocation(ArrayList locs) Selects the location for the next move. Implemented to randomly pick one of the possible locations, or to return the current location if locs has size 0. Override this method for subclasses that have another mechanism for selecting the next move location. Postconditions: (1) The returned location is an element of locs, this critter's current location, or null. (2) The state of all actors is unchanged.
56
AP CS 2009 56 makeMove method public void makeMove(Location loc) Moves this critter to the given location loc, or removes this critter from its grid if loc is null. An actor may be added to the old location. If there is a different actor at location loc, that actor is removed from the grid. Override this method in subclasses that want to carry out other actions (for example, turning this critter or adding an occupant in its previous location). Postconditions: (1) getLocation() == loc. (2) The state of all actors other than those at the old and new locations is unchanged.
57
AP CS 2009 57 Override one or more of these methods when creating a new Critter subclass. The Narrative states: 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.
58
AP CS 2009 58 Design issues The postconditions help to ensure that subclasses implement behavior that is consistent with the purpose of each piece.
59
AP CS 2009 59 ChameleonCritter The ChameleonCritter class defines a new type of critter that gets the same neighboring actors as a Critter. However, unlike a Critter, a ChameleonCritter doesn’t eat them. Instead, a ChameleonCritter randomly selects one and changes its own color to the color of the selected actor. When a ChameleonCritter moves, it turns toward the new location.
60
AP CS 2009 60 ChameleonCritter (testable code) ChameleonCritter overrides which methods???? 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 getActors processActors getMoveLocations selectMoveLocation makeMove
61
AP CS 2009 61 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
62
AP CS 2009 62 CrabCritter A CrabCritter is a critter that eats whatever is found in the locations immediately in front, to the right-front, or to the left-front of it. It will not eat a rock or another critter. A CrabCritter can move only to the right or to the left. If both locations are empty, it randomly selects one. If a CrabCritter cannot move, then it turns 90 degrees, randomly to the left or right.
63
AP CS 2009 63 CrabCritter 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 Which methods does the CrabCritter override? getActors processActors getMoveLocations selectMoveLocation makeMove
64
AP CS 2009 64 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
65
AP CS 2009 65 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.
66
AP CS 2009 66 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.
67
AP CS 2009 67 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.