Part (a)
/** Whenever actors contains more foes than friends, this OpossumCritter plays dead.
* 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.
* @param actors a group of actors to be processed
*/
public void processActors(ArrayList<Actor> actors)
{
int friends = 0, foes = 0;
for (Actor a : actors)
{
if (isFriend(a))
friends++;
else if (isFoe(a))
foes++;
} 1
if (foes > friends)
{
numStepsDead++;
setColor(Color.BLACK);
}
else
{
numStepsDead = 0;
setColor(Color.ORANGE);
}
}
Notes:
- Or use one counter: add 1 for a friend, subtract 1 for a foe.
Part (b)
/** Selects the location for the next move.
* Postcondition: (1) The returned location is an element of locs, this critter's current location,
* or null. (2) The state of all actors is unchanged.
* @param locs the possible locations for the next move
* @return the location that was selected for the next move, or null to indicate
* that this OpossumCritter should be removed from the grid.
*/
public Location selectMoveLocation(ArrayList<Location> locs)
{
if (numStepsDead == 3)
return null;
else if (numStepsDead > 0)
return getLocation();
else
return super.selectMoveLocation(locs);
}
|