static void Main() { LinkedList <Bunny> bunnies = new LinkedList <Bunny>(); Bunny[,] bunniesGrid = new Bunny[GridSize, GridSize]; // Place a dragon on the grid. (Using the Bunny class). The dragon will be at least 2 spaces from the edges. dragonX = new Random().Next(2, GridSize - 2); dragonY = dragonX; Dragon dragon = new Dragon(RandomString(10)); dragon.Direction = 0; bunnies.AddLast(dragon); bunniesGrid[dragonX, dragonY] = dragon; // Inializing the list, creating 8 bunnies. One male and one female for each Noble house. for (int i = 0; i < 8; i++) { string sex; if (i % 2 == 0) { sex = "Male"; } else { sex = "Female"; } string color = Enum.GetName(typeof(Color), new Random().Next(6)); Bunny bunny = null; switch (i / 2) { case 0: bunny = new Stark(sex, color, 0, RandomString(10)); break; case 1: bunny = new Baratheon(sex, color, 0, RandomString(10)); break; case 2: bunny = new Lannister(sex, color, 0, RandomString(10)); break; case 3: bunny = new Targaryen(sex, color, 0, RandomString(10)); break; } bunnies.AddLast(bunny); PlaceBunnyOnGrid(bunniesGrid, bunny); PrintANewbornBunny(bunny); } PrintBunniesGrid(bunniesGrid); NextTurnController(bunnies, bunniesGrid); }
// Kill a bunny at a given index. private static void KillBunnyAtIndex(int x, int y, Bunny[,] bunniesGrid, LinkedList <Bunny> bunnies) { Bunny bunny = bunniesGrid[x, y]; bunniesGrid[x, y] = null; bunnies.Remove(bunny); PrintADeadBunny(bunny); }
// Load a saved game state from a json file. public static void LoadSavedGame(LinkedList <Bunny> bunnies, Bunny[, ] bunniesGrid) { // Clear current game data. bunnies.Clear(); Array.Clear(bunniesGrid, 0, bunniesGrid.Length); // Deserialize the data from the json file. string json = File.ReadAllText(@"C:\Users\itiel\Desktop\BunnyWorldSavedGame.json"); Bunny[,] savedBunniesGrid = JsonConvert.DeserializeObject <Bunny[, ]>(json); for (int x = 0; x < savedBunniesGrid.GetLength(0); x++) { for (int y = 0; y < savedBunniesGrid.GetLength(1); y++) { if (savedBunniesGrid[x, y] != null) { Bunny bunny = null; switch (savedBunniesGrid[x, y].HouseName) { case "Stark": bunny = new Stark(savedBunniesGrid[x, y].sex, savedBunniesGrid[x, y].color, savedBunniesGrid[x, y].age, savedBunniesGrid[x, y].name); break; case "Baratheon": bunny = new Baratheon(savedBunniesGrid[x, y].sex, savedBunniesGrid[x, y].color, savedBunniesGrid[x, y].age, savedBunniesGrid[x, y].name); break; case "Lannister": bunny = new Lannister(savedBunniesGrid[x, y].sex, savedBunniesGrid[x, y].color, savedBunniesGrid[x, y].age, savedBunniesGrid[x, y].name); break; case "Targaryen": bunny = new Targaryen(savedBunniesGrid[x, y].sex, savedBunniesGrid[x, y].color, savedBunniesGrid[x, y].age, savedBunniesGrid[x, y].name); ((Targaryen)bunny).Invincible = savedBunniesGrid[x, y].Invincible; break; case "White Walker": bunny = new WhiteWalker(savedBunniesGrid[x, y].age, savedBunniesGrid[x, y].name); break; case "Dragon": bunny = new Dragon(savedBunniesGrid[x, y].name); dragonX = x; dragonY = y; ((Dragon)bunny).Direction = savedBunniesGrid[x, y].Direction; break; } bunnies.AddLast(bunny); bunniesGrid[x, y] = bunny; } } } PrintBunniesGrid(bunniesGrid); NextTurnController(bunnies, bunniesGrid); }
// Create a new baby bunny. private static void CreateABabyBunny(int motherX, int motherY, Bunny[,] bunniesGrid, Bunny fatherBunny, LinkedList <Bunny> bunnies) { Space emptySpace = GetASpaceFromSurrounding(motherX, motherY, bunniesGrid, true); if (emptySpace != null) { string sex; if (new Random().NextDouble() < 0.5) { sex = "Male"; } else { sex = "Female"; } Bunny babyBunny = null; string color; // 2% chance the bunny will be a white walker. if (new Random().NextDouble() < 0.98) { color = bunniesGrid[motherX, motherY].color; switch (fatherBunny.HouseName) { case "Stark": babyBunny = new Stark(sex, color, 0, RandomString(10)); break; case "Baratheon": babyBunny = new Baratheon(sex, color, 0, RandomString(10)); break; case "Lannister": babyBunny = new Lannister(sex, color, 0, RandomString(10)); break; case "Targaryen": babyBunny = new Targaryen(sex, color, 0, RandomString(10)); break; } } else { babyBunny = new WhiteWalker(0, RandomString(10)); } bunniesGrid[emptySpace.x, emptySpace.y] = babyBunny; bunnies.AddLast(babyBunny); PrintANewbornBunny(babyBunny); } }
// 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 && !(randomBunny is Dragon)) { bunnies.Remove(randomBunny); PrintADeadBunny(randomBunny); } } }
// Print a message for each dead bunny. private static void PrintADeadBunny(Bunny bunny) { if (!AutoTurns) { if (!(bunny is WhiteWalker)) { 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.HouseName, bunny.age, bunny.Sigil, bunny.Saying); } else { Console.WriteLine("Lady {0} of bunny house {1} died at age {2}! She was a fierce {3}! {4}", bunny.name, bunny.HouseName, bunny.age, bunny.Sigil, bunny.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 (!AutoTurns) { if (!(bunny is WhiteWalker)) { if (bunny.sex == "Male") { Console.WriteLine("Lord {0} of bunny house {1} of color {2} was born!", bunny.name, bunny.HouseName, bunny.color); } else { Console.WriteLine("Lady {0} of bunny house {1} of color {2} was born!", bunny.name, bunny.HouseName, bunny.color); } } else { Console.WriteLine("White Walker bunny {0} was born!", bunny.name); } } }
// Attack bunnies. private static void AttackBunnies(Bunny attackerBunny, int attackerX, int attackerY, Bunny[,] bunniesGrid, LinkedList <Bunny> bunnies) { // Get a space of a surrounding bunny to attack. Space victimBunnySpace = GetASpaceFromSurrounding(attackerX, attackerY, bunniesGrid, false); if (victimBunnySpace != null) { Bunny victimBunny = bunniesGrid[victimBunnySpace.x, victimBunnySpace.y]; // Do something only if they're not from the same house. (Skip if victim is a dragon). if (victimBunny.HouseName != attackerBunny.HouseName && !(victimBunny is Dragon)) { // A Targaryen affected by the dragon wins every attack, as attacker or defender. if (attackerBunny is Targaryen && ((Targaryen)attackerBunny).Invincible) { KillBunnyAtIndex(victimBunnySpace.x, victimBunnySpace.y, bunniesGrid, bunnies); } else if (victimBunny is Targaryen && ((Targaryen)victimBunny).Invincible) { KillBunnyAtIndex(attackerX, attackerY, bunniesGrid, bunnies); } else if (victimBunny is WhiteWalker) { // Turn the attacker bunny into a White Walker. bunniesGrid[attackerX, attackerY] = null; bunnies.Remove(attackerBunny); Bunny whiteWalker = new WhiteWalker(attackerBunny.age, attackerBunny.name); bunniesGrid[attackerX, attackerY] = whiteWalker; bunnies.AddLast(whiteWalker); if (!AutoTurns) { if (attackerBunny.sex == "Male") { Console.WriteLine("Lord {0} of bunny house {1} turned to a White! Kill him and burn his body!", attackerBunny.name, attackerBunny.HouseName); } else { Console.WriteLine("Lady {0} of bunny house {1} turned to a White! Kill her and burn her body!", attackerBunny.name, attackerBunny.HouseName); } } } else if (victimBunny.age < 2) { bunniesGrid[victimBunnySpace.x, victimBunnySpace.y] = null; bunnies.Remove(victimBunny); PrintADeadBunny(victimBunny); } // Attack on an adult female from another house. else if (victimBunny.sex == "Female") { // Kill the mother. bunniesGrid[victimBunnySpace.x, victimBunnySpace.y] = null; bunnies.Remove(victimBunny); PrintADeadBunny(victimBunny); // Create a new bastard baby bunny in the mother space. string sex; if (new Random().NextDouble() < 0.5) { sex = "Male"; } else { sex = "Female"; } Bunny babyBunny = null; string color; // 2% chance the bunny will be a white walker. if (new Random().NextDouble() < 0.98) { color = victimBunny.color; switch (attackerBunny.HouseName) { case "Stark": babyBunny = new Stark(sex, color, 0, RandomString(10)); break; case "Baratheon": babyBunny = new Baratheon(sex, color, 0, RandomString(10)); break; case "Lannister": babyBunny = new Lannister(sex, color, 0, RandomString(10)); break; case "Targaryen": babyBunny = new Targaryen(sex, color, 0, RandomString(10)); break; } } else { babyBunny = new WhiteWalker(0, RandomString(10)); } // Insert the newborn bastard instead of the mother. bunniesGrid[victimBunnySpace.x, victimBunnySpace.y] = babyBunny; bunnies.AddLast(babyBunny); PrintANewbornBunny(babyBunny); } else { switch (attackerBunny.HouseName) { case "Stark": if (victimBunny is Baratheon || victimBunny is Lannister) { KillBunnyAtIndex(attackerX, attackerY, bunniesGrid, bunnies); } else { KillBunnyAtIndex(victimBunnySpace.x, victimBunnySpace.y, bunniesGrid, bunnies); } break; case "Baratheon": if (victimBunny is Stark || victimBunny is Targaryen) { KillBunnyAtIndex(victimBunnySpace.x, victimBunnySpace.y, bunniesGrid, bunnies); } else { KillBunnyAtIndex(attackerX, attackerY, bunniesGrid, bunnies); } break; case "Lannister": if (victimBunny is Stark || victimBunny is Baratheon) { KillBunnyAtIndex(victimBunnySpace.x, victimBunnySpace.y, bunniesGrid, bunnies); } else { KillBunnyAtIndex(attackerX, attackerY, bunniesGrid, bunnies); } break; case "Targaryen": if (victimBunny is Stark || victimBunny is Baratheon) { KillBunnyAtIndex(attackerX, attackerY, bunniesGrid, bunnies); } else { KillBunnyAtIndex(victimBunnySpace.x, victimBunnySpace.y, bunniesGrid, bunnies); } break; } } } } }
// Performing a turn and making modifications. private static void NextTurn(LinkedList <Bunny> bunnies, Bunny[,] bunniesGrid) { 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 is Dragon) && ((!(bunny is WhiteWalker) && bunny.age > 10) || (bunny is WhiteWalker && 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 90% of the maximum number of possible bunnies, kill half the population randomly. if (bunnies.Count > 0.9 * GridSize * GridSize) { LongHardWinter(bunnies); } // Move the dragon in a unique pattern (∞) on the grid. MoveDragonOnGrid(bunniesGrid); // Move the bunnies one space each turn randomly. MoveBunniesOnGrid(bunnies, bunniesGrid); /* 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 is Dragon) && !(bunny is WhiteWalker) && bunny.sex == "Female" && bunny.age >= 2) { Bunny adultMaleBunny = FindAnAdultMale(bunnies, bunny.HouseName); if (adultMaleBunny != null) { // Create a new baby bunny. CreateABabyBunny(x, y, bunniesGrid, adultMaleBunny, bunnies); } } } } } // We go over the entire grid again, because attacks includes newborn bunnies. 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) { // Start an attack. if (!(bunny is Dragon) && !(bunny is WhiteWalker) && bunny.sex == "Male" && bunny.age >= 2) { AttackBunnies(bunny, x, y, bunniesGrid, bunnies); } } } } // Kill all bunnies in a certain range. DragonAttack(bunnies, bunniesGrid); // Print the new grid. PrintBunniesGrid(bunniesGrid); if (!AutoTurns) { Console.WriteLine("Press any key for the next turn. Press ESC to pause game, then S to save or L to load, Press Esc again to exit."); } }