import java.awt.Color;

public class Stalker extends MobileSimObject {
    // OVERVIEW: A RandomWalker is a drunk simulator object that performace a random walk,
    //    moving one square in a random direction each step.

    private boolean inTurn; // Currently in the middle of a turn (used for coloring)

    public Stalker () {
	inTurn = false;
    }

    public String getName() {
      return "Stalker";
    }

    public Color getColor() {
		if (inTurn) {
	    	return Color.yellow;
		} else {
	    	return Color.green;
		}
    } //@nowarn NonNullResult // ESC/Java doesn't know Color constants are not null

    public void executeTurn() throws RuntimeException
       // Note: requires isInitialized is inherited from SimObject
       // EFFECTS: Picks a random direction and tries to move that way.
       //          If the move is successful, return true. If the move fails
       //          because the spot being attempted to move into is already
       //          occupied then return false.

    {
	inTurn = true;
	Direction dir = Direction.randomDirection ();
	int newrow = getRow () + dir.northerlyDirection ();
	int newcol = getColumn () + dir.easterlyDirection ();
	
	synchronized (getGrid()) {
		if (getGrid ().validLocation (newrow, newcol)) {
	    	if (getGrid().isSquareEmpty (newrow, newcol)) {
		   		setLocation (newrow, newcol);
			}
		}
		inTurn = false;
    }
	}
}