21
GridWorld Case Study

GridWorld Case Study

  • Upload
    corby

  • View
    79

  • Download
    0

Embed Size (px)

DESCRIPTION

GridWorld Case Study . Overview. Simulates actions and interactions of objects in a two-d grid Actors are displayed by a GUI Can add new actors to the grid and invoke methods on all actors Each actor ‘acts’ in a specified way. The Class Hierarchy. The Actors and their Actions. - PowerPoint PPT Presentation

Citation preview

Page 1: GridWorld  Case Study

GridWorld Case Study

Page 2: GridWorld  Case Study

Overview

• Simulates actions and interactions of objects in a two-d grid

• Actors are displayed by a GUI• Can add new actors to the grid and invoke

methods on all actors• Each actor ‘acts’ in a specified way

Page 3: GridWorld  Case Study

The Class Hierarchy

Page 4: GridWorld  Case Study

The Actors and their Actions• Rock – does nothing• Flower – darkens its color• Bug – moves forward if it can. It can move into an 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 can’t move if it is blocked in front by either another non-flower actor or the edge of the grid. When a bug is prevented from moving, it turns 45⁰ to the right.

• BoxBug – Moves like a bug. Additionally, if it encounters no obstacles, 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.

Page 5: GridWorld  Case Study

The Actors and their Actions continued

• Critter – gets a list of its adjacent neighboring actors and processes them by “eating” each actor is not a rock or another Critter. It then randomly selects one of the empty neighboring locations and moves there. If there are no available empty locations, a critter does not move.

• ChameleonCritter – gets a list of adjacent neighbors, randomly picks on of them, and changes its color to that of the selected actor. Moves like a Critter, but additionally, it first changes its direction to face its new location before moving.

Page 6: GridWorld  Case Study

Location Class

• Encapsulates row and column values for any position in the grid

• Provides constants for compass directions and turn angles

• Provides methods for determining relationships between locations and compass directions

Page 7: GridWorld  Case Study

Location ConstantsConstants that represent compass directions:Constant int Value

Location.NORTH 0

Location.EAST 90

Location.SOUTH 180

Location.WEST 270

Location.NORTHEAST 45

Location.SOUTHEAST 135

Location.SOUTHWEST 225

Location.NORTHWEST 315

Constants that represent commonly used turn anglesConstant int Value

Location.LEFT -90

Location.RIGHT 90

Location.HALF_LEFT -45

Location.HALF_RIGHT 45

Location.FULL_CIRCLE 360

Location.HALF_CIRCLE 180

Location.AHEAD 0

Page 8: GridWorld  Case Study

Location Methods• public Location(int r, int c) – constructs a Location with given row and column• Accessors

– public int getRow()– public int getColumn()

• public Location getAdjascentLocation(int direction) – returns the adjascent location in the compass direction closest to direction

• public int getDirectionToward(Location target) – returns the direction, rounded to the nearest compass direction, from this location toward a target location

• public int getHashCode() – generates and returns a hash code for this Location• Comparison methods

– public boolean equals(Object other)– public int compareTo(Object other) – by row first, then left to right

• public String toString() – form (row, col)

Page 9: GridWorld  Case Study

Actor Class

• Superclass for every creature• HAS-A location, direction, and color• Has the ability to put itself in the grid and

remove itself from the grid• Actor is a black-box class

Page 10: GridWorld  Case Study

Actor public methods• public Actor() – default constructor; blue actor facing north• Accessors

– public Color getColor()– public int getDirection()– public Location getLocation()– public Grid<Actor> getGrid() – returns null if actor is not in grid

• Mutators– public void setColor(Color newColor)– public void setDirection(int newDirection)

• public void moveTo(Location newLocation)– If new location is occupied, actor in that location is removed from grid– Preconditions:

• This Actor is in a grid• newLocation is valid in this Actor’s grid

Page 11: GridWorld  Case Study

Actor public methods continued

• public void putSelfInGrid(Grid<Actor> gr, Location loc) – Precondition: loc is valid

• public void removeSelfFromGrid()• public void act()– Reverses direction of this actor– This method is often overridden in subclasses

