import info.gridworld.actor.*;
import info.gridworld.grid.Location;
import java.util.ArrayList;
import java.awt.Color;
public class OpossumCritter extends Critter
{
private int numStepsDead;
public OpossumCritter()
{
numStepsDead=0;
setColor(Color.ORANGE);
} // end constructor
public void processActors( ArrayList<Actor> actors)
{
int numFriends=0;
int numFoes=0;
for( Actor a : actors )
{
if( isFriend( a ) )
{
numFriends++;
}
else if( isFoe( a ) )
{
numFoes++;
}
}
System.out.println( "numFoes is: " + numFoes );
System.out.println( "numFriends is: " + numFriends );
if( numFoes > numFriends )
{
setColor( Color.BLACK );
numStepsDead++;
}
else
{
setColor( Color.ORANGE );
numStepsDead=0;
}
System.out.println("num steps dead is:" + numStepsDead );
} // end method processActors
public Location selectMoveLocation( ArrayList<Location> locs )
{
if( numStepsDead == 3) // if 3, I'm dead
{
return null;
}
else if( numStepsDead > 0 ) //playing dead
{
return getLocation(); // just stay where I'm at
}
else
{
return super.selectMoveLocation( locs ); // i'm free!
} // move like a Critter
} // end method selectMoveLocation
private boolean isFriend( Actor other )
{
if( other instanceof Flower )
{
return true;
}
else
{
return false;
}
} // end method isFriend
private boolean isFoe( Actor other )
{
if( other instanceof Bug || other instanceof Critter )
{
return true;
}
else
{
return false;
}
} // end method isFoe
} // end class OpossumCritter
**************************************************************************
/*
* AP(r) Computer Science GridWorld Case Study:
* Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @author Cay Horstmann
*/
import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;
/**
* This class runs a world that contains a bug and a rock, added at random
* locations. Click on empty locations to add additional actors. Click on
* populated locations to invoke methods on their occupants. <br />
* To build your own worlds, define your own actors and a runner class. See the
* BoxBugRunner (in the boxBug folder) for an example. <br />
* 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(0,0), new Bug());
world.add(new Location(0,1), new Flower());
world.add(new Location(0,2), new Bug());
world.add(new Location(1,1), new OpossumCritter());
world.add(new Location(2,1), new Critter());
world.add(new Location(2,2), new Rock());
world.show();
}
}