// Arup Guha
// 2/22/2010
// Solution to 2008 AP CS Computer Science A Free Response Question #3
// OpossumCritter

import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;
import java.awt.Color;

import java.util.ArrayList;

public class OpossumCritter extends Critter {
	
	private int numStepsDead;

	// This was given.	
	public OpossumCritter() {
		numStepsDead = 0;
		setColor(Color.ORANGE);
	}
	
	// Solution to Question #3a
	public void processActors(ArrayList<Actor> actors) {
		
		// Count friends.
		int numFriends = 0;
		for (Actor a: actors)
			if (isFriend(a))
				numFriends++;
		
		// Count foes.
		int numFoes = 0;
		for (Actor a: actors)
			if (isFoe(a))
				numFoes++;
		
		// Time to play dead =)	
		if (numFoes > numFriends) {
			setColor(Color.BLACK);
			numStepsDead++;
		}
		
		// Not dead!
		else {
			setColor(Color.ORANGE);
			numStepsDead = 0;
		}
	}
	
	// Solution to Question #3b
	public Location selectMoveLocation(ArrayList<Location> locs) {
		
		// Not playing dead, so move like a regular Critter.
		if (!getColor().equals(Color.BLACK))
			return super.selectMoveLocation(locs);
			
		// Stay in the same place.
		else if (numStepsDead < 3)
			return getLocation();
			
		// You disappear after playing dead for three steps in a row.
		else
			return null;
	}
	
	// I made this up. It's fairly arbitrary. I make you a friend if I've been 
	// playing dead for 2 steps or if your row+col is 10 or more.
	private boolean isFriend(Actor other) {
		
		// Add our row and column
		Location where = other.getLocation();
		int sum = where.getRow() + where.getCol();
		
		// This is the criteria I made up.
		if (sum >= 10 || numStepsDead == 2)
			return true;
		return false;
	}
	
	// I made this up also. You are a foe if your row+col is less than 10.
	private boolean isFoe(Actor other) {
		
		// Add our row and column
		Location where = other.getLocation();
		int sum = where.getRow() + where.getCol();
		
		// Use this criterion.
		if (sum < 10)
			return true;
		return false;
	}		
	
}