/** * Run the simulation from its current state for a single step. Iterate * over the whole field updating the state of each fox and rabbit. */ public void SimulateOneStep() { step++; // Provide space for newborn rabbits. List <Rabbit> newRabbits = new List <Rabbit>(); // Let all rabbits act. for (int r = 0; r < rabbits.Count; r++) { Rabbit rabbit = rabbits[r]; rabbit.Run(); if (!rabbit.IsAlive()) { rabbits.Remove(rabbit); } else { rabbit.GiveBirth(newRabbits); } } // Provide space for newborn foxes. List <Fox> newFoxes = new List <Fox>(); // Let all foxes act. for (int f = 0; f < foxes.Count; f++) { Fox fox = foxes[f]; fox.Hunt(); if (!fox.IsAlive()) { foxes.Remove(fox); } else { fox.GiveBirth(newFoxes); } } // Add the newly born foxes and rabbits to the main lists. rabbits.AddRange(newRabbits); foxes.AddRange(newFoxes); //view.ShowStatus(step, field); }
/** * Look for rabbits adjacent to the current location. * Only the first live rabbit is eaten. * @return Where food was found, or null if it wasn't. */ private Location FindFood() { List <Location> adjacent = field.AdjacentLocations(location); foreach (Location where in adjacent) { Object animal = field.GetAnimalAt(where); if (animal is Rabbit) { Rabbit rabbit = (Rabbit)animal; if (rabbit.IsAlive()) { rabbit.SetDead(); foodLevel = RABBIT_FOOD_VALUE; return(where); } } } return(null); }