Пример #1
0
        Dictionary <string, int> get_damage_expression_breakdown()
        {
            Dictionary <string, int> breakdown = new Dictionary <string, int>();

            foreach (CreaturePower power in fPowers)
            {
                DiceExpression exp = DiceExpression.Parse(power.Damage);
                if (exp == null)
                {
                    continue;
                }
                if (exp.Maximum == 0)
                {
                    continue;
                }

                string dmg = exp.ToString();

                if (!breakdown.ContainsKey(dmg))
                {
                    breakdown[dmg] = 0;
                }

                breakdown[dmg] += 1;
            }

            return(breakdown);
        }
Пример #2
0
        private void ExpressionBox_TextChanged(object sender, EventArgs e)
        {
            if (fUpdating)
            {
                return;
            }

            DiceExpression exp = DiceExpression.Parse(ExpressionBox.Text);

            if (exp != null)
            {
                fUpdating = true;

                ClearBtn_Click(sender, e);

                fConstant = exp.Constant;

                for (int n = 0; n != exp.Throws; ++n)
                {
                    add_die(exp.Sides);
                }

                fUpdating = false;
            }
        }
Пример #3
0
        private int ResolveDefense(Actor defender, int hits, StringBuilder attackMsg, StringBuilder defenseMsg)
        {
            int blocks = 0;

            if (hits > 0)
            {
                attackMsg.AppendFormat("Атака с рейтингом {0} попадания.", hits);
                defenseMsg.AppendFormat("   {0} защищается с рейтингом: ", defender.Name);

                DiceExpression defenseDice = new DiceExpression().Dice(defender.Defense, 100);
                DiceResult     defenseRoll = defenseDice.Roll();

                foreach (TermResult termResult in defenseRoll.Results)
                {
                    defenseMsg.Append(termResult.Value + ", ");

                    if (termResult.Value >= 100 - defender.DefenseChance)
                    {
                        blocks++;
                    }
                    defenseMsg.AppendFormat("{0}.", blocks);
                }
            }
            else
            {
                attackMsg.Append("и полностью промахивается.");
            }

            return(blocks);
        }
Пример #4
0
        private float ResolveDefence()
        {
            if (GetComponent <Statistic>(ComponentType.Stats).Energy < 3)
            {
                return(0f);
            }

            GetComponent <Statistic>(ComponentType.Stats).Energy -= 3;
            float          blockPercent = 0;
            DiceExpression blockDice    = new DiceExpression().Dice(Stats.Defence, 100);
            DiceResult     blockResult  = blockDice.Roll();

            foreach (TermResult termResult in blockResult.Results)
            {
                if (termResult.Value >= (100 - Stats.DefenceChance))
                {
                    blockPercent++;
                }
            }

            var bp = blockPercent / (float)Stats.Defence;

            if (bp == 1 && Stats.Defence >= 20)
            {
                return(1);
            }

            bp = bp * (float)0.80;

            System.Console.WriteLine("BlockPercent:{0}", bp);

            return(bp);
        }
Пример #5
0
        //check too see how many hits the attacker got on the defender and displays message
        private static int ResolveAttack(Actor attacker, Actor defender, StringBuilder attackMessage)
        {
            int hits = 0;

            //displays message of who is attacking which defender
            //attackMessage.AppendFormat("{0} attacks {1} and rolls: ", attacker.Name, defender.Name);

            //Roll a number 100-dice equal to the attack value of the attacking actor
            //gets the dice for that attacker
            DiceExpression attackDice = new DiceExpression().Dice(attacker.Attack, 100);
            //rolls the attackers dice and gets the dice result
            DiceResult attackResult = attackDice.Roll();

            //foreach dice roll in the attackers results
            foreach (TermResult termResult in attackResult.Results)
            {
                //appends the attack message adding the current attack dice value
                attackMessage.Append(termResult.Value + ", ");

                //if the current attack dice roll result was more than 100 - the attackers attack chance
                //it was a success and a hit is added
                if (termResult.Value >= 100 - attacker.AttackChance)
                {
                    hits++;
                }
            }

            return(hits);
        }
