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

public class WanderGrid extends Canvas implements Runnable {
    int squareSize = 20;
    int gridSize = 25;
    GridSquare grid[][] = new GridSquare[gridSize][gridSize];
    int ants = 0;
    int moveNum=0;
    Image img;
    Graphics theG;

    public static void main(String args[]){
	(new Thread((new WanderGrid()))).start();
    }

    public WanderGrid() {
	Random r = new Random();
	for(int x=0;x<gridSize;x++){
	    for(int y=0;y<gridSize;y++){
		double d = r.nextDouble();
		grid[x][y] = new GridSquare();
		/*		if(d > .75)
		    grid[x][y].addThing(new GridDirt());
		else if(d > .72){
		    if(ants <= 10000){
			grid[x][y].addThing(new GridAnt());
			ants++;
		    }
		}
		*/
		/*
		if(d > .50)
		    grid[x][y].addThing(new GridFighter());
		*/
		if(d > .95)
		    grid[x][y].addThing(new GridFooEater());
		else if(d > .9)
		    grid[x][y].addThing(new GridBarEater());
		else if(d > .8)
		    grid[x][y].addThing(new GridFoo());
		else if(d > .7)
		    grid[x][y].addThing(new GridBar());


	    }
	}
	Frame f = new Frame();
	f.add(this);
	f.setBounds(10, 10, 500, 500);
	f.show();
    }

    public void run() {
	img = createImage(gridSize*squareSize, gridSize*squareSize);
	theG = img.getGraphics();
	while(true){
	    int gbe=0;
	    int gfe=0;
	    for(int x=-1;x<gridSize;x++){
		for(int y=-1;y<gridSize;y++){
		    if((x < (gridSize-1)) && (y < (gridSize-1))){
			if(grid[x+1][y+1].thing instanceof GridBarEater)
			    gbe++;
			if(grid[x+1][y+1].thing instanceof GridFooEater)
			    gfe++;
			theG.setColor(grid[x+1][y+1].getColor());
			theG.fillRect((x+1)*squareSize, (y+1)*squareSize, 
				      squareSize, squareSize);
		    }
		    if((x >= 0) && (y>=0))
			grid[x][y].behave(grid, x, y, moveNum);
		}
	    }
	    System.out.println("Grid Eaters: "+gbe+"/"+gfe+"  "+(gfe+gbe));
	    moveNum++;
	    repaint();
	    try { Thread.sleep(80); }  catch (Exception e) { }
	}
    }

    public void update(Graphics g){
	paint(g);
    }

    public void paint(Graphics g){
	g.drawImage(img, 0, 0, this);
    }
}
