diff --git a/Cow.java b/Cow.java index 2e210a2..7f879f3 100755 --- a/Cow.java +++ b/Cow.java @@ -12,35 +12,71 @@ */ //import java.Exception.*; +import java.util.HashMap; +import java.util.Iterator; public class Cow { private static int idCounter = 1; + private static int maxGen = 1; private int id; private int generation; + private int[] parents; private String name; private String sex; - private int milk, strength, meat, aggression; + private HashMap traits; + private int milk, strength, meat, aggression; //TODO convert to dict + /* + * This constructor provides a default cow or bull to start the game with. + */ public Cow(String desiredSex) { - //Default cow/bull sex = desiredSex; id = idCounter; idCounter++; name = makeName(sex, id); - generation = 0; - milk = 1; - strength = 5; - meat = 1; - aggression = 10; + generation = 1; + traits = new HashMap(); + traits.put("milk", 1); + traits.put("strength", 5); + traits.put("meat", 1); + traits.put("aggression", 10); + parents = new int[] {0,0}; } + /* + * This constructor is used when a cow is born through breeding. + */ public Cow(Cow father, Cow mother) { - //Constructor used for reproduction - //TODO + //Figure out the ID and save the parents' ID numbers + id = idCounter; + idCounter++; + parents = new int[] {father.getID(), mother.getID()}; + //Figure out the generation + if (father.getGeneration() > mother.getGeneration()) + generation = father.getGeneration()+1; + else generation = mother.getGeneration()+1; + if (generation > maxGen) maxGen = generation; + //Choose a random sex + if (Herd.random.nextInt(2) == 1) sex = "male"; + else sex = "female"; + name = makeName(sex, id); + //Calculate new trait values + traits = new HashMap(); + Iterator keys = mother.getTraits().keySet().iterator(); + while (keys.hasNext()) { + String k = keys.next(); + traits.put(k, combineTrait(father.getTraits().get(k), + mother.getTraits().get(k))); + } + } + + public static int getMaxGen() + { + return maxGen; } public int getID() @@ -48,6 +84,12 @@ return id; } + public int getGeneration() + { + return generation; + } + + public String getName() { return name; @@ -58,9 +100,9 @@ return sex; } - public int[] getStats() + public HashMap getTraits() { - return new int[] {milk, strength, meat, aggression}; + return traits; } public String rateCow(Cow c, String use) @@ -122,5 +164,11 @@ else return "ERROR No such sex: "+sex; //else throw Exception("No such sex: "+sex); } + + private int combineTrait(int fatherTrait, int motherTrait) + { + //TODO + return 0; + } }