c# - 不确定如何在 DnD Kata 中添加强度修饰符

标签 c# tdd

我开始在 Everquest TDD Kata ( Github Link) 上编写代码,并且我正在参与

Feature: Character Ability Modifiers Modify Attributes

As a character I want to apply my ability modifiers improve my capabilities in combat so that I can vanquish my enemy with extreme prejudice

add Strength modifier to:

  • attack roll and damage dealt
  • double Strength modifier on critical hits

但我不知道我的数学在我的考试中是否正确。使用 NUnit 的 TestCaseSource 我有以下测试

[TestFixture]
public class CharacterAbilityTests
{
    [TestCaseSource("StrengthAbilityTestCases", Category = "Character Strength Tests")]
    public int StrengthAbiltyAddsModifierToAttackRollAndDamage(Character hero, int hitroll)
    {
        var enemy = new Character();
        Assert.That(hero.Attack(enemy, hitroll), Is.True);

        return enemy.HitPoints;
    }

    private static IEnumerable<TestCaseData> StrengthAbilityTestCases
    {
        get
        {
            yield return My.Hero.WithStrength(12).RollsHitOf(9).LeavesEnemyWithHitPoints(3);
            yield return My.Hero.WithStrength(14).RollsHitOf(8).LeavesEnemyWithHitPoints(2);
            yield return My.Hero.WithStrength(16).RollsHitOf(7).LeavesEnemyWithHitPoints(1);
            yield return My.Hero.WithStrength(19).RollsHitOf(6).LeavesEnemyWithHitPoints(0);
            yield return My.Hero.WithStrength(20).RollsHitOf(5).LeavesEnemyWithHitPoints(-1);

            yield return My.Hero.WithStrength(12).RollsHitOf(20).LeavesEnemyWithHitPoints(2);
            yield return My.Hero.WithStrength(14).RollsHitOf(20).LeavesEnemyWithHitPoints(0);
            yield return My.Hero.WithStrength(16).RollsHitOf(20).LeavesEnemyWithHitPoints(-2);
            yield return My.Hero.WithStrength(19).RollsHitOf(20).LeavesEnemyWithHitPoints(-4);
            yield return My.Hero.WithStrength(20).RollsHitOf(20).LeavesEnemyWithHitPoints(-6);
        }
    }
    private class My
    {
        private Character hero;
        private int hitRoll;

        public static My Hero { get { return new My(); } }
        private My() { hero = new Character(); }

        public My WithStrength(int strength){hero.Strength = strength;return this;}
        public My RollsHitOf(int hitRoll) { this.hitRoll = hitRoll; return this; }

        public TestCaseData LeavesEnemyWithHitPoints(int expectedHitPoints)
        {
            var testCaseData = new TestCaseData(hero, hitRoll)
            {
                HasExpectedResult = true,
                ExpectedResult = expectedHitPoints,
                TestName = $"Hero with Stength of {hero.Strength} rolling a {hitRoll} Should leave enemy with {expectedHitPoints} Hit points"
            };

            return testCaseData;
        }
    }
}

我所有处理关键命中的测试都失败了(最后 5 个测试)问题是我不确定我的数学是否正确。这是我的攻击方法:(请注意,我之前的所有测试目前都通过了)

public class Character
{
    public string Name { get; set; }
    public Alignment Alignment { get; set; }
    public int ArmorClass { get; private set; }
    public int HitPoints { get; private set; }
    public bool IsDead { get { return HitPoints <= 0; } }

    public Ability Strength { get; set; }
    public Ability Dexterity { get; set; }
    public Ability Constitution { get; set; }
    public Ability Wisdom { get; set; }
    public Ability Intelligence { get; set; }
    public Ability Charisma { get; set; }

    public Character()
    {
        ArmorClass = 10;
        HitPoints = 5;
        Strength = 10;
        Dexterity = 10;
        Constitution = 10;
        Wisdom = 10;
        Intelligence = 10;
        Charisma = 10;
    }

    public bool Attack(Character enemy, int hitRoll)
    {
        var damage = 0;
        var modifier = IsCritHit(hitRoll) ? Strength.Modifier * 2 : Strength.Modifier;
        var isEnemyHit = IsCritHit(hitRoll) || (enemy.ArmorClass <= (hitRoll + modifier));

        if (!isEnemyHit) return false;

        damage = 1 + modifier;
        if (IsCritHit(hitRoll)) damage *= 2;
        enemy.TakeDamage(damage);

        return true;
    }

    private static bool IsCritHit(int hitRoll)
    {
        return hitRoll == 20;
    }

    public void TakeDamage(int damage)
    {
        HitPoints-= damage;
    }
}

public class Ability
{
    public int Score { get; private set; }
    public int Modifier { get; private set; }

    private Ability(int score)
    {
        Score = score;
        Modifier = (int)(Math.Floor((Score - 10) / 2.0));
    }

    public override string ToString()
    {
        return $"{Score} [{Modifier}]";
    }
    public static implicit operator int(Ability ability)
    {
        return ability.Score;
    }

    public static implicit operator Ability(int score)
    {
        return new Ability(score);
    }
}

编辑

我忘记了 AC 和 HP 的魔法数字分别是 10 和 5。它们来自 Kata 的规范。

编辑 2.0

这是我 5 次失败测试中第一个的输出

Test Name:  Hero with Stength of 12 [1] rolling a 20 Should leave enemy with 2 Hit points
Test FullName:  EmptyProject.Tests.CharacterAbilityTests.Hero with Stength of 12 [1] rolling a 20 Should leave enemy with 2 Hit points
Test Source:    D:\Users\Robert\Source\Repos\EverCraft-Kata\dotNet\src\EmptyProject.Tests\CharacterAbilityTests.cs : line 14
Test Outcome:   Failed
Test Duration:  0:00:00.012

Result StackTrace:  
at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.TestActionCommand.Execute(TestExecutionContext context)
Result Message: 
Expected: 2
  But was:  -1

编辑希望最终

我想我的意思是问它应该是 (baseDamage+strengthModifier) * 2,还是 (baseDamage *2 ) + strengthModifier?

最佳答案

您的 .LeavesEnemyWithHitPoints(2) 是错误的。应该是-1。

这段代码:

var damage = 0;
var modifier = IsCritHit(hitRoll) ? Strength.Modifier * 2 : Strength.Modifier;
var isEnemyHit = IsCritHit(hitRoll) || (enemy.ArmorClass <= (hitRoll + modifier));

if (!isEnemyHit) return false;

damage = 1 + modifier;
if (IsCritHit(hitRoll)) damage *= 2;
    enemy.TakeDamage(damage);

Strength.Modifier 为 1 时,您的 modifier 计算在重击时产生 2。这意味着 damage = 1 + modifier 是 3。因为你掷出了重击,所以伤害加倍使你的总 damage = 6。如果敌人的总 HP 是 5你打了他 6,他应该有 -1 HP。

我不知道这是否是正确的 DnD 计算(不记得了)

关于c# - 不确定如何在 DnD Kata 中添加强度修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34864142/

相关文章:

c# - UnitTest - 测试来自模拟的方法时的问题

c# - .NET 的免费 UML 绘图库

c# - 带有子计数的 NHibernate 父列表

c++ - 哪些开源 C++ 项目有很好的(鼓舞人心的)测试?

ActionScript tdd 框架

php - 包含或需要一个 vfsStream 文件

c# - 使用 C# 的 HTTP MAC 身份验证

c# - Asp.net Core IoC 类型工厂

c# - 使用流从 C# 中的 mssql 中获取图像

c# - 美元对象的复杂性