Warning: include(/home/mathwa6/public_html/php_include/forum_left_side.php) [function.include]: failed to open stream: No such file or directory in /home/mathwa6/public_html/php_include/ontheLeft_moris.php on line 296
Warning: include() [function.include]: Failed opening '/home/mathwa6/public_html/php_include/forum_left_side.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/mathwa6/public_html/php_include/ontheLeft_moris.php on line 296
|
Grid World Student Labs
Critter Sub Class Assignments
import info.gridworld.actor.Critter ;
Directly from the mouths of the College Board:
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 the act method is unsuitable for your actors, you should consider extendig Actor, not Critter.
- public ArrayList<Actor> getActors()
- Postcondition
- 1) CANNOT change the state of the actors
- public processActors(ArrayList<Actor> actors
- PostConditions
- 1) State of all actors in the grid other than this critter and the elements of actors is unchanged (You can only mess with this critter and with ArrayList paramater).
- 2) location of this critter is unchanged
- public ArrayList<Location> getMoveLocations()
- Postcondition
- 1) state o all actors is unchanged
- public ArrayList<Location> SelectMoveLocations()
- Postconditions
- 1) Returned location is an element of locs, this critter's current location, or null
- 2) the state of all actors is unchanged
- public ArrayList<Location> getMoveLocations()
- public void MakeMove(Location loc)
- Postconditions
- 1) getLocation() == loc
- 2) state of all actors other than those at the old and new locations are unchanged
Extending Critter Labs
WizardCritter class
A WizardCritter is a Critter with the power to transform actors from one type to another (with the same color, direction, and location as they had before the transformation).
- A WizardCritter gets actors like a normal critter
- If a WizardCritter is next to any non-WizardCritter actors, he tranmutes them as follows:
- Bugs get transformed into Flowers
- Critters get transformed into Rocks
- Rocks get transformed into Bugs
- Flowers get transformed into Critters
Again, after the "transformation" the new object should have the same color, direction, and location as the original actor that was replaced.
- After transforming the actors around it, a WizardCritter apparates or teleports to a random empty location somewhere on the grid. This means your
getMoveLocations() method will work differently in an UnboundedGrid than it does in a BoundedGrid:
- on a BoundedGrid it will return all empty locations on the grid.
- on an UnboundedGrid, it will return all empty locations from your current location's row-5 to your current location's row+5, and your current location's column-5 to your current location's column+5, not including the current location.

Dementor class
A Dementor is a Critter (from Harry Potter, of course). Dementors are described as "the foulest beasts on Earth" and "soul-sucking fiends"; basically, bad guys. Our version will have the following:
- A private
Actor instance variable named victim. The Dementor will use this instance variable to follow its victim, not letting them escape.
- Two constructors:
- both set the color to
null
- the default constructor sets
victim=null (we'll set it later in our processActors method); the other one takes in an Actor input parameter and to initialize victim.
- a helper method with the following signature:
private double getDistance(Location loc)
- this method will see how far
loc is from you using the basic distance formula:

- The
getActors method returns all Actor objects on the grid
- The
processActors method is a little more complicated:
- first, it checks if
victim==null, or if the victim is no longer in the same grid as our Dementor
- if it either of the above is true, we need to get a new
victim. Go through your ArrayList<Actor> and pick the NON-ROCK and NON-FLOWER Actor closest to you (using your getDistance method)
- if there are no non-Rock or non-Flower Actors on the grid, a Dementor can't survive, so it should remove itself from the grid in saddness and
return.
- if we are adjacent to our victim, change its color to white (you're making it feel its worst fears, you see)
- if we aren't next to our vicitm, we it needs to clear the way between us and our victim.
- use the
getDirectionToward(Location target) method in the Location class to see what direction your victim is in
- use
getAdjacentLocation to get the Location in that Direction. If it contains a Rock or Flower, remove it. If it contains a non-Flower or non-Rock actor other than our victim, move that actor to any empty adjacent location out of the spot between us and our victim. If there are no empty adjacent spots, we're out of luck, so do nothing.
- the
getMoveLocations method has several parts, as well
- first, it checks if we are not in the grid, and returns
null if we aren't in the grid anymore (ie, we removed ourselves in the processActors method)
- if we are still in the grid, we must still have a victim. So use getDirectionToward(Location target) and getAdjacentLocation(int direction) to find and return the Location moving us towards our victim (or the victim's location if we are already adjacent to them). If the spot we want to go to is occupied by something other than our victim, return an empty ArrayList.
- the
selectMoveLocation method:
- again checks if we aren't in the grid and returns
null if we aren't in the grid anymore
- return
null if our input parameter is an empty ArrayList
- if we are still in the grid, return the (only one) location from
getMoveLocations
- the
makeMove method:
- again checks if we aren't in the grid anymore and returns if we aren't
- if we are still in the grid, check and see if our victim is already in the location we are supposed to move to. If it ISN'T, move there. If it is, just stay where you are.
- finally, whether you moved or not, turn to face the victim
Use your Circus.java driver to test out your new Object.
And here's a Dementor.gif if you want to use it.
Phoenix class
A Phoenix is a Critter. Actually, it is a mythological bird that has the power to come back from the dead. Ours will have the following behaviors:
- Set the color of a phoenix to
null in its constructor
- When it gets actors, it only looks at the spaces to the right, left, forward, right-forward, and left-forward. In other words, it doesn't get actors anywhere behind it
- When it processes actors, it eats or removes the actor if is a Bug.
- When it gets move locations, it only gets the locations forward, right-forward, and left-forward.
- When it makes its move, if the location it is going to is the same as its current location, turn 90 degrees to the right or left (choose randomly). Otherwise, move to the location, and turn to face the direction you are moving to, in the same way a ChameleonCritter does. So if the bird is facing north and moves left-forward, after moving its direction should be
Location.NORTHWEST.
- Override the
removeSelfFromGrid() method from Actor by first calling super.removeSelfFromGrid() to die as you are supposed to. Then you will use your phoenix powers to come back to life and put yourself back into the same grid in a new random, empty, and valid location. And yes, to get credit for this you actually have to remove yourself before re-adding yourself; you can't just call moveTo
- Download the image
|