Unit 9 - GridWorld Unit 9 - GridWorldx

Download Report

Transcript Unit 9 - GridWorld Unit 9 - GridWorldx

See Class website for links to Gridworld
Code, installation and student manual
http://staff.rentonschools.us/hhs/apcomp-science/gridworld-links

About ¼ of the AP Exam will be related to the
case study
◦ Roughly 6-8 multiple choice questions and 1 free
response question


For some classes you are only expected to
know the API
For others, you are expected to know the
implementation

You will be asked to:
◦ Demonstrate understanding of the existing code
◦ Describe changes to existing code to get new
behavior
◦ Extend existing classes to get new types of Actors
◦ Use GridWorld classes to implement other programs
(such as games)

Single constructor:
◦ Location(int r, int c)

Accessors:
◦ int getRow()
◦ int getCol()
◦ Location getAdjacentLocation(int direction)
 Gets the Location next to this one in the given
direction
◦ int getDirectionToward(Location target)
 Gets the direction in which target lies from this
Location
 Uses only compass directions

Constant fields - Directions:
◦
◦
◦
◦
◦
◦
◦
◦
int
int
int
int
int
int
int
int
NORTH = 0
EAST = 90
SOUTH = 180
WEST = 270
NORTHEAST =
SOUTHEAST =
SOUTHWEST =
NORTHWEST =
45
135
225
315

Constant fields - Angles:
◦
◦
◦
◦
◦
◦
◦
int
int
int
int
int
int
int
LEFT = -90
RIGHT = 90
HALF_LEFT = -45
HALF_RIGHT = 45
FULL_CIRCLE = 360
HALF_CIRCLE = 180
AHEAD = 0



Actor is the base class which
all things that…act will extend
Most of the work you do will be extending or
changing Actor or one of its subclasses
You will want to be very comfortable with the
Actor API


Single, default constructor
Accessors:
◦
◦
◦
◦

Color getColor()
int getDirection()
Grid<Actor> getGrid()
Location getLocation()
Modifiers:
◦ setColor(Color newColor)
 Color class contains public final fields for a bunch of
colors
◦ setDirection(int newDirection)
 Directions are integers with 0 <= d <= 359

Working in Grids
◦ putSelfInGrid(Grid<Actor> gr, Location loc)
◦ removeSelfFromGrid()
 Do not try to add Actors to Grids directly– use these
methods
◦ moveTo(Location newLocation)

Acting
◦ act()
 By default, just reverses direction (rotates 180
degrees)
 Will be override in almost all subclasses of Actor

The Rock class
◦ Two constructors:
 Rock()
 Creates a black rock
 Rock(Color rockColor)
 Creates a rock of the given color
◦ act() overriden to do nothing!
 (Yes, you can do that.)

The Flower class
◦ Two constructors:
 Flower()
 Creates a pink flower
 Flower(Color initialColor)
 Creates a flower of the given color
◦ act() overriden to darken the flower’s color


Exercise 1: Extend the Flower class by creating
SpinningFlower which rotates half a turn to the
right each time it acts (in addition to darkening)
Exercise 2: Create a DyingFlower class that
extends Flower and acts like a flower, except
after a given number of actions, it “dies” by
removing itself (lifetime specified in constructor)
◦ Hint: you will need a variable to track the number of
actions performed


Bugs are Actors that move around
the Grid in various ways
As they move, Bugs leave behind Flowers
◦ The Flower will be the same color as the Bug

Two constructors
◦ default and with a single Color argument

act() overridden to:
◦ Check if the Bug can move straight ahead
◦ If so, move and leave a Flower behind
◦ Otherwise, turn

New methods
◦ canMove()
 Determines if the Bug can move forward (space ahead
is empty or contains a Flower)
◦ move()
 Moves the Bug forward and leaves a Flower in the
previously occupied location
◦ turn()
 Turns the Bug 45 degrees to the right (half-turn right)



We can create new types of Bugs by extending the
Bug class
BoxBug draws a square with the Flowers it leaves
behind
Two new instance variables:
◦ int sideLength
◦ int steps

One new constructor:
◦ BoxBug(int n)
 Creates a BoxBug that will draw a square of the given size

Overrides act()
◦ Does not override or add any other methods!


Exercise 3: Create a CircleBug
class extending Bug that
works like BoxBug, but draws a
circle instead of a square
Exercise 4: Create a
ZigZagBug class extending
Bug that walks in a zig-zag
pattern

Critters are Actors that interact
with other Actors in the Grid
◦ The specific way they interact can vary by the type of
Critter

Critters can also move non-deterministically
◦ That is, the Critter might not move the same way
even though the circumstances are the same

You are discouraged from overriding act()
◦ If you need to modify act(), you probably shouldn’t
extend Critter






Exercise 5: Without looking at the source code try
to figure out what each of getActors(),
processActors(), getMoveLocations(),
selectMoveLocation(), and makeMove() do in the
Critter class
getActor(): gets all neighboring Actors
processActors(): “eats” (removes) all neighbors (except
Rocks or Critters)
getMoveLocations(): gets all empty neighboring locations
selectMoveLocation(): chooses an empty neighbor randomly
makeMove(): moves to selected location

A ChameleonCritter is a Critter
except that:
◦ Instead of “eating” its neighbors, it picks one at
random and changes its color to match that
neighbor
◦ It turns to face the direction in which it moves

Which methods does ChameleonCritter
override?
◦ processActors() and makeMove()


Exercise 6: Write a class HungryCritter that
acts like a ChameleonCritter except that it
eats any neighbors that have the same color
it does after changing color
Exercise 7: Write a class RockCritter that
acts like a Critter, except that instead of
eating neighbors, it turns them all into Rocks
of the same color