Add dice #4

Merged
malmal200 merged 1 commits from PF-1-add-dice into master 2024-06-17 23:04:58 +02:00
4 changed files with 147 additions and 4 deletions

31
.editorconfig Normal file
View File

@ -0,0 +1,31 @@
[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = false
insert_final_newline = false
indent_style = space
tab_width = 4
# Microsoft .NET properties
indent_size = 4
csharp_new_line_before_members_in_object_initializers = false
csharp_preferred_modifier_order = public, private, protected, internal, file, new, static, abstract, virtual, sealed, readonly, override, extern, unsafe, volatile, async, required:suggestion
csharp_style_var_elsewhere = true:suggestion
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
dotnet_naming_rule.unity_serialized_field_rule.severity = warning
dotnet_naming_rule.unity_serialized_field_rule.style = lower_camel_case_style
dotnet_naming_rule.unity_serialized_field_rule.symbols = unity_serialized_field_symbols
dotnet_naming_style.lower_camel_case_style.capitalization = camel_case
dotnet_naming_symbols.unity_serialized_field_symbols.applicable_accessibilities = *
dotnet_naming_symbols.unity_serialized_field_symbols.applicable_kinds =
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
dotnet_style_qualification_for_event = false:suggestion
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion

View File

@ -0,0 +1,58 @@
using System;
using NUnit.Framework;
using pflib_net.dice;
namespace pflib_net.Tests;
[TestFixture]
[TestOf(typeof(Dice))]
public class DiceTest
{
[Test]
[Repeat(100)]
public void D20RollProducesValueBetween1And20()
{
var die = new Dice(20);
Assert.That(die.GetValue(), Is.InRange(1, 20));
}
[Test]
[TestCase(10)]
[TestCase(-10)]
public void RollAddsValuesCorrectly(int value)
{
var die = new Dice(4, add: value);
Assert.That(die.GetValue(), Is.InRange(1 + value, 4 + value));
}
[Test]
[Repeat(100)]
public void RollMultipleDiceHasCorrectAmountOfDice()
{
var dice = new Dice(4, 20);
Assert.That(dice.GetValue(), Is.InRange(20, 4 * 20));
}
[Test]
public void KeepHighestHasCorrectNumberOfDice()
{
var dice = new Dice(1, 40, 20);
Assert.That(dice.GetValue(), Is.EqualTo(20));
}
[Test]
public void KeepLowestHasCorrectNumberOfDice()
{
var dice = new Dice(1, 40, keepLowest: 20);
Assert.That(dice.GetValue(), Is.EqualTo(20));
}
[Test]
[TestCase(1, 1)]
public void CannotSetKeepHighestAndKeepLowest(int high, int low)
{
var e = Assert.Throws<InvalidOperationException>(
() => new Dice(1, 40, high, low));
}
}

57
pflib-net/dice/Dice.cs Normal file
View File

@ -0,0 +1,57 @@
namespace pflib_net.dice;
public struct Dice
{
private int Count { get; set; }
private int Size { get; set; }
private int KeepHighest { get; set; }
private int KeepLowest { get; set; }
private int Add { get; set; }
public Dice(int size, int count = 1, int keepHighest = 0, int keepLowest = 0, int add = 0)
{
if (keepHighest > 0 && keepLowest > 0)
{
throw new InvalidOperationException("Cannot set keepHighest and keepLowest at the same time");
}
Count = count;
Size = size;
KeepHighest = keepHighest;
KeepLowest = keepLowest;
Add = add;
}
public int GetValue()
{
var random = new Random();
var rolls = new List<int>();
for (var i = 0; i < Count; i++)
{
rolls.Add(random.Next(Size) + 1);
}
rolls.Sort();
if (KeepHighest > 0)
{
var repeats = Count - KeepHighest;
for (var i = 0; i < repeats; i++)
{
Console.WriteLine("Removing max");
rolls.Remove(rolls.Max());
}
}
if (KeepLowest > 0)
{
var repeats = Count - KeepLowest;
for (var i = 0; i < repeats; i++)
{
Console.WriteLine("Removing min");
rolls.Remove(rolls.Min());
}
}
return rolls.Sum() + Add;
}
}

View File

@ -5,10 +5,7 @@
<RootNamespace>pflib_net</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
<ItemGroup>
<Folder Include="conditions\" />
</ItemGroup>
</Project>