public int CalculateLeastAmountOfManaSpentWhenWinning(
            Dictionary <string, Effect> effects,
            FighterStats bossStats,
            bool hardDifficulty
            )
        {
            FighterStats playerStats = new FighterStats
            {
                HitPoints  = playerHitPoints,
                Damage     = 0,
                ManaPoints = playerManaPoints
            };

            int leastAmountOfManaSpentWhenWinning = int.MaxValue;

            FightPermutations(
                (FighterStats)playerStats.Clone(),
                (FighterStats)bossStats.Clone(),
                effects,
                new Dictionary <string, Effect>(),
                true,
                0,
                hardDifficulty,
                ref leastAmountOfManaSpentWhenWinning
                );

            return(leastAmountOfManaSpentWhenWinning);
        }
예제 #2
0
        public int Solution(string input)
        {
            Dictionary <string, Effect> effects = effectsRepository.GetEffects();
            FighterStats bossStats = bossStatsRepository.GetBossStats(input);

            int leastAmountOfMana = wizardSimulatorGame.CalculateLeastAmountOfManaSpentWhenWinning(
                effects,
                bossStats,
                true
                );

            return(leastAmountOfMana);
        }
        public FighterStats GetBossStats(string input)
        {
            Regex hitPointsRegex = new Regex(@"Hit Points: (\d+)");
            Match hitPointsMatch = hitPointsRegex.Match(input);
            int   hitPoints      = int.Parse(hitPointsMatch.Groups[1].Value);

            Regex damageRegex = new Regex(@"Damage: (\d+)");
            Match damageMatch = damageRegex.Match(input);
            int   damage      = int.Parse(damageMatch.Groups[1].Value);

            FighterStats bossStats = new FighterStats
            {
                HitPoints  = hitPoints,
                Damage     = damage,
                ManaPoints = 0
            };

            return(bossStats);
        }
        private void FightPermutations(
            FighterStats playerStats,
            FighterStats bossStats,
            Dictionary <string, Effect> effects,
            Dictionary <string, Effect> activeEffects,
            bool playerTurn,
            int manaSpent,
            bool hardDifficulty,
            ref int leastAmountOfManaSpentWhenWinning
            )
        {
            // If playing on hard difficulty
            if (hardDifficulty)
            {
                playerStats.HitPoints -= 1;
                if (playerStats.HitPoints <= 0)
                {
                    return;
                }
            }

            int playerArmor = 0;

            foreach (KeyValuePair <string, Effect> activeEffect in activeEffects)
            {
                if (activeEffect.Value.Damage > 0)
                {
                    bossStats.HitPoints -= activeEffect.Value.Damage;
                    // If player won
                    if (bossStats.HitPoints <= 0)
                    {
                        leastAmountOfManaSpentWhenWinning = manaSpent;
                        return;
                    }
                }

                if (activeEffect.Value.Armor > 0)
                {
                    playerArmor = activeEffect.Value.Armor;
                }

                if (activeEffect.Value.Heal > 0)
                {
                    playerStats.HitPoints += activeEffect.Value.Heal;
                }

                if (activeEffect.Value.ManaRecharge > 0)
                {
                    playerStats.ManaPoints += activeEffect.Value.ManaRecharge;
                }

                activeEffect.Value.Turns--;
                // If effect expired
                if (activeEffect.Value.Turns == 0)
                {
                    activeEffects.Remove(activeEffect.Key);
                }
            }

            // Player turn
            if (playerTurn)
            {
                foreach (KeyValuePair <string, Effect> effect in effects)
                {
                    // Already active effect cannot be casted
                    if (!activeEffects.ContainsKey(effect.Key))
                    {
                        // If player can afford to cast spell
                        if (playerStats.ManaPoints - effect.Value.ManaCost >= 0)
                        {
                            // If mana spent is less than least amount of mana spent when winning
                            if (manaSpent + effect.Value.ManaCost <= leastAmountOfManaSpentWhenWinning)
                            {
                                Dictionary <string, Effect> newActiveEffects = CloneEffects(activeEffects);
                                newActiveEffects.Add(effect.Key, (Effect)effect.Value.Clone());

                                FighterStats newPlayerStats = (FighterStats)playerStats.Clone();
                                newPlayerStats.ManaPoints -= effect.Value.ManaCost;

                                FightPermutations(
                                    newPlayerStats,
                                    (FighterStats)bossStats.Clone(),
                                    effects,
                                    newActiveEffects,
                                    false,
                                    manaSpent + effect.Value.ManaCost,
                                    hardDifficulty,
                                    ref leastAmountOfManaSpentWhenWinning
                                    );
                            }
                        }
                    }
                }
            }
            // Boss turn
            else
            {
                playerStats.HitPoints -= bossStats.Damage - playerArmor;
                // If boss won
                if (playerStats.HitPoints <= 0)
                {
                    return;
                }

                FightPermutations(
                    (FighterStats)playerStats.Clone(),
                    (FighterStats)bossStats.Clone(),
                    effects,
                    new Dictionary <string, Effect>(activeEffects),
                    true,
                    manaSpent,
                    hardDifficulty,
                    ref leastAmountOfManaSpentWhenWinning
                    );
            }
        }