Пример #6
0
        // El atacante tira en función de sus estadísticas para ver si obtiene
        // algún resultado.
        private static int ResolveAttack( Actor attacker, Actor defender, StringBuilder attackMessage)
        {
            int hits = 0;

            attackMessage.AppendFormat("{0} ataca a {1} y tira: ", attacker.Name, defender.Name);

            // Rodamos un número de 100 dados de igual lados, que será igual al valor de
            // ataque del atacante {attacker}.
            DiceExpression attackDice = new DiceExpression().Dice(attacker.Attack, 100);
            DiceResult attackResult = attackDice.Roll();

            // Miramos el valor de la cara de cada dado que fue rodado.
            foreach ( TermResult termResult in attackResult.Results )
            {
                attackMessage.Append(termResult.Value + ", ");
                // Comparamos el valor a 100, menos la probabilidad de
                // ataque y añadimos el golpe si este es mayor.
                if ( termResult.Value >= 100 - attacker.AttackChance )
                {
                    hits += 1;
                }
            }

            return hits;
        }
Пример #7
0
        private static int ResolveDefense(Actor defender, int hits, StringBuilder attackMessage, StringBuilder defenseMessage)
        {
            int blocks = 0;

            if (hits > 0)
            {
                attackMessage.AppendFormat("scoring {0} hits.", hits);
                defenseMessage.AppendFormat("  {0} defends and rolls: ", defender.Name);
                DiceExpression defenseDice = new DiceExpression().Dice(defender.Defense, 100);

                DiceResult defenseRoll = defenseDice.Roll();
                foreach (TermResult termResult in defenseRoll.Results)
                {
                    defenseMessage.Append(termResult.Value + ", ");
                    if (termResult.Value >= 100 - defender.DefenseChance)
                    {
                        blocks++;
                    }
                }
                defenseMessage.AppendFormat("resulting in {0} blocks.", blocks);
            }
            else
            {
                attackMessage.Append("and misses completely.");
            }

            return(blocks);
        }
Пример #8
0
        private static int ResolveDefense(Actor defender, int hits, StringBuilder attackMessage, StringBuilder defenseMessage)
        {
            int blocks = 0;

            if (hits > 0)
            {
                attackMessage.AppendFormat("atingindo {0} golpes", hits);
                defenseMessage.AppendFormat(" {0} defende e rola: ", defender.Name);

                DiceExpression defenseDice = new DiceExpression().Dice(defender.Defense, 100);
                DiceResult     defenseRoll = defenseDice.Roll();

                foreach (TermResult termResult in defenseRoll.Results)
                {
                    defenseMessage.Append(termResult.Value + ", ");

                    if (termResult.Value >= 100 - defender.DefenseChance)
                    {
                        blocks++;
                    }
                }

                defenseMessage.AppendFormat("resultando em {0} bloqueios", blocks);
            }
            else
            {
                attackMessage.Append("e erra completamente");
            }

            return(blocks);
        }
Пример #9
0
        // The defender rolls based on his stats to see if he blocks any of the hits from the attacker
        private int ResolveDefense(Actor defender, int hits, StringBuilder attackMessage, StringBuilder defenseMessage)
        {
            int blocks = 0;

            if (hits > 0)
            {
                attackMessage.AppendFormat("scoring {0} hits.", hits);
                defenseMessage.AppendFormat("  {0} defends and rolls: ", defender.Name);

                // Roll a number of 100-sided dice equal to the Defense value of the defendering actor
                DiceExpression defenseDice = new DiceExpression().Dice(defender.Defense, 100);
                DiceResult     defenseRoll = defenseDice.Roll();

                // Look at the face value of each single die that was rolled
                foreach (TermResult termResult in defenseRoll.Results)
                {
                    defenseMessage.Append(termResult.Value + ", ");
                    // Compare the value to 100 minus the defense chance and add a block if it's greater
                    if (termResult.Value >= 100 - defender.DefenseChance)
                    {
                        blocks++;
                    }
                }
                defenseMessage.AppendFormat("resulting in {0} blocks.", blocks);
            }
            else
            {
                attackMessage.Append("and misses completely.");
            }

            return(blocks);
        }
