Exemplo n.º 1
0
        // 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();
            }
        }
Exemplo n.º 2
0
        // Save current game state into a json file.
        public static void SaveGame(LinkedList <Bunny> bunnies, Bunny[,] bunniesGrid)
        {
            // Open file stream.
            using (StreamWriter file = File.CreateText(@"C:\Users\itiel\Desktop\BunnyWorldSavedGame.json"))
            {
                JsonSerializer serializer = new JsonSerializer();
                // Serialize the data directly into file stream.
                serializer.Serialize(file, bunniesGrid);
            }

            Console.WriteLine("Game saved to file (C:/Users/itiel/Desktop/BunnyWorldSavedGame.json).");

            if (AutoTurns)
            {
                Console.WriteLine("Press any key to continue the game. Press ESC to exit.");
                if (Console.ReadKey(true).Key != ConsoleKey.Escape)
                {
                    NextTurnController(bunnies, bunniesGrid);
                }
            }
            else
            {
                NextTurnController(bunnies, bunniesGrid);
            }
        }
Exemplo n.º 3
0
        // 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);
            }
        }
Exemplo n.º 4
0
        // 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);
        }
Exemplo n.º 5
0
        // 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);
        }
Exemplo n.º 6
0
        // 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);
            }
        }
Exemplo n.º 7
0
        // 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;
        }
Exemplo n.º 8
0
        // Move the dragon in a unique pattern (∞) on the grid.
        private static void MoveDragonOnGrid(Bunny[,] bunniesGrid)
        {
            Dragon dragon = (Dragon)bunniesGrid[dragonX, dragonY];

            Array.Clear(bunniesGrid, 0, bunniesGrid.Length);

            switch (dragonX)
            {
            case 2 when dragonY == 2:
                dragon.Direction = 0;
                break;

            case GridSize - 3 when dragonY == GridSize - 3:
                dragon.Direction = 1;
                break;

            case 2 when dragonY == GridSize - 3:
                dragon.Direction = 2;
                break;

            case GridSize - 3 when dragonY == 2:
                dragon.Direction = 3;
                break;
            }


            switch (dragon.Direction)
            {
            case 0:
                dragonX++;
                dragonY++;
                break;

            case 1:
                dragonX--;
                break;

            case 2:
                dragonX++;
                dragonY--;
                break;

            case 3:
                dragonX--;
                break;
            }

            bunniesGrid[dragonX, dragonY] = dragon;
        }
