// Turn Noble bunnies into White Walkers bunnies. private static void WhiteWalkerInfection(Bunny[,] bunniesGrid) { /* We first creating a list of selected bunnies and only after we go over the entire grid we turn them into White Walker bunnies. * This is done in order to prevent a new infected bunny that turned into a White Walker bunny to infect more bunnies in its surronding. * New White Walker bunnies will only infect other bunnies at the next turn. */ LinkedList <Bunny> infectedNobleBunnies = new LinkedList <Bunny>(); // Go over the entire grid of bunnies to find the White Walker bunnies. for (int x = 0; x < bunniesGrid.GetLength(0); x++) { for (int y = 0; y < bunniesGrid.GetLength(1); y++) { if (bunniesGrid[x, y] != null && bunniesGrid[x, y].house == "White Walker") { // Search for a Noble bunny in its surrounding and add him into the infected bunnies list. Bunny randomNobleBunnyFromSurrounding = GetANobleBunnyFromSurrounding(x, y, bunniesGrid, infectedNobleBunnies); if (randomNobleBunnyFromSurrounding != null) { infectedNobleBunnies.AddLast(randomNobleBunnyFromSurrounding); } } } } // Turn the infected bunnies into White Walker bunnies only after we went over the entire grid. foreach (Bunny infectedBunny in infectedNobleBunnies) { infectedBunny.TurnToWhite(); } }
// Create a new baby bunny. private static void CreateABabyBunny(int motherX, int motherY, Bunny[,] bunniesGrid, Bunny fatherBunny, LinkedList <Bunny> bunnies) { Space emptySpace = GetAnEmptySpaceFromSurrounding(motherX, motherY, bunniesGrid); if (emptySpace != null) { string sex; if (new Random().NextDouble() < 0.5) { sex = "Male"; } else { sex = "Female"; } string color; string house; // 2% chance the bunny will be a white walker. if (new Random().NextDouble() < 0.98) { color = bunniesGrid[motherX, motherY].color; house = fatherBunny.house; } else { color = "White"; house = "White Walker"; } Bunny babyBunny = new Bunny(sex, color, 0, RandomString(10), house); bunniesGrid[emptySpace.x, emptySpace.y] = babyBunny; bunnies.AddLast(babyBunny); PrintANewbornBunny(babyBunny); } }
static void Main(string[] args) { Console.WriteLine("Here are the bunnies:"); LinkedList <Bunny> bunnies = new LinkedList <Bunny>(); Bunny[,] bunniesGrid = new Bunny[GridSize, GridSize]; // Inializing the list, creating 5 bunnies. for (int i = 0; i < 5; i++) { string sex; if (new Random().NextDouble() < 0.5) { sex = "Male"; } else { sex = "Female"; } string color; string house; // 2% chance the bunny will be a white walker. if (new Random().NextDouble() < 0.98) { color = Enum.GetName(typeof(Color), new Random().Next(6)); house = Enum.GetName(typeof(House), new Random().Next(4)); } else { color = "White"; house = "White Walker"; } Bunny bunny = new Bunny(sex, color, 0, RandomString(10), house);; bunnies.AddLast(bunny); PlaceBunnyOnGrid(bunniesGrid, bunny); PrintANewbornBunny(bunny); } PrintBunniesGrid(bunniesGrid); /* Listen for click events to move forward to the next turn. * Terminate the program when all the bunnies have died. */ Console.WriteLine("Press any key for the next turn. Press ESC to stop."); while (Console.ReadKey(true).Key != ConsoleKey.Escape && bunnies.Count > 0) { if (!Console.KeyAvailable) { NextTurn(bunnies); } } ; }
// Place a bunny on the grid in a random empty space. private static void PlaceBunnyOnGrid(Bunny[,] bunniesGrid, Bunny bunny) { int radomBunnyX = new Random().Next(GridSize); int radomBunnyY = new Random().Next(GridSize); while (bunniesGrid[radomBunnyX, radomBunnyY] != null) { radomBunnyX = new Random().Next(GridSize); radomBunnyY = new Random().Next(GridSize); } bunniesGrid[radomBunnyX, radomBunnyY] = bunny; }
// Kill exactly half the bunnies population randomly. private static void LongHardWinter(LinkedList <Bunny> bunnies) { // Marking the half population count. int halfBunniesPopulation = bunnies.Count / 2; // Kill bunnies until we reach the half population count. while (bunnies.Count > halfBunniesPopulation) { Bunny randomBunny = GetARandomBunny(bunnies); if (randomBunny != null) { bunnies.Remove(randomBunny); PrintADeadBunny(randomBunny); } } }
// Print a message for each dead bunny. private static void PrintADeadBunny(Bunny bunny) { if (bunny.house != "White Walker") { string sigil; string saying; switch (bunny.house) { case "Stark": sigil = "Wolf"; saying = "Winter Is Coming!"; break; case "Baratheon": sigil = "Stag"; saying = "Ours Is The Furry!"; break; case "Lannister": sigil = "Lion"; saying = "Hear Me Roar!"; break; case "Targaryen": sigil = "Dragon"; saying = "Fire And Blood!"; break; default: sigil = ""; saying = ""; break; } if (bunny.sex == "Male") { Console.WriteLine("Lord {0} of bunny house {1} died at age {2}! He was a fierce {3}! {4}", bunny.name, bunny.house, bunny.age, sigil, saying); } else { Console.WriteLine("Lady {0} of bunny house {1} died at age {2}! She was a fierce {3}! {4}", bunny.name, bunny.house, bunny.age, sigil, saying); } } else { Console.WriteLine("White Walker {0} died at age {1}!", bunny.name, bunny.age); } }
// Print a message for each newborn bunny. private static void PrintANewbornBunny(Bunny bunny) { if (bunny.house != "White Walker") { if (bunny.sex == "Male") { Console.WriteLine("Lord {0} of bunny house {1} of color {2} was born!", bunny.name, bunny.house, bunny.color); } else { Console.WriteLine("Lady {0} of bunny house {1} of color {2} was born!", bunny.name, bunny.house, bunny.color); } } else { Console.WriteLine("White Walker bunny {0} was born!", bunny.name); } }
// Move the bunnies on the grid to a new random space. private static Bunny[,] MoveBunniesOnGrid(LinkedList <Bunny> bunnies) { Bunny[,] bunniesGrid = new Bunny[GridSize, GridSize]; foreach (Bunny bunny in bunnies) { // Check if there is enough space on the grid before we place another bunny. if (bunnies.Count <= GridSize * GridSize) { PlaceBunnyOnGrid(bunniesGrid, bunny); } } // In case the number of bunnies is greater than the number of cells in the grid. if (bunnies.Count > GridSize * GridSize) { Console.WriteLine("Not enough space on the grid!"); } return(bunniesGrid); }
// Performing a turn and making modifications. private static void NextTurn(LinkedList <Bunny> bunnies) { LinkedList <Bunny> deadBunnies = new LinkedList <Bunny>(); // First we age all the bunnies in a unique loop, because we need to age them before we start the breeding. foreach (Bunny bunny in bunnies) { bunny.age++; /* A bunny dies when he becomes older than 10 years old. * A White Walker bunny dies when he becomes 50 years old. */ if ((bunny.house != "White Walker" && bunny.age > 10) || (bunny.house == "White Walker" && bunny.age >= 50)) { deadBunnies.AddLast(bunny); } } // Removing the dead bunnies from the list (needs to happen before breeding, to prevent a dead father). foreach (Bunny deadBunny in deadBunnies) { bunnies.Remove(deadBunny); PrintADeadBunny(deadBunny); } // If the bunny population exceeds 1000, kill half the population randomly. if (bunnies.Count > 1000) { LongHardWinter(bunnies); } // Move the bunnies one space each turn randomly. Bunny[,] bunniesGrid = MoveBunniesOnGrid(bunnies); /* Go over the entire grid of bunnies and make changes. * We go over the grid and not the linked list, because we need to keep track of mother bunny index. */ for (int x = 0; x < bunniesGrid.GetLength(0); x++) { for (int y = 0; y < bunniesGrid.GetLength(1); y++) { Bunny bunny = bunniesGrid[x, y]; if (bunny != null) { // Mating a female bunny with a male bunny. if (bunny.house != "White Walker" && bunny.sex == "Female" && bunny.age >= 2) { Bunny adultMaleBunny = FindAnAdultMale(bunnies); if (adultMaleBunny != null) { // Create a new baby bunny. CreateABabyBunny(x, y, bunniesGrid, adultMaleBunny, bunnies); } } } } } /* Start the infection of the White Walker bunnies. (Infection includes newborn bunnies). * Infection starts only after the bunnies moved to their new space. * Infection does not include new infected bunnies that turned into White Walkers bunnies this turn, * i.e. a Noble bunny that got infected and turned into a White Walker bunny this turn, will only infect bunnies at the next turn. */ WhiteWalkerInfection(bunniesGrid); // Print the new grid. PrintBunniesGrid(bunniesGrid); Console.WriteLine("Press any key for next turn. Press ESC to stop."); }