Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import info.gridworld.actor.Critter;
- import info.gridworld.actor.Actor;
- import info.gridworld.grid.Location;
- import info.gridworld.grid.Grid;
- import java.util.Random;
- import java.awt.Color;
- import java.util.ArrayList;
- /**
- * A Mammal is either male or female. Females are pink and males are blue. A mammal will mate
- * with another mammal when they come into contact with each other 75% of the time. When they do
- * mate they spawn an offspring that has a 50/50 chance of beeing either male or female. The offspring
- * gets the same name as the parent of its gender.
- *
- * @author Shaan Iqbal
- * @version 02/02/2012
- */
- public class Mammal extends Critter
- {
- private String id;
- private String gender;
- private int stepsTaken;
- /**
- * Creates a new Mammal of a given gender and name. Males are blue and females are pink.
- *
- * @param gen gender of the Mammal
- * @param name the name of the Mammal
- */
- public Mammal(String gen, String name)
- {
- id = name;
- gender = gen;
- if (gender.equals("F"))
- {
- setColor(Color.PINK);
- }
- else if (gender.equals("M"))
- {
- setColor(Color.BLUE);
- }
- stepsTaken = 0;
- }
- /**
- * Gets the gender of a mammal.
- *
- * @return the gender of the mammal
- */
- public String getGender()
- {
- return gender;
- }
- /**
- * Gets the id, or name, of the Mammal.
- *
- * @return the id of the mammal
- */
- public String getId()
- {
- return id;
- }
- /**
- * Creates a new Mammal object in a random empty adjacent location to this Mammal.
- * The new Mammal is either male or female (50/50 chance) and has the same id as the
- * parent of the same gender.
- */
- public void mate()
- {
- Random gen = new Random();
- Grid gr = getGrid();
- Location loc = getLocation();
- ArrayList<Location> locNext = gr.getEmptyAdjacentLocations(loc);
- if (gen.nextInt(2) == 0)
- {
- Mammal maleOffspring = new Mammal("M" , getId());
- Location spawn = locNext.get(gen.nextInt(locNext.size() -1));
- maleOffspring.putSelfInGrid(gr , spawn);
- }
- else
- {
- Mammal femaleOffspring = new Mammal ("F" , getId());
- Location spawn = locNext.get(gen.nextInt(locNext.size() -1));
- femaleOffspring.putSelfInGrid(gr , spawn);
- }
- }
- /**
- * Two Mammals will mate if they are of opposite gender and have taken more than three steps.
- */
- public void processActors(ArrayList<Actor> actors)
- {
- Random gen = new Random();
- for (Actor a : actors)
- {
- if (a instanceof Mammal && !(this.getColor().equals(a.getColor())) && stepsTaken > 3)
- {
- if ((gen.nextInt(4) < 3))
- {
- mate();
- }
- }
- }
- }
- /**
- * Moves the Mammal to a new location. Each time it moves the stepsTaken instance varibale is updated.
- */
- public void makeMove(Location loc)
- {
- if (loc == null)
- {
- removeSelfFromGrid();
- }
- else
- {
- moveTo(loc);
- }
- stepsTaken++;
- }
- }
Add Comment
Please, Sign In to add comment