Пример #10
0
        public void TestCalculationExpressionWithDies()
        {
            var d1 = new DiceExpression("1d6");
            var d2 = new DiceExpression("1d8");

            var ce = new CalculationExpression("+", d1, d2);

            Assert.AreEqual(8, ce.GetExpected());
            Assert.AreEqual(2, ce.GetMin());
            Assert.AreEqual(14, ce.GetMax());

            ce = new CalculationExpression("-", d1, d2);
            Assert.AreEqual(-1, ce.GetExpected());
            Assert.AreEqual(-7, ce.GetMin());
            Assert.AreEqual(5, ce.GetMax());

            ce = new CalculationExpression("*", d1, d2);
            Assert.AreEqual(3.5 * 4.5, ce.GetExpected());
            Assert.AreEqual(1, ce.GetMin());
            Assert.AreEqual(48, ce.GetMax());

            ce = new CalculationExpression("/", d1, d2);
            Assert.AreEqual(3.5 / 4.5, ce.GetExpected());
            Assert.AreEqual(0.125, ce.GetMin());
            Assert.AreEqual(6, ce.GetMax());
        }
Пример #11
0
        /// <summary>
        /// The attacker rolls based on his stats to see if he gets any hits
        /// </summary>
        /// <param name="attacker"></param>
        /// <param name="defender"></param>
        /// <param name="attackMessage"></param>
        /// <returns></returns>
        private static int ResolveAttack(Actor attacker, Actor defender, StringBuilder attackMessage)
        {
            int hits = 0;

            // Roll a number of 100-sided dice equal to the Attack value of the attacking actor
            DiceExpression attackDice   = new DiceExpression().Dice(attacker.Attack, 100);
            DiceResult     attackResult = attackDice.Roll();

            // Look at the face value of each single die that was rolled
            foreach (TermResult termResult in attackResult.Results)
            {
                // Compare the value to 100 minus the attack chance and add a hit if it's greater
                if (termResult.Value >= 100 - attacker.AttackChance)
                {
                    hits++;
                }
            }

            // TODO: Different messages based on how many hits were made
            if (hits == attacker.Attack)
            {
                attackMessage.AppendFormat("{0} slams into {1} with full force!", attacker.Name, defender.Name);
            }
            else if (hits > 0)
            {
                attackMessage.AppendFormat("{0} lunges at {1}.", attacker.Name, defender.Name);
            }
            else
            {
                attackMessage.AppendFormat("{0} launches offbalance toward {1}.", attacker.Name, defender.Name);
            }

            return(hits);
        }
Пример #12
0
        // El defensor tira el dado basado en sus estadísticas para ver si
        // bloquea cuaqluiera de los golpes del atacante.
        private static int ResolveDefense( Actor defender, int hits, StringBuilder attackMessage, StringBuilder defenderMessage)
        {
            int blocks = 0;

            if ( hits > 0 )
            {
                attackMessage.AppendFormat("puntuación {0} golpes.", hits);
                defenderMessage.AppendFormat(" {0} defiende y tira: ", defender.Name);

                // Rodamos un número de 100 dados de igual lado con el valor de la
                // defensa del defensor.
                DiceExpression defenseDice = new DiceExpression().Dice(defender.Defensa, 100);
                DiceResult defenseRoll = defenseDice.Roll();

                // Miramos el valor de la cara de cada dado que fue rodado.
                foreach( TermResult termResult in defenseRoll.Results )
                {
                    defenderMessage.Append(termResult.Value + ", ");
                    // Comparamos el valor a 100, menos la probabilidad de
                    // defender y añadimos un bloqueo si este es mayor.
                    if ( termResult.Value >= 100 - defender.DefensaChance )
                    {
                        blocks += 1;
                    }
                }

                defenderMessage.AppendFormat("resultando en {0} bloqueo(s).", blocks);
            }
            else
            {
                attackMessage.Append("y falla completamente.");
            }

            return blocks;
        }
