我开始使用Everquest TDD Kata(Github Link)进行编码,我就此而言
功能:角色能力修改器修改属性
作为一个角色,我想运用我的能力修正器来提高我在战斗中的能力,这样我就能用极端的偏见战胜我的敌人
将强度修饰符添加到:
- 攻击掷骰和造成的伤害
- 关键命中的双倍强度修正
但我无法弄清楚我的数学是否正确。使用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。它们来自卡塔的规格。
EDIT 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
?< / p>
答案 0 :(得分:2)
您的MQQueueManager
错了。它应该是-1。
这部分代码:
.LeavesEnemyWithHitPoints(2)
如果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);
为1,那么您的Strength.Modifier
计算会在重击时产生2。这意味着modifier
是3.因为你已经击中一个致命一击,伤害加倍使你的总damage = 1 + modifier
= 6.如果敌人的总HP为5而你打他6,他应该有-1 HP。
我不知道这是否是正确的DnD计算(不记得了)
答案 1 :(得分:1)
它是(baseDamage+strengthModifier) * 2
但是,当不重要时,如果“强度”小于10,且修改器为负,则您实际造成的损害为负或零,这听起来不合理。我的意思是,如果您有力量6并击中某人,您实际上是给该玩家1点生命值?如果很关键,您给2点生命值? :P该部分的问题描述不明确