From ac18607594c001d696c2aff65818705559ee6824 Mon Sep 17 00:00:00 2001 From: Malachy Byrne Date: Mon, 17 Jun 2024 22:00:22 +0100 Subject: [PATCH] Add dice --- .editorconfig | 31 +++++++++++++++++ pflib-net.Tests/dice/DiceTest.cs | 58 ++++++++++++++++++++++++++++++++ pflib-net/dice/Dice.cs | 57 +++++++++++++++++++++++++++++++ pflib-net/pflib-net.csproj | 5 +-- 4 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 .editorconfig create mode 100644 pflib-net.Tests/dice/DiceTest.cs create mode 100644 pflib-net/dice/Dice.cs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4d17742 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/pflib-net.Tests/dice/DiceTest.cs b/pflib-net.Tests/dice/DiceTest.cs new file mode 100644 index 0000000..17e55b9 --- /dev/null +++ b/pflib-net.Tests/dice/DiceTest.cs @@ -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( + () => new Dice(1, 40, high, low)); + } +} \ No newline at end of file diff --git a/pflib-net/dice/Dice.cs b/pflib-net/dice/Dice.cs new file mode 100644 index 0000000..383054d --- /dev/null +++ b/pflib-net/dice/Dice.cs @@ -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(); + 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; + } +} \ No newline at end of file diff --git a/pflib-net/pflib-net.csproj b/pflib-net/pflib-net.csproj index decb98f..41a64f8 100644 --- a/pflib-net/pflib-net.csproj +++ b/pflib-net/pflib-net.csproj @@ -5,10 +5,7 @@ pflib_net enable enable + true - - - -