Пример #13
0
        public void HandlesDieTermWithChooseAndScalar()
        {
            const string   expression     = "2 + 3*4d6k3";
            DiceExpression diceExpression = _diceParser.Parse(expression);

            Assert.AreEqual(expression, diceExpression.ToString());
        }
Пример #14
0
        private float ResolveAttack()
        {
            if (GetComponent <Statistic>(ComponentType.Stats).Energy < 2)
            {
                return(0f);
            }
            GetComponent <Statistic>(ComponentType.Stats).Energy -= 2;
            float          hitPercent   = 0;
            DiceExpression attackDice   = new DiceExpression().Dice(AttackValue, 100);
            DiceResult     attackResult = attackDice.Roll();

            foreach (TermResult termResult in attackResult.Results)
            {
                if (termResult.Value >= (100 - Stats.AttackChance))
                {
                    hitPercent++;
                }
            }

            float hp = hitPercent / (float)AttackValue;

            System.Console.WriteLine("HitPercent:{0}", hp);

            return(hp);
        }
Пример #15
0
        private void Calculate()
        {
            if (Strenght == 0)
            {
                //This is an item do not calculate.
                calculated = true;
                return;
            }
            //Not an Equipment
            EquipmentSlot = EquipmentSlot.None;

            for (int i = 0; i < Vitality; i++)
            {
                DiceExpression diceExpression = new DiceExpression().Constant(1).Dice(1, 2);
                Health += diceExpression.Roll().Value;
                Energy += diceExpression.Roll().Value;
            }
            MaxHealth = Health;
            MaxEnergy = Energy;
            double atk = 0;

            for (int i = 0; i < Strenght; i++)
            {
                DiceExpression diceExpression = new DiceExpression().Dice(1, 2);
                atk += diceExpression.Roll().Value / 2.0;
            }
            Attack = (int)atk;
            double def = 0;

            for (int i = 0; i < Strenght; i++)
            {
                DiceExpression diceExpression = new DiceExpression().Dice(1, 2);
                def += (diceExpression.Roll().Value / 2.0) * 0.80;
            }
            Defence = (int)def;
            double atk_c = 50.0 + (Intelligence * 0.3);

            for (int i = 0; i < Dexterity; i++)
            {
                DiceExpression diceExpression = new DiceExpression().Dice(2, 3, 1, choose: 1);
                atk_c += diceExpression.Roll().Value;
            }
            AttackChance = (int)atk_c;
            double def_c = 20.0 + (Intelligence * 0.3);

            for (int i = 0; i < Dexterity; i++)
            {
                DiceExpression diceExpression = new DiceExpression().Dice(2, 3, 1, choose: 1);
                def_c += diceExpression.Roll().Value;
            }
            DefenceChance = (int)def_c;
            Awareness     = (int)(2.0 + (Intelligence * 0.3));
            Speed         = (int)(15.0 - (Dexterity * 0.4));
            _hpRegen      = (Vitality + Intelligence) * 0.005;
            _enRegen      = (Vitality + Intelligence) * 0.041;

            StatusChanged();
            calculated = true;
        }
        public void ToStringReturnsTermsSeparatedByPlus()
        {
            DiceExpression diceExpression = new DiceExpression().Constant(10).Die(8, -1);

            string toString = diceExpression.ToString();

            Assert.AreEqual("10 + -1*1d8", toString);
        }
Пример #17
0
        public void Parse_NegativeScaler_ExpectedExpression()
        {
            const string expression = "2 + -2*1d6";

            DiceExpression diceExpression = _diceParser.Parse(expression);

            Assert.AreEqual(expression, diceExpression.ToString());
        }
Пример #18
0
        public void Parse_DiceTermWithChooseAndScalar_ExpectedExpression()
        {
            const string expression = "2 + 3*4d6k3";

            DiceExpression diceExpression = _diceParser.Parse(expression);

            Assert.AreEqual(expression, diceExpression.ToString());
        }
