namespace pflib_net.dice; public class Dice { private int _keepHighest; private int _keepLowest; private int Count { get; set; } private int Size { get; set; } private int KeepHighest { get => _keepHighest; set { if (value > 0 && KeepLowest > 0) { throw new InvalidOperationException("Cannot set keepHighest and keepLowest at the same time"); } _keepHighest = value; } } private int KeepLowest { get => _keepLowest; set { if (value > 0 && KeepHighest > 0) { throw new InvalidOperationException("Cannot set keepHighest and keepLowest at the same time"); } _keepLowest = value; } } 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) { Count = count; Size = size; KeepHighest = keepHighest; KeepLowest = keepLowest; Add = add; } public Dice Kh(int keepHighest) { KeepHighest = keepHighest; return this; } public Dice Kl(int keepLowest) { KeepLowest = keepLowest; return this; } public int Roll() { 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++) { rolls.Remove(rolls.Max()); } } if (KeepLowest > 0) { var repeats = Count - KeepLowest; for (var i = 0; i < repeats; i++) { rolls.Remove(rolls.Min()); } } return rolls.Sum() + Add; } } public static class IntExtension { public static Dice D(this int count, int size) { return new Dice(size, count); } }