Exemplo n.º 9
0
        // Move the bunnies on the grid to a new random space.
        private static void MoveBunniesOnGrid(LinkedList <Bunny> bunnies, Bunny[,] bunniesGrid)
        {
            foreach (Bunny bunny in bunnies)
            {
                // Check if there is enough space on the grid before we place another bunny.
                if (!(bunny is Dragon) && 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!");
            }
        }
Exemplo n.º 10
0
        /* Go automatically (control flag in declaration) or listen for click events to move forward to the next turn, save or load game.
         * Terminate the program when all the bunnies have died.
         */
        private static void NextTurnController(LinkedList <Bunny> bunnies, Bunny[,] bunniesGrid)
        {
            if (AutoTurns)
            {
                while (!Console.KeyAvailable)
                {
                    NextTurn(bunnies, bunniesGrid);
                }
                switch (Console.ReadKey(true).Key)
                {
                case ConsoleKey.S:
                    SaveGame(bunnies, bunniesGrid);
                    break;

                case ConsoleKey.L:
                    LoadSavedGame(bunnies, bunniesGrid);
                    break;
                }
            }
            else
            {
                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.");
                while (Console.ReadKey(true).Key != ConsoleKey.Escape && bunnies.Count > 0)
                {
                    if (!Console.KeyAvailable)
                    {
                        NextTurn(bunnies, bunniesGrid);
                    }
                }
                ;
                switch (Console.ReadKey(true).Key)
                {
                case ConsoleKey.S:
                    SaveGame(bunnies, bunniesGrid);
                    break;

                case ConsoleKey.L:
                    LoadSavedGame(bunnies, bunniesGrid);
                    break;
                }
            }
        }
Exemplo n.º 11
0
 // Kill all bunnies in a certain range.
 private static void DragonAttack(LinkedList <Bunny> bunnies, Bunny[,] bunniesGrid)
 {
     for (int x = dragonX - 2; x < dragonX + 3; x++)
     {
         for (int y = dragonY - 2; y < dragonY + 3; y++)
         {
             if (bunniesGrid[x, y] != null && !(bunniesGrid[x, y] is Dragon))
             {
                 if (bunniesGrid[x, y] is Targaryen)
                 {
                     ((Targaryen)bunniesGrid[x, y]).Invincible = true;
                 }
                 else
                 {
                     KillBunnyAtIndex(x, y, bunniesGrid, bunnies);
                 }
             }
         }
     }
 }
Exemplo n.º 12
0
        // Print the grid of bunnies.
        private static void PrintBunniesGrid(Bunny[,] bunniesGrid)
        {
            for (int x = 0; x < bunniesGrid.GetLength(0); x++)
            {
                for (int y = 0; y < bunniesGrid.GetLength(1); y++)
                {
                    if (bunniesGrid[x, y] != null)
                    {
                        ChangeConsoleColor(bunniesGrid[x, y].color);
                        switch (bunniesGrid[x, y].house)
                        {
                        case "Stark":
                            Console.Write((bunniesGrid[x, y].age >= 2) ? "S " : "s ");
                            break;

                        case "Baratheon":
                            Console.Write((bunniesGrid[x, y].age >= 2) ? "B " : "b ");
                            break;

                        case "Lannister":
                            Console.Write((bunniesGrid[x, y].age >= 2) ? "L " : "l ");
                            break;

                        case "Targaryen":
                            Console.Write((bunniesGrid[x, y].age >= 2) ? "T " : "t ");
                            break;

                        case "White Walker":
                            Console.Write("W ");
                            break;
                        }
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.Write("  ");
                    }
                }
                Console.WriteLine();
            }
        }
Exemplo n.º 13
0
 // Print the grid of bunnies.
 private static void PrintBunniesGrid(Bunny[,] bunniesGrid)
 {
     if (AutoTurns)
     {
         Console.Clear();
     }
     for (int x = 0; x < bunniesGrid.GetLength(0); x++)
     {
         for (int y = 0; y < bunniesGrid.GetLength(1); y++)
         {
             if (bunniesGrid[x, y] != null)
             {
                 ChangeConsoleColor(bunniesGrid[x, y].color);
                 bunniesGrid[x, y].PrintOnGrid();
                 Console.ResetColor();
             }
             else
             {
                 Console.Write("  ");
             }
         }
         Console.WriteLine();
     }
 }
Exemplo n.º 14
0
        // Get a random empty space from surrounding of a mother bunny for a new baby bunny.
        private static Space GetAnEmptySpaceFromSurrounding(int motherX, int motherY, Bunny[,] bunniesGrid)
        {
            // Creating a list of empty spaces in surrounding of a mother bunny.
            LinkedList <Space> emptySpaces = new LinkedList <Space>();

            // Determining the range for searching, to prevent edge cases (out if index).
            int minXRange, maxXRange, minYRange, maxYRange;

            // X axis range.
            if (motherX == 0)
            {
                minXRange = 0;
                maxXRange = 2;
            }
            else if (motherX == bunniesGrid.GetLength(0) - 1)
            {
                minXRange = bunniesGrid.GetLength(0) - 2;
                maxXRange = bunniesGrid.GetLength(0);
            }
            else
            {
                minXRange = motherX - 1;
                maxXRange = motherX + 2;
            }

            // Y axis range.
            if (motherY == 0)
            {
                minYRange = 0;
                maxYRange = 2;
            }
            else if (motherY == bunniesGrid.GetLength(1) - 1)
            {
                minYRange = bunniesGrid.GetLength(1) - 2;
                maxYRange = bunniesGrid.GetLength(1);
            }
            else
            {
                minYRange = motherY - 1;
                maxYRange = motherY + 2;
            }

            // Start searching for empty spaces in compliance with range boundaries.
            for (int x = minXRange; x < maxXRange; x++)
            {
                for (int y = minYRange; y < maxYRange; y++)
                {
                    if (bunniesGrid[x, y] == null)
                    {
                        emptySpaces.AddLast(new Space(x, y));
                    }
                }
            }

            // Get a random empty space from the found empty spaces and return it.
            if (emptySpaces.Count > 0)
            {
                // Creating a random index to get a randomized space.
                int selectedSpaceIndex = new Random().Next(emptySpaces.Count);
                foreach (Space emptySpace in emptySpaces)
                {
                    if (selectedSpaceIndex == 0)
                    {
                        return(emptySpace);
                    }
                    // Downgrading the index until we find the randomized space.
                    selectedSpaceIndex--;
                }
            }
            return(null);
        }
Exemplo n.º 15
0
        // Get a random Noble bunny from surrounding of a White Walker bunny.
        private static Bunny GetANobleBunnyFromSurrounding(int whiteX, int whiteY, Bunny[,] bunniesGrid, LinkedList <Bunny> infectedNobleBunnies)
        {
            // Creating a list of Noble bunnies in surrounding of a White Walker bunny.
            LinkedList <Bunny> nobleBunnies = new LinkedList <Bunny>();

            // Determining the range for searching, to prevent edge cases (out if index).
            int minXRange, maxXRange, minYRange, maxYRange;

            // X axis range.
            if (whiteX == 0)
            {
                minXRange = 0;
                maxXRange = 2;
            }
            else if (whiteX == bunniesGrid.GetLength(0) - 1)
            {
                minXRange = bunniesGrid.GetLength(0) - 2;
                maxXRange = bunniesGrid.GetLength(0);
            }
            else
            {
                minXRange = whiteX - 1;
                maxXRange = whiteX + 2;
            }

            // Y axis range.
            if (whiteY == 0)
            {
                minYRange = 0;
                maxYRange = 2;
            }
            else if (whiteY == bunniesGrid.GetLength(1) - 1)
            {
                minYRange = bunniesGrid.GetLength(1) - 2;
                maxYRange = bunniesGrid.GetLength(1);
            }
            else
            {
                minYRange = whiteY - 1;
                maxYRange = whiteY + 2;
            }

            // Start searching for noble bunnies in compliance with range boundaries.
            for (int x = minXRange; x < maxXRange; x++)
            {
                for (int y = minYRange; y < maxYRange; y++)
                {
                    /* Check to prevent empty spaces, White Walker bunnies and already chosen Noble bunnies to be infected.
                     * (A Noble bunny can be selected for infection by only one White Walker bunny).
                     */
                    if (bunniesGrid[x, y] != null && bunniesGrid[x, y].house != "White Walker" && !infectedNobleBunnies.Contains(bunniesGrid[x, y]))
                    {
                        nobleBunnies.AddLast(bunniesGrid[x, y]);
                    }
                }
            }

            // Get a random Noble bunny from the found Noble bunnies and return it.
            if (nobleBunnies.Count > 0)
            {
                return(GetARandomBunny(nobleBunnies));
            }
            return(null);
        }
Exemplo n.º 16
0
        // 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;
                        }
                    }
                }
            }
        }
