1 / 92

GridWorld

GridWorld. Case Studies. Case studies are a teaching tool used in many disciplines. They have been a part of the AP Computer Science curriculum since the 1995-96 academic year. Case studies give students an opportunity to: Read source code written by someone else.

ghada
Download Presentation

GridWorld

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

  2. Case Studies • Case studies are a teaching tool used in many disciplines. They have been a part of the AP Computer Science curriculum since the 1995-96 academic year. Case studies give students an opportunity to: • Read source code written by someone else. • Work with a program of significant length. • Become familiar with good coding, design, and documentation practice. • Learn about testing in a non-trivial context. • Think through design and implementation tradeoffs.

  3. Overview • The GridWorld case study provides a graphical environment where visual objects inhabit and interact in a two-dimensional grid. • In this case study, you will • design and create “actor” objects, • add them to the grid, and • determine whether the actors behave according to their specifications.

  4. Overview • A graphical user interface (GUI) is provided that displays the grid and the actors. • In addition, the GUI has a facility for adding actors to the grid and for invoking methods on them.

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

  6. Chapter 1: Observing and Experimenting with GridWorld

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

  8. Demo: Compile and run BugRunner.java. GridWorld GUI shows a grid containing two actors, a “bug” and a “rock”. Clicking on the Step button runs one step, making each actor act once. Clicking on the Run button carries out a series of steps until the Stop button is clicked. The delay between steps during a run can be adjusted with the slider.

  9. Demo Clicking on an empty cell in the grid displays a drop-down menu that shows the constructors for different actor types. The menu lists constructors for the classes of all objects that have ever been placed in the grid Selecting one of these constructors places an instance of that type in the grid. If the constructor has parameters, a dialog window appears requesting parameter values

  10. Exercise Set 1 • Does the bug always move to a new location? • In what direction does the bug move? • What does the bug do if it can’t move? • What does the bug leave behind when it moves? • What happens when the bug is at the edge of the grid? • And facing the edge? • And not facting the edge? • What happens when a bug is facing a rock and you click step? • Does a flower move? • What behavior does a flower have? • Does a rock move or have any other behavior? • Can more than one actor (bug, flower, rock) be in the same grid location at the same time?

  11. Exercise Answers Set 1 • Does the bug always move to a new location? • No • In what direction does the bug move? • The direction it is facing • What does the bug do if it can’t move? • Turns right 45 degrees • What does the bug leave behind when it moves? • A flower • What happens when the bug is at the edge of the grid? • And facing the edge? • Turns 45 • And not facing the edge? • Continues to move if it can • What happens when a bug is facing a rock and you click step? • It turns 45 degrees to the right. • Does a flower move? • No • What behavior does a flower have? • The color gets darker over time. It can act. It inherits other behavior from Actor (setColor, setDirection, etc) • Does a rock move or have any other behavior? • It doesn’t change over time. It can act. It inherits other behavior from Actor (setColor, setDirection, etc) • Can more than one actor (bug, flower, rock) be in the same grid location at the same time? • No

  12. Exercises page 8 • 1. Click on a bug, flower, or rock • Invoke the setDirection method • Fill in the following table for the setDirection method

  13. Exercise Answers • Click on a bug, flower, or rock • Invoke the setDirection method • Fill in the following table for the setDirection method • Can you set the direction to an angle that isn’t a multiple of 45? • What happens?

  14. Exercise • 2. Use the moveTo method to move a bug • Does it change direction? • How far can you move it? • What happens if you try to move it outside the grid? • What is the top left location? • What is the bottom right location?

  15. Exercise Answers • Use the moveTo method to move a bug • Does it change direction? • No • How far can you move it? • Anywhere in the grid • What happens if you try to move it outside the grid? • You get an IllegalArgumentException • What is the top left location? • 0,0 • What is the bottom right location? • 9,9

  16. Exercise • 3. What method can you use to change the color of a bug, rock, or flower? • 4. Move a rock to a location with a bug in it. Move the rock again to another location. What happened to the bug? • What methods are defined in bug • Not just inherited from Actor?

  17. Exercise Answers • What method can you use to change the color of a bug, rock, or flower? • setColor • Move a rock to a location with a bug in it. Move the rock again to another location. What happened to the bug? • It is removed from the world • What methods are defined in bug • Not just inherited from Actor • act, move, turn, canMove

  18. BugRunner publicstaticvoid main(String[] args) { ActorWorld world = new ActorWorld(); world.add(new Bug()); world.add(new Rock()); world.show(); } not tested on AP exam

  19. BugRunner • 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. • Good time to introduce inheritance; • a Bug IS-A Actor • a Rock IS-A Actor • a Flower IS-A Actor

  20. BugRunner • Right click on a bug. • Run a method that will move the bug off the grid. • Is there more than one way to do this? • After all bugs are removed from the grid, can you add a new bug to the grid? • Add a few bugs. • What conditions cause the bug's canMove method to return false?

  21. BugRunner • What can a flower do? • What situations cause a bug to turn? • What happens if you move a bug to a cell that is occupied by • A flower? • A rock? • Another bug?

  22. Chapter 2Bug Variations

  23. Methods of the Bug class • The Bug class provides three methods that specify how bugs move and turn. • public boolean canMove() • public void move() • public void turn() • These methods are in the act method

  24. public void act() { if (canMove()) move(); else turn(); }

  25. public boolean canMove() { Grid <Actor> gr = getGrid(); if (gr == null) return false; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (!gr.isValid(next)) return false; Actor neighbor = gr.get(next); return (neighbor == null) || (neighbor instanceOf Flower); // ok to move into empty location or onto flower // not ok to move onto any other actor }

  26. public void move() { Grid <Actor> gr = getGrid(); if (gr == null) return; Location loc = getLocation(); Location next = loc.getAdjacentLocation(getDirection()); if (gr.isValid(next)) moveTo(next); else removeSelfFromGrid(); Flower flower = new Flower(getColor()); flower.putSelfInGrid(gr,loc); }

  27. public void turn() { setDirection(getDirection() + Location.HALF_RIGHT); }

  28. Extending the Bug Class • A new type of bug with different behavior can be created by extending the Bug class and overriding the act method. • No new methods need to be added; the act method uses the three auxiliary methods from the Bug class.

  29. BoxBug Class • A BoxBug moves in a square pattern. • In order to keep track of its movement, the BoxBug class has two instance variables • sideLength • steps.

  30. Exercise Set 2 • What is the role of the instance variable sideLength? • What is the role of the instance variable steps? • Why is the turn method called twice when steps becomes equal to sideLength? • Why can the move method be called in BoxBug when there is no move method in the BoxBug class? • After a BoxBug is constructed will the size of the square pattern always be the same? • Can the path a BoxBug travels ever change? • When will the value of steps be zero?

  31. Exercise Answers Set 2 • What is the role of the instance variable sideLength? • The number of grid cells in one side of the box – 1. • What is the role of the instance variable steps? • Keeps track of the current number of steps the bug has taken on the current side. • Why is the turn method called twice when steps becomes equal to sideLength? • To make a 90 degree turn (each turn is 45 degrees) • Why can the move method be called in BoxBug when there is no move method in the BoxBug class? • It is inherited from Bug since the BugBog class extends the Bug class • After a BoxBug is constructed will the size of the square pattern always be the same? • Yes, there is no mutator (modifier) method in BoxBug for sideLength • Can the path a BoxBug travels ever change? • Yes, if it can’t move it turns 90 degrees. • When will the value of steps be zero? • When it is constructed and after each 90 degree turn.

  32. Runner Classes • In order to observe the behavior of one or more actors, a “runner” class is required. • That class constructs an ActorWorld object, places actors into it, and shows the world. • BugRunner • BoxBugRunner • When a new class is written that extends Bug, you also need to create a similar runner class.

  33. BoxBugRunner import info.gridworld.actor.ActorWorld; import info.gridworld.grid.Location; import java.awt.Color; public class BoxBugRunner { publicstaticvoid 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(); } } not tested on the AP exam

  34. Exercise 1 • Write a class CircleBug that is identical to BoxBug, except that in the act method the turn method is called once instead of twice. • How is its behavior different from a BoxBug?

  35. BoxBug • Students need to understand • Bug constuctors • act method • Students will need to use • canMove • turn • move • Students can create a new type of Bug by modifying BoxBug code

  36. publicclass BoxBug extends Bug { privateint steps; privateint sideLength; public BoxBug(int length) { steps = 0; sideLength = length; } publicvoid act() { if (steps < sideLength && canMove()) { move(); steps++; } else { turn(); turn(); steps = 0; } } } BoxBug Code

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

  38. Exercise 2 • Write a class SpiralBug that drops flowers in a spiral pattern. • Hint: Imitate BoxBug, but adjust the side length when the bug turns. You may want to use an UnboundedGrid to see the spiral pattern more clearly.

  39. Exercise 3 • Write a class ZBug to implement bugs that move in a “Z” pattern, starting at the top left corner. • After completing one “Z” pattern, a ZBug should stop moving. • Supply the length of the “Z” as a parameter in the constructor. • Hint: Notice that a ZBug needs to be facing east before beginning its “Z” pattern.

  40. Exercise 4 • Write a class DancingBug that “dances” by making different turns before each move. • The DancingBug constructor has an integer array as a parameter. • The integer entries in the array represent how many times the bug turns before it moves.

  41. Exercise 5 • Study the code for BoxBugRunner class. Summarize the steps you would use to add another BoxBug actor to the grid.

  42. Chapter 3: GridWorld Classes and Interfaces

  43. Main classes are: • Location – Class • Grid of Actors - Interface • BoundedGrid of Actors - Class • UnboundedGrid of Actors - Class • Actor – Class • Bug – Class • Flower – Class • Rock - Class

  44. Location Class • Encapsulates the coordinates for an actor’s position in a grid • Row and Column number • Rows increase going down • Columns increase going right • Also has methods that determine relationships between locations and compass directions • Every actor has a direction • 0 for north, 45 is northeast, … • Location class also has eight constants that specify the constant direction • NORTH, NORTHEAST, …

  45. Location Class public Location (int r, int c) constructs a location with row r and column c public int getRow() returns the row of this location public int getCol() returns the column of this 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 Testable API

  46. Location Class public boolean equals(Object other) returns true if other is a Location object with the same row and column values as this location, and false otherwise public int hashCode() returns a hash code for this location public int compareTo(Object otherObject) returns a negative integer if this location is less than other, zero if the two locations are equal, or a positive integer if this location is greater than other. Locations are ordered in row-major order public String toString() returns a string with the row and the column of this location, in the format (row, col) Testable API

  47. Location Class Compass directions: public static final int NORTH = 0; public static final int EAST = 90; public static final int SOUTH = 180; public static final int WEST = 270; public static final int NORTHEAST = 45; public static final int SOUTHEAST = 135; public static final int SOUTHWEST = 225; public static final int NORTHWEST = 315;

  48. Location Class Turn angles: public static final int LEFT = -90; public static final int RIGHT = 90; public static final int HALF_LEFT = -45; public static final int HALF_RIGHT= 45; public static final int FULL_CIRCLE = 360; public static final int HALF_CIRCLE = 180 ; public static final int AHEAD = 0;

  49. Example Location loc1 = new Location(5,7); Location loc2 = loc1.getAdjacentLocation(Location.WEST); Location loc3 = loc1.getAdjacentLocaton(Location.NORTHEAST); int dir = loc1.directionToward(new Location(6,8));

  50. Example Location loc1 = new Location(5,7); Location loc2 = loc1.getAdjacentLocation(Location.WEST); // (5,6) Location loc3 = loc1.getAdjacentLocaton(Location.NORTHEAST); // (4,8) int dir = loc1.directionToward(new Location(6,8)); // 135

More Related