• public String toString()– Returns a String with the location, direction, and color of

this Actor

Page 12: GridWorld  Case Study

Rock Class

• Acts by doing nothing (act() is overridden with empty method body)

• Default constructor that creates a black rock• Second constructor that allows construction of

a Rock with a specified color

Page 13: GridWorld  Case Study

Flower Class

• Acts by darkening its color (act() is overridden; reduces values of the red, green, and blue components of its color by a constant factor)

• Default constructor that creates a pink flower• Second constructor that allows construction of

a flower with a specified color

Page 14: GridWorld  Case Study

Bug Class

• Moves forward in a straight line• Turns only if it is blocked• Can be blocked by the edge of the grid or a

non-Flower actor• Any flower in the bug’s path is removed from

the grid

Page 15: GridWorld  Case Study

Bug public methods• public Bug() – default constructor; constructs a red Bug• public Bug(Color bugColor) – constructs a Bug with specified

color• public void act() – turns bug 45⁰ to the right without changing

location (adds Location.HALF_RIGHT to bug’s current direction)• public boolean canMove()

– Returns true if:• Location directly in front of it is valid• Location directly in front of it is empty or contains a flower

• public void move() – moves bug forward, places a flower that is the color of the bug in its previous location

Page 16: GridWorld  Case Study

BoxBug Class

• IS-A Bug that moves in a square pattern if unimpeded

• Two private instance variables– sideLength – number of steps in a side of its

square– steps – keeps track of where the BoxBug is in

creating a side; gets reset to 0 when a side has been completed or if the bug must start again after encountering an obstacle

Page 17: GridWorld  Case Study

BoxBug public methods• public BoxBug(int length) – constructor; sets sideLength to length;

initializes steps to 0• public void act() – overridden method performs one step in the creation

of the BoxBug’s square• Steps for making the square:

– act() method tests whether the bug is still in the process of making a side – if(steps < sideLength)…

– If this is true, and the bug can move, the bug moves and steps is incremented– If this is false, the bug turns by calling turn() twice – then, steps is reset to 0– If steps < sideLength is true but canMove() is false, the same preparation for a

new side occurs (turn, turn, reset steps)– If BoxBug is unimpeded in making its square, and sideLength is k, there will be

k+1 flowers on each side of the square

Page 18: GridWorld  Case Study

Critter Class

• IS-A actor with the following behavior:– Get a list of neighboring actors– Process the actors– Get a list of possible locations to move to– Select a location from the list– Move there

• No explicit constructor, thus default Actor constructor used (blue Critter facing North)

Page 19: GridWorld  Case Study

Critter public methods• public void act() – gets a list of neighbors, processing them, gets a list of possible

locations to move to, selects one of these, moves to selected location• public ArrayList<Actor> getActors() – returns a list of adjacent actors• public void processActors(ArrayList<Actor> actors) – processes actors; Critter “eats” all

actors that are not rocks or other critters.• public ArrayList<Location> getMoveLocations() – returns a list of valid, adjacent, empty,

neighboring locations, which are the possible locations for the next move. Grid method getEmptyAdjacentLocations() is used, with the critter’s current location as its parameter

• public Location selectMoveLocation(ArrayList<Location> locs) – – Assign n to length of list– If n == 0, return current location of critter– Get random int from 0 to n – 1 (int r = (int)(Math.random() * n))– Return locs.get(r)

• public void makeMove(Location loc) – moves Critter to specified Location– Precondition: loc is valid– Implemented with moveTo() in Actor

Page 20: GridWorld  Case Study

ChameleonCritter Class

• IS-A Critter• Instead of eating neighbors, it randomly

selects one actor and changes its color to that of the selected actor

• Moves like a Critter with one difference, before it moves it turns to face its new location

Page 21: GridWorld  Case Study

ChameleonCritter public methods

• public void processActors(ArrayList<Actor> actors) – randomly selects adjacent neighbor, changes its color to that of selected actor. Does nothing if no neighbors exist

• public void makeMove(Location loc) – moves like Critter, but faces new location before it moves– setDirection() is used– getLocation().getDirectionToward(loc) used as

parameter for setDirection()