Add dice (#4)
All checks were successful
MANDATORY: Build project (Pflib Net) TeamCity build finished
All checks were successful
MANDATORY: Build project (Pflib Net) TeamCity build finished
Reviewed-on: #4 Co-authored-by: Malachy Byrne <malachybyrne1@gmail.com> Co-committed-by: Malachy Byrne <malachybyrne1@gmail.com>
This commit is contained in:
parent
f7fb1ff826
commit
8abd1e99ef
31
.editorconfig
Normal file
31
.editorconfig
Normal 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
|
||||||
58
pflib-net.Tests/dice/DiceTest.cs
Normal file
58
pflib-net.Tests/dice/DiceTest.cs
Normal 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
57
pflib-net/dice/Dice.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,10 +5,7 @@
|
|||||||
<RootNamespace>pflib_net</RootNamespace>
|
<RootNamespace>pflib_net</RootNamespace>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="conditions\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user