/// <summary>
        /// Process a round in a battle with two given BattleDeck objects.
        /// Round Winner calculation uses Discards and when clause introduced in C#7.0 and switch expression introduced in C#8.0.
        /// Ref when clause: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch#the-case-statement-and-the-when-clause
        /// Ref Discards: https://docs.microsoft.com/en-us/dotnet/csharp/discards
        /// </summary>
        /// <param name="battleDeckP1"></param>
        /// <param name="battleDeckP2"></param>
        private void ProcessRound(string player1Name, string player2Name, ref BattleDeck battleDeckP1, ref BattleDeck battleDeckP2, ref BattleLog log)
        {
            Card cardP1 = battleDeckP1.GetRandomCard();
            Card cardP2 = battleDeckP2.GetRandomCard();

            //generate the accuracy of the next attack (max 1.7, min 0.3
            _accuracy = new Random().NextDouble() * (1.7 - 0.3) + 0.3;

            BattleResult roundWinner = cardP1 switch
            {
                ISpell _ when cardP2 is ISpell => FightSpellVsSpell(cardP1, cardP2),
                IMonster _ when cardP2 is ISpell => FightMonsterVsSpell(cardP1, cardP2),
                ISpell _ when cardP2 is IMonster => FightMonsterVsSpell(cardP2, cardP1, true),
                _ => FightMonsterVsMonster(cardP1, cardP2)
            };

            if (roundWinner == BattleResult.Player1Wins)
            {
                battleDeckP2.RemoveCard(cardP2);
                battleDeckP1.AddCard(cardP2);
                log.Rounds.Add(
                    $"{cardP1.Name} ({cardP1.GetType().Name}, {player1Name}) won with damage {cardP1.Damage} and element {Enum.GetName(typeof(ElementType), cardP1.Element)} " +
                    $"against {cardP2.Name} ({cardP2.GetType().Name}, {player2Name}) with damage {cardP2.Damage} and element {Enum.GetName(typeof(ElementType), cardP2.Element)}. " +
                    $"The accuracy of the attack was {Math.Round(_accuracy, 3)}");
            }
            else
            {
                battleDeckP1.RemoveCard(cardP1);
                battleDeckP2.AddCard(cardP1);
                log.Rounds.Add(
                    $"{cardP1.Name} ({cardP1.GetType().Name}, {player1Name}) lost with damage {cardP1.Damage} and element {Enum.GetName(typeof(ElementType), cardP1.Element)} " +
                    $"against {cardP2.Name} ({cardP2.GetType().Name}, {player2Name}) with damage {cardP2.Damage} and element {Enum.GetName(typeof(ElementType), cardP2.Element)}. " +
                    $"The accuracy of the attack was {Math.Round(_accuracy, 3)}");
            }
        }
Example #2
0
        public static BattleDeck GenerateBattleDeck(User user)
        {
            DummyCard[] dummyDeck = CardsUsersDatabaseHandler.GetDummyDeck(user);
            BattleDeck  deck      = new BattleDeck();

            foreach (DummyCard card in dummyDeck)
            {
                deck.AddCard(DummyCardConverter.Convert(card));
            }

            return(deck);
        }