Пример #19
0
        public void HandlesNegativeScalar()
        {
            const string expression = "2 + -2*1d6";

            DiceExpression diceExpression = _diceParser.Parse(expression);

            Assert.AreEqual(expression, diceExpression.ToString());
        }
Пример #20
0
 private static void AssertReturnedInRange(DiceExpression expr, int min, int max)
 {
     for (var i = 0; i < 100; i++)
     {
         var result = expr.Roll();
         Assert.True(result >= min && result <= max);
     }
 }
Пример #21
0
        public void ToString_ExpressionBuiltFluently_ReturnsTermsSeparatedByPlus()
        {
            DiceExpression diceExpression = new DiceExpression().Constant( 10 ).Die( 8, -1 );

             string toString = diceExpression.ToString();

             Assert.AreEqual( "10 + -1*1d8", toString );
        }
Пример #22
0
        public void HandlesDieWithImplicitMultiplicityOf1()
        {
            const string expression         = "2 + 2*d6";
            const string expectedExpression = "2 + 2*1d6";

            DiceExpression diceExpression = _diceParser.Parse(expression);

            Assert.AreEqual(expectedExpression, diceExpression.ToString());
        }
Пример #23
0
        public void Parse_DiceTermWithImplicitMultiplicityOfOne_ExpectedExpression()
        {
            const string expression         = "2 + 2*d6";
            const string expectedExpression = "2 + 2*1d6";

            DiceExpression diceExpression = _diceParser.Parse(expression);

            Assert.AreEqual(expectedExpression, diceExpression.ToString());
        }
Пример #24
0
        public void OneSidedDie()
        {
            var leftExpression  = new ConstantExpression(1);
            var rightExpression = new ConstantExpression(1);

            var expression = new DiceExpression(leftExpression, rightExpression, dice);

            Assert.AreEqual(1, expression.Evaluate());
        }
Пример #25
0
        private void RollDamageBtn_Click(object sender, EventArgs e)
        {
            DiceExpression exp = DiceExpression.Parse(DamageExpLbl.Text);

            if (exp != null)
            {
                int roll = exp.Evaluate();
                DamageBox.Value = roll;
            }
        }
Пример #26
0
        private void RollDamageBtn_Click(object sender, EventArgs e)
        {
            DiceExpression diceExpression = DiceExpression.Parse(this.DamageExpLbl.Text);

            if (diceExpression != null)
            {
                int num = diceExpression.Evaluate();
                this.DamageBox.Value = num;
            }
        }
Пример #27
0
        public void OperandsGetTruncated()
        {
            var leftExpression  = new ConstantExpression(2.9);
            var rightExpression = new ConstantExpression(6.9);

            var expression = new DiceExpression(leftExpression, rightExpression, dice);

            Assert.That(expression.Evaluate(), Is.GreaterThanOrEqualTo(2));
            Assert.That(expression.Evaluate(), Is.LessThanOrEqualTo(12));
        }
Пример #28
0
        public void SixSidedDice()
        {
            var leftExpression  = new ConstantExpression(2);
            var rightExpression = new ConstantExpression(6);

            var expression = new DiceExpression(leftExpression, rightExpression, dice);

            Assert.That(expression.Evaluate(), Is.GreaterThanOrEqualTo(2));
            Assert.That(expression.Evaluate(), Is.LessThanOrEqualTo(12));
        }
Пример #29
0
        public void Roll_ExpressionBuiltFluently_ReturnsCorrectNumberOfResults()
        {
            DiceExpression diceExpression = new DiceExpression()
             .Constant( 5 )
             .Die( 8 )
             .Dice( 4, 6, choose: 3 );
             const int expectedNumberOfTerms = 1 + 1 + 3;

             DiceResult result = diceExpression.Roll( new DotNetRandom() );

             Assert.AreEqual( expectedNumberOfTerms, result.Results.Count );
        }
