

import java.awt.*;

 class Population

   {

     private static final int MAX_POP = 1000;

     private Creature mr_primal;
     private Creature ms_primal;
     private Creature all_creatures[];
     private int how_many;


     public Population()
        {
           all_creatures = new Creature[MAX_POP];
           all_creatures[0] = new Creature (150,50);
           all_creatures[1] = new Creature (170,89);
           all_creatures[2] = new Creature (100,202);
           all_creatures[3] = new Creature (78,65);
           how_many = 4;

        }

     public void advance_time(int t)
         {
           for(int i=0;i<how_many;i++)   //  Move everyone
              all_creatures[i].move();

   //        kill_weakest(n_replace);
           if(how_many+3 < MAX_POP-1)
              reproduce_strongestN(3);
//                 new Creature((int) ((World.east-World.west)*Math.random()+World.west),
//                      (int) ((World.south-World.north)*Math.random()+World.north));

            }

     //  One way to implement distance:  have the pop take two creatures
     //   and calculate directly from their coordinates.
     //  Return the distance (in pixels)
     //    between two creatures, as an integer.
     public int distance(Creature A, Creature B)
        {
           int d;

           d=  (A.getX() - B.getX())^2;

           return d;
        }

     public int distance2(Creature A, Creature B)
        {
          return (A.distanceC(B));
         }

     public void annihilate(Graphics g)
        {
          for(int i=0;i<how_many;i++)
              all_creatures[i].explode(g);

         }

     public int neighbor_count(Creature me)
        {
          int count = 0;

      /*  Loop through array to vary the variable you over the entire
           creature array  */
            if(me.neighbor(you))
               count++;
      /*  End of loop body.  */

          return count;
        }

     public void neighbor_counts()
        {
           for(i= 0 to how_many-1, i++)
             all_creatures[i].num_neighbors = neighbor_count(all_creatures[i]);
         }

//  Kill the nk least healthy creatures in the array.  The array
//  will shrink by nk in size (i.e., how_many -= nk)
     public void kill_weakest(int nk)
        {
           //  Repeatedly find and remove the least healthy.
           //  Once one is found, you can "shift down" all
           //  creatures that are "above" the weakest (i.e.,
           //  have array subscripts greater than that of the
           //  weakest) or you can simply copy the last creature
           //  over the weakest, and decrement how_many.
        }

//  Find the nr healthiest creatures in the array, copy them, and
//   insert them at the end.
     public void reproduce_strongestN(int nr)
        {
             for(int i = 0; i < nr; i++)
                 reproduce_strongest();
        }
 //  Find the healthiest creature in the array, copy it, and
 //   insert it at the end.
     public void reproduce_strongest()
	    {

	     //  First find the healthiest creature in the array.
	     //  Here I keep track of TWO things: (1) the array INDEX
	     //  (i.e., the spot in the array) of the healthiest
	     //  creature seen so far in the loop, and (2) the actual
	     //  VALUE (i.e., the health) of the healthiest creat. found
	     //  so far.
	      int healthiest_creature = 0;
	      int max_health = -1000;
	      for(int i=0; i < how_many; i++)
	          if(all_creatures[i].gethealth() > max_health)
	             {max_health = all_creatures[i].gethealth();
	              healthiest_creature = i;
	              }
	      //  Next make a copy of this healthiest creature and
	      //  add it to the END of the array (i.e., at the next
	      //  avail. slot in the array, which is array[how_many].

	      //  Here is the RIGHT way to make a copy of a creature in
	      //   the array.

	      all_creatures[i].distanceC(all_creatures[healthiest_creature]);


	      all_creatures[how_many] =
	          all_creatures[healthiest_creature].reproduce();



	     //  Here is the WRONG way to copy creatures in the array.
	     //   But TRY it.  I want you to see what happens and think about
	     //   WHY.  (WHAT is being copied from one spot in array to
	     //  another?
	     //
	     // all_creatures[how_many] = all_creatures[healthiest_creature];

	      how_many++;

        }

     public void paint(Graphics g)
        {
          for(int i=0;i<how_many;i++)
              all_creatures[i].paint(g);
         }

   }


