public DetailsForm(int zebraId) { InitializeComponent(); z = Form1.farm.GetZebra(zebraId); if (z == null) { MessageBox.Show("No zebra found", "Error"); gbxZebra.Enabled = false; } else { gbxZebra.Text = "Zebra with id " + z.GetId(); tbxZebraName.Text = z.Name; switch (z.Gender) { case Gender.MARE: rbnZebraGenderMare.Checked = true; break; case Gender.STALLION: rbnZebraGenderStallion.Checked = true; break; case Gender.UNKNOWN: rbnZebraGenderUnknown.Checked = true; break; } } }
// Methods public Zebra AttemptToProduceOffpring(Zebra mate) { if (this.Gender == Gender.MARE && mate.Gender == Gender.STALLION) { string name = $"{this.name}-with-{mate.name}"; int randomVal = r.Next(0, 100); if (randomVal < 45) { return(new Zebra(name, Gender.MARE, this.id, mate.id)); } else if (randomVal >= 45 && randomVal < 45 * 2) { return(new Zebra(name, Gender.STALLION, this.id, mate.id)); } } return(null); }
public bool AttemptToBreed(int motherId, int fatherId) { Zebra mother = GetZebra(motherId); Zebra father = GetZebra(fatherId); if (mother != null && father != null) { Zebra baby = mother.AttemptToProduceOffpring(father); if (baby == null) { return(false); } else { zebras.Add(baby); return(true); } } else { throw new Exception("Invalid Zebra id"); } }