Guest User

Untitled

a guest
Jun 13th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import info.gridworld.actor.Critter;
  2. import info.gridworld.actor.Actor;
  3. import info.gridworld.grid.Location;
  4. import info.gridworld.grid.Grid;
  5. import java.util.Random;
  6. import java.awt.Color;
  7. import java.util.ArrayList;
  8.  
  9. /**
  10.  * A Mammal is either male or female. Females are pink and males are blue. A mammal will mate
  11.  * with another mammal when they come into contact with each other 75% of the time. When they do
  12.  * mate they spawn an offspring that has a 50/50 chance of beeing either male or female. The offspring
  13.  * gets the same name as the parent of its gender.
  14.  *
  15.  * @author Shaan Iqbal
  16.  * @version 02/02/2012
  17.  */
  18. public class Mammal extends Critter
  19. {
  20.     private String id;
  21.     private String gender;
  22.     private int stepsTaken;
  23.    
  24.     /**
  25.      * Creates a new Mammal of a given gender and name. Males are blue and females are pink.
  26.      *
  27.      * @param gen gender of the Mammal
  28.      * @param name the name of the Mammal
  29.      */
  30.     public Mammal(String gen, String name)
  31.     {
  32.         id = name;
  33.         gender = gen;
  34.  
  35.         if (gender.equals("F"))
  36.         {
  37.             setColor(Color.PINK);
  38.         }
  39.         else if (gender.equals("M"))
  40.         {
  41.             setColor(Color.BLUE);
  42.         }
  43.  
  44.         stepsTaken = 0;
  45.     }
  46.    
  47.     /**
  48.      * Gets the gender of a mammal.
  49.      *
  50.      * @return the gender of the mammal
  51.      */
  52.     public String getGender()
  53.     {
  54.         return gender;
  55.     }
  56.    
  57.     /**
  58.      * Gets the id, or name, of the Mammal.
  59.      *
  60.      * @return the id of the mammal
  61.      */
  62.     public String getId()
  63.     {
  64.         return id;
  65.     }
  66.    
  67.     /**
  68.      * Creates a new Mammal object in a random empty adjacent location to this Mammal.
  69.      * The new Mammal is either male or female (50/50 chance) and has the same id as the
  70.      * parent of the same gender.
  71.      */
  72.     public void mate()
  73.     {
  74.         Random gen = new Random();
  75.         Grid gr = getGrid();
  76.         Location loc = getLocation();
  77.         ArrayList<Location> locNext = gr.getEmptyAdjacentLocations(loc);
  78.  
  79.         if (gen.nextInt(2) == 0)
  80.         {
  81.             Mammal maleOffspring = new Mammal("M" , getId());
  82.             Location spawn = locNext.get(gen.nextInt(locNext.size() -1));
  83.             maleOffspring.putSelfInGrid(gr , spawn);
  84.         }
  85.         else
  86.         {
  87.             Mammal femaleOffspring = new Mammal ("F" , getId());
  88.             Location spawn = locNext.get(gen.nextInt(locNext.size() -1));
  89.             femaleOffspring.putSelfInGrid(gr , spawn);
  90.         }
  91.     }
  92.    
  93.     /**
  94.      * Two Mammals will mate if they are of opposite gender and have taken more than three steps.
  95.      */
  96.     public void processActors(ArrayList<Actor> actors)
  97.     {
  98.         Random gen = new Random();
  99.  
  100.         for (Actor a : actors)
  101.         {
  102.             if (a instanceof Mammal && !(this.getColor().equals(a.getColor())) && stepsTaken > 3)
  103.             {
  104.                 if ((gen.nextInt(4) < 3))
  105.                 {
  106.                     mate();
  107.                 }
  108.             }
  109.         }
  110.  
  111.     }
  112.    
  113.     /**
  114.      * Moves the Mammal to a new location. Each time it moves the stepsTaken instance varibale is updated.
  115.      */
  116.     public void makeMove(Location loc)
  117.     {
  118.         if (loc == null)
  119.         {
  120.             removeSelfFromGrid();
  121.         }
  122.         else
  123.         {
  124.             moveTo(loc);
  125.         }
  126.  
  127.         stepsTaken++;
  128.     }
  129. }
Add Comment
Please, Sign In to add comment