Malachy Byrne 327feab7d8
Some checks failed
MANDATORY: Build project (Pflib Net) TeamCity build failed
Add basic effects and condition interface
2024-06-28 20:08:14 +01:00

97 lines
2.1 KiB
C#

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<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++)
{
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);
}
}