Extend int class for natural dice construction #6
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using pflib_net.dice;
|
using pflib_net.dice;
|
||||||
|
|
||||||
@ -8,13 +9,21 @@ namespace pflib_net.Tests;
|
|||||||
[TestOf(typeof(Dice))]
|
[TestOf(typeof(Dice))]
|
||||||
public class DiceTest
|
public class DiceTest
|
||||||
{
|
{
|
||||||
|
private static IEnumerable<Func<Dice, Dice>> OperationCases
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return (dice) => dice.Kh(1);
|
||||||
|
yield return (dice) => dice.Kl(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
[Repeat(100)]
|
[Repeat(100)]
|
||||||
public void D20RollProducesValueBetween1And20()
|
public void D20RollProducesValueBetween1And20()
|
||||||
{
|
{
|
||||||
var die = new Dice(20);
|
var die = new Dice(20);
|
||||||
Assert.That(die.GetValue(), Is.InRange(1, 20));
|
Assert.That(die.Roll(), Is.InRange(1, 20));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -23,7 +32,7 @@ public class DiceTest
|
|||||||
public void RollAddsValuesCorrectly(int value)
|
public void RollAddsValuesCorrectly(int value)
|
||||||
{
|
{
|
||||||
var die = new Dice(4, add: 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]
|
[Test]
|
||||||
@ -31,21 +40,21 @@ public class DiceTest
|
|||||||
public void RollMultipleDiceHasCorrectAmountOfDice()
|
public void RollMultipleDiceHasCorrectAmountOfDice()
|
||||||
{
|
{
|
||||||
var dice = new Dice(4, 20);
|
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]
|
[Test]
|
||||||
public void KeepHighestHasCorrectNumberOfDice()
|
public void KeepHighestHasCorrectNumberOfDice()
|
||||||
{
|
{
|
||||||
var dice = new Dice(1, 40, 20);
|
var dice = new Dice(1, 40, 20);
|
||||||
Assert.That(dice.GetValue(), Is.EqualTo(20));
|
Assert.That(dice.Roll(), Is.EqualTo(20));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void KeepLowestHasCorrectNumberOfDice()
|
public void KeepLowestHasCorrectNumberOfDice()
|
||||||
{
|
{
|
||||||
var dice = new Dice(1, 40, keepLowest: 20);
|
var dice = new Dice(1, 40, keepLowest: 20);
|
||||||
Assert.That(dice.GetValue(), Is.EqualTo(20));
|
Assert.That(dice.Roll(), Is.EqualTo(20));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -54,4 +63,22 @@ public class DiceTest
|
|||||||
Assert.Throws<InvalidOperationException>(
|
Assert.Throws<InvalidOperationException>(
|
||||||
() => new Dice(1, 40, 1, 1));
|
() => 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;
|
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>();
|
var rolls = new List<int>();
|
||||||
for (var i = 0; i < Count; i++)
|
for (var i = 0; i < Count; i++)
|
||||||
@ -53,3 +75,11 @@ public struct Dice
|
|||||||
return rolls.Sum() + Add;
|
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