/** * 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 is a singleton class that holds the current herd. */ import java.util.ArrayList; import java.util.Random; public class Herd { public static int initialSize = 4; private static Herd singleton; //allow other classes to access the RNG public static Random random; private ArrayList<Cow> herd; private Cow[] parents; private Cow[] offspring; private Herd() { herd = new ArrayList<Cow>(); parents = new Cow[] {null, null}; offspring = new Cow[] {null, null}; for (int i = 0; i < initialSize; i++) { String sex = "male"; if ((i % 2) == 0) sex = "female"; herd.add(new Cow(sex)); } } public static Herd getInstance() { if (singleton == null) { random = new Random(); singleton = new Herd(); } return singleton; } public Cow getCow(int id) { for (int c = 0; c < herd.size(); c++) { if (herd.get(c).getID() == id) return herd.get(c); } return null; } public void removeCow(int id) { Cow c = getCow(id); if (c != null) herd.remove(c); } public void setParent(int id) { Cow p = getCow(id); if (p == null) return; if (p.getSex() == "male") parents[0] = p; else if (p.getSex() == "female") parents[1] = p; } public void removeParent(int id) { if (parents[0] != null && parents[0].getID() == id) parents[0] = null; else if (parents[1] != null && parents[1].getID() == id) parents[1] = null; } public String checkBreeding() { //TODO return "OK"; } public void breed() { //TODO //randomly generate either one or two offspring //breed the parents to create the offspring } public void keepChild(int id) { int o = -1; if (offspring[0] != null && offspring[0].getID() == id) o = 0; else if (offspring[1] != null && offspring[1].getID() == id) o = 1; if (o >= 0) { herd.add(offspring[o]); offspring[o] = null; } } public void discardChild(int id) { int o = -1; if (offspring[0] != null && offspring[0].getID() == id) o = 0; else if (offspring[1] != null && offspring[1].getID() == id) o = 1; if (o >= 0) offspring[o] = null; } public void saveHerd(String filename) { //TODO save to binary file } public void loadHerd(String filename) { //TODO load from binary file } public void resetHerd() { singleton = null; } }