From 2894fc99d8128687056aa12f56a3de46d8852207 Mon Sep 17 00:00:00 2001 From: Malachy Byrne Date: Tue, 18 Jun 2024 00:52:09 +0200 Subject: [PATCH] Make random object used in Dice.cs static and remove unused logging. (#5) Remove unused parts of negative test. Reviewed-on: https://git.malmal200.xyz/malmal200/pflib-net/pulls/5 Co-authored-by: Malachy Byrne Co-committed-by: Malachy Byrne --- pflib-net.Tests/dice/DiceTest.cs | 7 +++---- pflib-net/dice/Dice.cs | 6 ++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/pflib-net.Tests/dice/DiceTest.cs b/pflib-net.Tests/dice/DiceTest.cs index 17e55b9..1b93024 100644 --- a/pflib-net.Tests/dice/DiceTest.cs +++ b/pflib-net.Tests/dice/DiceTest.cs @@ -49,10 +49,9 @@ public class DiceTest } [Test] - [TestCase(1, 1)] - public void CannotSetKeepHighestAndKeepLowest(int high, int low) + public void CannotSetKeepHighestAndKeepLowest() { - var e = Assert.Throws( - () => new Dice(1, 40, high, low)); + Assert.Throws( + () => new Dice(1, 40, 1, 1)); } } \ No newline at end of file diff --git a/pflib-net/dice/Dice.cs b/pflib-net/dice/Dice.cs index 383054d..90024d2 100644 --- a/pflib-net/dice/Dice.cs +++ b/pflib-net/dice/Dice.cs @@ -7,6 +7,7 @@ public struct Dice private int KeepHighest { get; set; } private int KeepLowest { get; set; } private int Add { get; set; } + private static readonly Random Random = new(); public Dice(int size, int count = 1, int keepHighest = 0, int keepLowest = 0, int add = 0) { @@ -24,11 +25,10 @@ public struct Dice 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.Add(Random.Next(Size) + 1); } rolls.Sort(); @@ -37,7 +37,6 @@ public struct Dice var repeats = Count - KeepHighest; for (var i = 0; i < repeats; i++) { - Console.WriteLine("Removing max"); rolls.Remove(rolls.Max()); } } @@ -48,7 +47,6 @@ public struct Dice for (var i = 0; i < repeats; i++) { - Console.WriteLine("Removing min"); rolls.Remove(rolls.Min()); } }