2008 Gridworld
<< 2008Encoding | APQuestionsTrailIndex | 2008Checker >>
OpossumCritter
Here is my homemade 48 x 48 gif of the OpossumCritter that turns black in the presence of an enemy: OpossumCritter.gif
import info.gridworld.actor.*; import info.gridworld.grid.*; import java.util.ArrayList; import java.awt.Color; /** * @author Chris Thiel * @version 8 May 2008 */ public class OpossumCritter extends Critter { private int numStepsDead; public OpossumCritter() { numStepsDead=0; setColor(Color.ORANGE); } private boolean isFriend(Actor other) { if (other instanceof Bug) return true; else return false; } private boolean isFoe(Actor other) { if (other instanceof Critter) return true; else return false; } public void processActors(ArrayList<Actor> actors) { //part a } public Location selectMoveLocation(ArrayList<Location> locs) { //part b } }
OpossumCritterRunner
import info.gridworld.actor.*; import info.gridworld.grid.*; import java.awt.Color; /** * This class runs a world that contains critters. * This class is not tested on the AP CS A and AB exams. */ public class OpossumCritterRunner { public static void main(String[] args) { ActorWorld world = new ActorWorld(); world.add(new Location(7, 8), new Rock()); world.add(new Location(3, 3), new Rock()); world.add(new Location(2, 8), new Flower(Color.BLUE)); world.add(new Location(5, 5), new Flower(Color.PINK)); world.add(new Location(1, 5), new Flower(Color.RED)); world.add(new Location(7, 2), new Flower(Color.YELLOW)); world.add(new Location(4, 4), new OpossumCritter()); world.add(new Location(5, 8), new Critter()); world.add(new Location(8, 0), new Bug()); world.show(); } }
Hints
Part(a)
Hints: This is a Critter, not a Bug. Use your Quick reference guide. We need to keep track of the number of steps that the Opossum is playing dead, much like we kept track of the number of steps taken in the BoxBug and Spiral Bug, but since this is a Critter, we need to adapt the processActors method and the selectMoveLocation method. Note the postcondition: (1) The state of all actors in the grid other than this critter and the elements of actors is unchanged. (2) The location of this critter is unchanged. All we need to do is count the friends and foes, and either play dead or comeback to life. If you play dead we turn black, and orange otherwise. If we play dead, the number of steps that we play dead increases. If we are on more friendly ground, the number of steps that we are playing dead is reset to 0.
Part(b)
This is much easier than it sounds. If we are playing dead too long (3 steps) we return null, if we are playing dead the counter is non-zero, so we should return the current location, otherwise we return the result from the super class.