1 / 21

GridWorld Case Study

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.

corby
Download Presentation

GridWorld Case Study

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GridWorld Case Study

  2. 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

  3. The Class Hierarchy

  4. 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.

  5. 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.

  6. 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

  7. Location Constants Constants that represent compass directions: Constants that represent commonly used turn angles

  8. Location Methods • public Location(int r, int c) – constructs a Location with given row and column • Accessors • public intgetRow() • public intgetColumn() • public Location getAdjascentLocation(int direction) – returns the adjascent location in the compass direction closest to direction • public intgetDirectionToward(Location target) – returns the direction, rounded to the nearest compass direction, from this location toward a target location • public intgetHashCode() – generates and returns a hash code for this Location • Comparison methods • public boolean equals(Object other) • public intcompareTo(Object other) – by row first, then left to right • public String toString() – form (row, col)

  9. 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

  10. Actor public methods • public Actor() – default constructor; blue actor facing north • Accessors • public Color getColor() • public intgetDirection() • public Location getLocation() • public Grid<Actor> getGrid() – returns null if actor is not in grid • Mutators • public void setColor(Color newColor) • public void setDirection(intnewDirection) • 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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_RIGHTto bug’s current direction) • public booleancanMove() • 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

  16. 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

  17. 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

  18. 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)

  19. 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

  20. 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

  21. 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()

More Related