Extend int class for natural dice construction #6
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using pflib_net.dice;
|
||||
|
||||
@ -8,13 +9,21 @@ namespace pflib_net.Tests;
|
||||
[TestOf(typeof(Dice))]
|
||||
public class DiceTest
|
||||
{
|
||||
private static IEnumerable<Func<Dice, Dice>> OperationCases
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return (dice) => dice.Kh(1);
|
||||
yield return (dice) => dice.Kl(1);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Repeat(100)]
|
||||
public void D20RollProducesValueBetween1And20()
|
||||
{
|
||||
var die = new Dice(20);
|
||||
Assert.That(die.GetValue(), Is.InRange(1, 20));
|
||||
Assert.That(die.Roll(), Is.InRange(1, 20));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -23,7 +32,7 @@ public class DiceTest
|
||||
public void RollAddsValuesCorrectly(int value)
|
||||
{
|
||||
var die = new Dice(4, add: value);
|
||||
Assert.That(die.GetValue(), Is.InRange(1 + value, 4 + value));
|
||||
Assert.That(die.Roll(), Is.InRange(1 + value, 4 + value));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -31,21 +40,21 @@ public class DiceTest
|
||||
public void RollMultipleDiceHasCorrectAmountOfDice()
|
||||
{
|
||||
var dice = new Dice(4, 20);
|
||||
Assert.That(dice.GetValue(), Is.InRange(20, 4 * 20));
|
||||
Assert.That(dice.Roll(), Is.InRange(20, 4 * 20));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void KeepHighestHasCorrectNumberOfDice()
|
||||
{
|
||||
var dice = new Dice(1, 40, 20);
|
||||
Assert.That(dice.GetValue(), Is.EqualTo(20));
|
||||
Assert.That(dice.Roll(), Is.EqualTo(20));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void KeepLowestHasCorrectNumberOfDice()
|
||||
{
|
||||
var dice = new Dice(1, 40, keepLowest: 20);
|
||||
Assert.That(dice.GetValue(), Is.EqualTo(20));
|
||||
Assert.That(dice.Roll(), Is.EqualTo(20));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -54,4 +63,22 @@ public class DiceTest
|
||||
Assert.Throws<InvalidOperationException>(
|
||||
() => new Dice(1, 40, 1, 1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(OperationCases))]
|
||||
[Repeat(100)]
|
||||
public void CanBuildUsingIntExtension(Func<Dice, Dice> func)
|
||||
{
|
||||
var dice = func(2.D(6));
|
||||
Assert.That(dice.Roll(), Is.InRange(1, 6));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void KhAndKlTogetherFails()
|
||||
{
|
||||
Assert.Throws<InvalidOperationException>(
|
||||
() => 1.D(6).Kh(1).Kl(1));
|
||||
Assert.Throws<InvalidOperationException>(
|
||||
() => 1.D(6).Kl(1).Kh(1));
|
||||
}
|
||||
}
|
||||
@ -23,7 +23,29 @@ public struct Dice
|
||||
Add = add;
|
||||
}
|
||||
|
||||
public int GetValue()
|
||||
public Dice Kh(int keepHighest)
|
||||
{
|
||||
if (KeepLowest > 0)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot set keepHighest and keepLowest in the same roll");
|
||||
}
|
||||
|
||||
KeepHighest = keepHighest;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Dice Kl(int keepLowest)
|
||||
{
|
||||
if (KeepHighest > 0)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot set keepHighest and keepLowest in the same roll");
|
||||
}
|
||||
|
||||
KeepLowest = keepLowest;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int Roll()
|
||||
{
|
||||
var rolls = new List<int>();
|
||||
for (var i = 0; i < Count; i++)
|
||||
@ -52,4 +74,12 @@ public struct Dice
|
||||
}
|
||||
return rolls.Sum() + Add;
|
||||
}
|
||||
}
|
||||
|
||||
public static class IntExtension
|
||||
{
|
||||
public static Dice D(this int count, int size)
|
||||
{
|
||||
return new Dice(size, count);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user