import java.awt.*;
import java.util.Random;
import java.util.Iterator;

/**
 * Pellets, to be eaten by Caterpillars in the MunchGame.
 *
 * @see MunchGame
 */
public class Pellets
{
    // Private variables giving the characteristics of the pellets
    private Color color;                     // pellet color
    private int numPellets;                  // the number of pellets
    private SimpleList pellets = new LinkedSimpleList(); // the pellets
    private Random gen = new Random();       // random number generator

    /**
     * Create the new pellets, with the given color and randomly
     * chosen positions.
     *
     * @param c the pellet color
     * @param num the number of pellets
     * @param n the number of pellets to create
     */
    public Pellets(Color c, int num)
    {
		color = c;
		numPellets = num;

		for (int i=0; i<num; i++)
		{
			Point p = new Point(gen.nextInt(MunchGame.BoardWidth-1)+1,
	  	  						gen.nextInt(MunchGame.BoardHeight-1)+1);
	  	  	pellets.add(p);
		}
    }


    /**
     * Determine whether any pellets lie on the given position.
     *
     * @param np the position to test
     * @return true if any pellet lies on np
     */
    public boolean inPosition(Point p)
    {
		Iterator i = pellets.iterator();
		while (i.hasNext())
		{
	    	Point pellet = (Point)i.next();
	    	if (p.equals(pellet))
	    		return true;
		}
		return false;
    }

    /**
     * Remove the pellet at point p from the set of pellets.
     *
     * @param p the pellet to remove
     */
    public void remove(Point p)
    {
		pellets.remove(p);
    }


    /**
     * Determine whether all of the pellets have been consumed
     *
     * @return true if there are no more pellets
     */
    public boolean allGone()
    {
		return pellets.isEmpty();
    }


    /**
     * Draw the pellets on the given graphics pane
     *
     * @param g the graphics pane on which to draw the pellet
     */
    public void paint (Graphics g)
    {
		g.setColor(color);
		Iterator i = pellets.iterator();
		while (i.hasNext())
		{
	    	Point pellet = (Point)i.next();
			g.fillOval(5 + MunchGame.SegmentSize * pellet.x + 2,
					   15 + MunchGame.SegmentSize * pellet.y + 2, 
					   MunchGame.SegmentSize - 4,
					   MunchGame.SegmentSize - 4);
		}
    }
}
