Newer
Older
moobreeder / Cow.java
/**
 * Moobreeder is a simple cow breeding game to illustrate the principles of
 * selective breeding to KS3 pupils.
 *
 * (c) Daniel Vedder 2018, Amano Christian School
 * Licensed under the terms of the MIT license.
 * https://git.synoikos.de/daniel/moobreeder
 */

/*
 * This file holds the cow class and associated methods.
 */

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 HashMap<String, Integer> traits;

	/*
	 * This constructor provides a default cow or bull to start the game with.
	 */
	public Cow(String desiredSex)
	{
		sex = desiredSex;
		id = idCounter;
		idCounter++;
		name = makeName(sex, id);
		generation = 1;
		traits = new HashMap<String, Integer>();
		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)
	{
		//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<String, Integer>();
		Iterator<String> 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()
	{
		return id;
	}

	public int getGeneration()
	{
		return generation;
	}

	public int[] getParents()
	{
		return parents;
	}
	
	public String getName()
	{
		return name;
	}

	public String getSex()
	{
		return sex;
	}

	public HashMap<String, Integer> getTraits()
	{
		return traits;
	}

	public String rateCow(String use)
	{
		int rating = 0;
		if (use == "dairy") {
			rating = rateDairyCow();
		}
		else if (use == "plowing") {
			rating = ratePlowCow();
		}
		else if (use == "meat") {
			rating = rateMeatCow();
		}
		else {
			rating = 0;
			System.out.println("ERROR No such use: "+use);
		}
		return "This is a "+rating2string(rating)+" cow for "+use+".";
	}

	private int rateDairyCow()
	{
		//TODO
		return 0;
	}

	private int rateMeatCow()
	{
		//TODO
		return 0;
	}

	private int ratePlowCow()
	{
		//TODO
		return 0;
	}

	/*
	 * Return a string rating for values between 0 and 20.
	 */
	private String rating2string(int rating)
	{
		if (rating < 1) return "useless";
		else if (rating < 3) return "terrible";
		else if (rating < 6) return "pretty bad";
		else if (rating < 9) return "moderate";
		else if (rating < 12) return "passable";
		else if (rating < 15) return "fairly good";
		else if (rating < 18) return "pretty good";
		else if (rating < 20) return "very good";
		else return "brilliant";
	}

	private String makeName(String sex, int id)
	{
		if (sex == "male") return "Bull #"+id;
		else if (sex == "female") return "Cow #"+id;
		else return "ERROR No such sex: "+sex;
	}

	private int combineTrait(int fathertrait, int mothertrait)
	{
		int newtrait = (int) Math.round((fathertrait+mothertrait)/2);
		int quartertrait = (int) Math.round(newtrait*0.25);
		int randomfactor = Herd.random.nextInt(quartertrait*2) - quartertrait;
		return newtrait+randomfactor;
	}

	public static void resetCounters()
	{
		idCounter = 1;
		maxGen = 1;
	}	
}