Пример #30
0
        public void Roll_ExpressionBuiltFluently_ReturnsCorrectNumberOfResults()
        {
            DiceExpression diceExpression = new DiceExpression()
                                            .Constant(5)
                                            .Die(8)
                                            .Dice(4, 6, choose: 3);
            const int expectedNumberOfTerms = 1 + 1 + 3;

            DiceResult result = diceExpression.Roll(new DotNetRandom());

            Assert.AreEqual(expectedNumberOfTerms, result.Results.Count);
        }
Пример #31
0
        public void TestCalculationExpressionWithCalculations()
        {
            var const1 = new ConstantExpression("1");
            var const2 = new ConstantExpression("-1");
            var d1     = new DiceExpression("1d6");
            var d2     = new DiceExpression("1d8");

            var ce = new CalculationExpression("+", new CalculationExpression("+", d1, d2), new CalculationExpression("+", const1, const2));

            Assert.AreEqual(8, ce.GetExpected());
            Assert.AreEqual(2, ce.GetMin());
            Assert.AreEqual(14, ce.GetMax());
        }
        public void ContainsAndReturnsCorrectNumberOfValues()
        {
            DiceExpression diceExpression = new DiceExpression()
                                            .Constant(5)
                                            .Die(8)
                                            .Dice(4, 6, choose: 3);

            DiceResult result = diceExpression.Roll(new StandardDieRoller(new Random()));

            const int expectedNumberOfTerms = 1 + 1 + 3;

            Assert.AreEqual(expectedNumberOfTerms, result.Results.Count);
        }
Пример #33
0
 private static void Append( DiceExpression dice, ParseValues parseValues )
 {
     int constant = int.Parse( parseValues.Constant );
      if ( parseValues.Multiplicity == 0 )
      {
     dice.Constant( parseValues.Scalar * constant );
      }
      else
      {
     dice.Dice( parseValues.Multiplicity, constant, parseValues.Scalar, parseValues.Choose );
      }
 }
Пример #34
0
        /// <summary>
        /// Create a new DiceExpression by parsing the specified string
        /// </summary>
        /// <param name="expression">A dice notation string expression. Ex. 3d6+3</param>
        /// <returns>A DiceExpression parsed from the specified string</returns>
        /// <exception cref="ArgumentException">Invalid dice notation supplied</exception>
        public DiceExpression Parse( string expression )
        {
            if ( string.IsNullOrEmpty( expression ) )
             {
            throw new ArgumentException( "A dice notation expression must be supplied.", "expression" );
             }

             string cleanExpression = _whitespacePattern.Replace( expression.ToLower(), "" );
             cleanExpression = cleanExpression.Replace( "+-", "-" );

             var parseValues = new ParseValues().Init();

             var dice = new DiceExpression();

             for ( int i = 0; i < cleanExpression.Length; ++i )
             {
            char c = cleanExpression[i];

            if ( char.IsDigit( c ) )
            {
               parseValues.Constant += c;
            }
            else if ( c == '*' )
            {
               parseValues.Scalar *= int.Parse( parseValues.Constant );
               parseValues.Constant = "";
            }
            else if ( c == 'd' )
            {
               if ( parseValues.Constant.Length == 0 )
               {
                  parseValues.Constant = "1";
               }
               parseValues.Multiplicity = int.Parse( parseValues.Constant );
               parseValues.Constant = "";
            }
            else if ( c == 'k' )
            {
               string chooseAccum = "";
               while ( i + 1 < cleanExpression.Length && char.IsDigit( cleanExpression[i + 1] ) )
               {
                  chooseAccum += cleanExpression[i + 1];
                  ++i;
               }
               parseValues.Choose = int.Parse( chooseAccum );
            }
            else if ( c == '+' )
            {
               Append( dice, parseValues );
               parseValues = new ParseValues().Init();
            }
            else if ( c == '-' )
            {
               Append( dice, parseValues );
               parseValues = new ParseValues().Init();
               parseValues.Scalar = -1;
            }
            else
            {
               throw new ArgumentException( "Invalid character in dice expression", "expression" );
            }
             }
             Append( dice, parseValues );

             return dice;
        }