Beispiel #1
0
        /// <summary>
        /// Start fighting when both players are ready. Instructions will be given to fight and move players on the screen
        /// </summary>
        /// <param name="playerOne">PlayerOne class that implements IPlayer, IAttacker, IBadge interfaces</param>
        /// <param name="playerTwo">PlayerTwo class that implements IPlayer, IDefender interfaces</param>
        public static void StartFighting(PlayerOne playerOne, PlayerTwo playerTwo)
        {
            Console.WriteLine("\n Keyboard controller  A: Punch, S: Kick, E: Eat, K: Take Cover, Right Arrow: Walk, Up Arrow: Run \n");
            while (playerTwo.Health > 0)
            {
                // Building a string to show the progress of the fight on an underscored line '_'
                string        firstPart     = $"{playerOne.CharacterName} (Rank: {playerOne.Rank}) FIGHTING STAGE[";
                StringBuilder fightingStage = new StringBuilder("________________________________");
                string        secondPart    = $"] :{playerTwo.CharacterName}'s Health is {playerTwo.Health}";

                // When moving and fighting, the underscores are replaced with Players initials
                fightingStage[playerOne.Position] = playerOne.CharacterName.First();
                fightingStage[playerTwo.Position] = playerTwo.CharacterName.First();

                // Checking if player one has made it to the end of the line. Checking last or before last underscore of the stage are occupied to end the game
                if (fightingStage[fightingStage.Length - 1] != '_' || fightingStage[fightingStage.Length - 2] != '_')
                {
                    break;
                }

                Console.WriteLine(firstPart + fightingStage + secondPart);

                // Reading user's keyboard click
                ConsoleKey key = Console.ReadKey().Key;

                // Cases for user input, each with different action
                switch (key)
                {
                case ConsoleKey.A:
                    Console.WriteLine("\n\n Punch BOOM!!! ");
                    playerOne.Punch(playerTwo);
                    playerOne.Walk();
                    playerTwo.Walk();
                    break;

                case ConsoleKey.S:
                    Console.WriteLine("\n\n Kick YEAO!!! ");
                    playerOne.Kick(playerTwo);
                    playerOne.Walk();
                    playerTwo.Walk();
                    break;

                case ConsoleKey.E:
                    Console.WriteLine("\n\n Yum yum :) You can't kill me that easy");
                    playerTwo.Eat();
                    break;

                case ConsoleKey.K:
                    Console.WriteLine($"\n\n {playerTwo.CharacterName} is now under cover!");
                    playerTwo.TakeCoverFrom(playerOne);
                    break;

                case ConsoleKey.RightArrow:
                    if (fightingStage[fightingStage.Length - 1] == '_')
                    {
                        playerOne.Walk();
                        playerTwo.Walk();
                    }
                    break;

                case ConsoleKey.UpArrow:
                    playerOne.Run();
                    playerTwo.Run();
                    break;
                }
            }

            // Game is finished. Rank up the first player and declare them as the winner!
            playerOne.Rank = playerOne.RankUp(playerOne.Rank);
            Console.WriteLine($"\n {playerOne.CharacterName} is the WINNER! Rank upgraded to {playerOne.Rank}\n");
        }