/Users/petercappello/NetBeansProjects/56-2014/56-2014-4-AdaptableZapem/src/Game.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.ArrayList;

/**
 * Zapem game model
 *
 * @author Peter Cappello
 */
public class Game 
{
    /**
     * The edge length, in pixels, of the displayed Critter field.
     */
    public final static int IMAGE_SIZE = 600;
    private final static int N_CRITTERS = 5;

    /**
     * The edge length, in pixels, of each Critter object.
     */
    protected static final int EDGE_LENGTH = 60;
    
    private final Image image;
    private final Graphics graphics;
    
    private List<Critter> critterList;
    private boolean isOver;
        
    Game() 
    {         
        image = new BufferedImage( IMAGE_SIZE, IMAGE_SIZE, BufferedImage.TYPE_INT_RGB );
        graphics = image.getGraphics();
        graphics.setColor( Color.white );
        graphics.fillRect( 0, 0, IMAGE_SIZE, IMAGE_SIZE );
        isOver = true;
    }
    
    /**
     * Gets a reference to the Image field.
     * @return a reference to the Image field.
     */
    public Image getImage() { return image; }
    
    /**
     * Starts a new game.
     */
    public void start()
    {
        isOver = false;
        createCritters();
    }
    
    private void createCritters()
    {
        critterList = new ArrayList<>();
        for ( int i = 0; i < N_CRITTERS; i++ )
        {
            critterList.add( Critter.makeCritter() );
        }
    }
        
    /**
     * Draws each Critter in its current position after blanking the image.
     */
    public void draw()
    {       
        graphics.setColor( Color.white );
        graphics.fillRect( 0, 0, IMAGE_SIZE, IMAGE_SIZE );
        for ( Critter critter : critterList )
        {
            critter.move();
            critter.draw( graphics );
        }
    }
    
    /**
     * Is the game over?
     * The definition of "game over" is that the Critter List is empty.
     * @return true if and only if the game is over.
     */
    public boolean isOver() { return isOver; }
    
    /**
     * Processes a mouse click.
     * Remove the 1st Critter for which the coordinates of the mouse click 
     * are within the Critter's boundary.
     * @param x horizontal coordinate of mouse click.
     * @param y vertical coordinate of mouse click.
     */
    public void processClick( int x, int y )
    {
        if ( isOver )
        {
            return;
        }
        for ( int i = 0; i < critterList.size(); i++ )
        {
            if ( critterList.get( i ).isIn( x, y ) )
            {
                critterList.remove( i );
                isOver =  critterList.isEmpty();
                break;
            }
        }
    }
}