import java.awt.*;
import java.util.*;

public class GridAnt implements GridThing {
    boolean loaded = false, dropping=false;
    int duration=0;
    int wanderLust = 8;
    double lastDouble=-1;
    static Random r = new Random();
    int lastMove;

    public void behave(GridSquare[][] g, int mx, int my, int move){
	int gridSize = g[0].length;
	if(move == lastMove)
	    return;
	lastMove = move;
	GridSquare right = g[(mx+1)%gridSize][my];
	GridSquare left = g[(mx+(gridSize-1))%gridSize][my];
	GridSquare up = g[mx][(my+1)%gridSize];
	GridSquare down = g[mx][(my+(gridSize-1))%gridSize];
	if((duration < wanderLust) || loaded){
	    // Wander around
	    wander(g, mx, my);
	    duration++;
	}
	/*	else if(loaded){
	    // Look for a place to put it down
	    loaded = false;
	    dropping = true;
	    if((right.thing instanceof GridDirt) ||
	       (left.thing instanceof GridDirt) ||
	       (down.thing instanceof GridDirt) ||
	       (up.thing instanceof GridDirt)){
		if(right.thing == null){
		    right.addThing(new GridDirt());
		    loaded = false;
		    duration = 0;
		}
		else if(left.thing == null){
		    left.addThing(new GridDirt());
		    loaded = false;
		    duration = 0;
		}
		else if(down.thing == null){
		    down.addThing(new GridDirt());
		    loaded = false;
		    duration = 0;
		}
		else if(up.thing == null){
		    up.addThing(new GridDirt());
		    loaded = false;
		    duration = 0;
		}
	    }
	    else{
		wander(g, mx, my);
	    }
	}
	    */
	else{
	    GridSquare trysq;
	    for(int t=0;t<5;t++){
		double rn = r.nextDouble();
		String dir;
		if(rn > .75){
		    trysq = up;
		    dir = new String("up");
		}
		else if(rn > .5){
		    trysq = down;
		    dir = new String("down");
		}
		else if(rn > .25){
		    trysq = left;
		    dir = new String("left");
		}
		else{
		    trysq = right;
		    dir = new String("right");
		}
		
		if(trysq.thing instanceof GridDirt){
		    trysq.addThing(null);
		    loaded = true;
		    duration = 0;
		    return;
		}
	    }
	    duration++;
	    wander(g, mx, my);
	}
    }

    public void wander(GridSquare[][] g, int mx, int my){
	int gridSize = g[0].length;
	GridSquare right = g[(mx+1)%gridSize][my];
	GridSquare left = g[(mx+(gridSize-1))%gridSize][my];
	GridSquare up = g[mx][(my+1)%gridSize];
	GridSquare down = g[mx][(my+(gridSize-1))%gridSize];

	GridSquare trysq;
	for(int t=0;t<5;t++){
	    double rn = r.nextDouble();
	    if((lastDouble > 0) &&
	       (rn > .1))
		rn = lastDouble;
	    else
		rn = r.nextDouble();

	    lastDouble = rn;
	    String dir;
	    if(rn > .75){
		trysq = up;
		dir = new String("up");
	    }
	    else if(rn > .5){
		trysq = down;
		dir = new String("down");
	    }
	    else if(rn > .25){
		trysq = left;
		dir = new String("left");
	    }
	    else{
		trysq = right;
		dir = new String("right");
	    }

	    if(trysq.thing == null){
		//		System.out.println("Wandering "+dir+", dropping "+dropping);
		trysq.addThing(this);
		if(loaded && ((right.thing instanceof GridDirt) ||
				(left.thing instanceof GridDirt) ||
				(down.thing instanceof GridDirt) ||
				(up.thing instanceof GridDirt))) {
		    g[mx][my].addThing(new GridDirt());
		    loaded = false;
		    duration  = 0;
		}
		else
		    g[mx][my].addThing(null);

		return;
	    }
	}
    }

    public Color getColor() {
	if(loaded || dropping)
	    if(duration < wanderLust)
		return Color.cyan;
	    else
		return Color.blue;
	else if(duration < wanderLust)
	    return Color.pink;
	else
	    return Color.red;
    }
}
