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

public class GridFighter implements GridThing {
    public int strength=0;
    double lastDouble=-1;
    int lastMove=0;
    static Random r = new Random();
    Color strongest = new Color(255, 0, 0);
    Color strong = new Color(255, 0, 100);
    Color weak = new Color(100, 0, 255);
    Color weakest = new Color(0, 0, 255);


    public void behave(GridSquare[][] g, int mx, int my, int move){
	if(move == lastMove)
	    return;
	lastMove = move;
	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();
	    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);
		g[mx][my].addThing(null);
		return;
	    }
	    else if(trysq.thing instanceof GridFighter){
		GridFighter opponent = (GridFighter) trysq.thing;
		int mystr = strength + ((int) (r.nextDouble()*10));
		int opstr = opponent.strength + ((int) (r.nextDouble()*10));

		//		System.out.println(strength+" v. "+opponent.strength);

		if(mystr == opstr)
		    return;
		strength+= ((int) (5/(mystr-opstr)));
		opponent.strength-=((int) (5/(mystr-opstr)));
	    }
	}
    }

    public Color getColor() {
	int cint = 128-(strength*5);
	if(cint > 255)
	    cint = 255;
	if(cint < 0)
	    cint = 0;

	return new Color(200, cint, cint);
    }

}
