import java.awt.Color;
import java.util.Enumeration;

public abstract class AffinityWalker extends RandomWalker {
	// OVERVIEW: An AffinityWalker is an object that will move
	//     around the grid until it meets two of the same kind.
	
	private boolean inTurn; //Is moving
	
	public boolean GetinTurn(){
		return inTurn;
	}

	public AffinityWalker (){
		inTurn = false;
	}
	
	public Color getColor (){
		if (inTurn){
			return Color.white;
		}else {
			return Color.yellow;
		}
	}
		
	private boolean likeNeighbors(){
		Enumeration neighbors = getNeighbors();
		int nbs=0;
		while (neighbors.hasMoreElements()) {
			SimObject neighbor = (SimObject) neighbors.nextElement();
			if(someoneIlike(neighbor) == true){
				nbs++;
			}
		}
		if (nbs>1)
			return true;
		else 
			return false;	
	}	
	
	abstract public boolean someoneIlike(Object neighbor);
	
	public  void executeTurn () throws RuntimeException
	{
		synchronized(getGrid()){
			if (likeNeighbors () == false){
				inTurn = true;
				super.executeTurn();
			}
		}
                inTurn = false;
	}
}