Exemplo n.º 17
0
        /* Get a random space from surrounding of a given bunny.
         * Space returned is either empty or occupied depending on the sent flag.
         * This function is useful for finding an empty space for a newborn bunny, and for finding a surrounding space of a bunny to attack.
         */
        private static Space GetASpaceFromSurrounding(int bunnyX, int bunnyY, Bunny[,] bunniesGrid, bool shouldBeEmpty)
        {
            // Creating a list of spaces in surrounding of a given bunny.
            LinkedList <Space> spaces = new LinkedList <Space>();

            // Determining the range for searching, to prevent edge cases (out if index).
            int minXRange, maxXRange, minYRange, maxYRange;

            // X axis range.
            if (bunnyX == 0)
            {
                minXRange = 0;
                maxXRange = 2;
            }
            else if (bunnyX == bunniesGrid.GetLength(0) - 1)
            {
                minXRange = bunniesGrid.GetLength(0) - 2;
                maxXRange = bunniesGrid.GetLength(0);
            }
            else
            {
                minXRange = bunnyX - 1;
                maxXRange = bunnyX + 2;
            }

            // Y axis range.
            if (bunnyY == 0)
            {
                minYRange = 0;
                maxYRange = 2;
            }
            else if (bunnyY == bunniesGrid.GetLength(1) - 1)
            {
                minYRange = bunniesGrid.GetLength(1) - 2;
                maxYRange = bunniesGrid.GetLength(1);
            }
            else
            {
                minYRange = bunnyY - 1;
                maxYRange = bunnyY + 2;
            }

            // Start searching for spaces in compliance with range boundaries.
            for (int x = minXRange; x < maxXRange; x++)
            {
                for (int y = minYRange; y < maxYRange; y++)
                {
                    if ((shouldBeEmpty && bunniesGrid[x, y] == null) ||
                        (!shouldBeEmpty && bunniesGrid[x, y] != null && bunniesGrid[x, y] != bunniesGrid[bunnyX, bunnyY]))
                    {
                        spaces.AddLast(new Space(x, y));
                    }
                }
            }

            // Get a random space from the found spaces and return it.
            if (spaces.Count > 0)
            {
                // Creating a random index to get a randomized space.
                int selectedSpaceIndex = new Random().Next(spaces.Count);
                foreach (Space space in spaces)
                {
                    if (selectedSpaceIndex == 0)
                    {
                        return(space);
                    }
                    // Downgrading the index until we find the randomized space.
                    selectedSpaceIndex--;
                }
            }
            return(null);
        }
Exemplo n.º 18
0
        // 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.");
            }
        }
Exemplo n.º 19
0
        // 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.");
        }