Refactored DiceCup to store the previous value
This commit is contained in:
parent
767189821d
commit
23ee40c96d
@ -8,7 +8,8 @@ public class DiceCupTests
|
||||
public void Roll_ReturnsTwoElements()
|
||||
{
|
||||
var diceCup = new DiceCup();
|
||||
var roll = diceCup.Roll;
|
||||
diceCup.Roll();
|
||||
var roll = diceCup.Values;
|
||||
Assert.That(roll, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
@ -16,10 +17,15 @@ public class DiceCupTests
|
||||
public void Roll_ReturnsNumbersInRange1To6()
|
||||
{
|
||||
var diceCup = new DiceCup();
|
||||
var roll = diceCup.Roll;
|
||||
Assert.That(roll[0], Is.GreaterThanOrEqualTo(1));
|
||||
Assert.That(roll[0], Is.LessThanOrEqualTo(6));
|
||||
Assert.That(roll[1], Is.GreaterThanOrEqualTo(1));
|
||||
Assert.That(roll[1], Is.LessThanOrEqualTo(6));
|
||||
diceCup.Roll();
|
||||
var roll = diceCup.Values;
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(roll[0], Is.GreaterThanOrEqualTo(1));
|
||||
Assert.That(roll[0], Is.LessThanOrEqualTo(6));
|
||||
Assert.That(roll[1], Is.GreaterThanOrEqualTo(1));
|
||||
Assert.That(roll[1], Is.LessThanOrEqualTo(6));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,8 @@ public class DiceTests
|
||||
public void Roll_ReturnsNumberBetween1And6()
|
||||
{
|
||||
var dice = new Dice();
|
||||
var roll = dice.Roll;
|
||||
dice.Roll();
|
||||
var roll = dice.Value;
|
||||
Assert.That(roll, Is.GreaterThanOrEqualTo(1));
|
||||
Assert.That(roll, Is.LessThanOrEqualTo(6));
|
||||
}
|
||||
|
@ -60,6 +60,7 @@ public class ActionServiceTests
|
||||
[Test]
|
||||
public void RollDice_ReturnsListOfIntegers()
|
||||
{
|
||||
_service.Group = new pacMan.Services.Game(new Queue<DirectionalPosition>());
|
||||
var dices = _service.RollDice();
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
|
@ -1,13 +1,12 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace pacMan.GameStuff.Items;
|
||||
|
||||
public interface IDice
|
||||
{
|
||||
int Roll { get; }
|
||||
}
|
||||
|
||||
public class Dice : IDice
|
||||
public class Dice
|
||||
{
|
||||
private readonly Random _random = new();
|
||||
|
||||
public int Roll => _random.Next(1, 7);
|
||||
[JsonInclude] public int Value { get; private set; }
|
||||
|
||||
public void Roll() => Value = _random.Next(1, 7);
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace pacMan.GameStuff.Items;
|
||||
|
||||
public interface IDiceCup
|
||||
{
|
||||
List<int> Roll { get; }
|
||||
}
|
||||
|
||||
public class DiceCup : IDiceCup
|
||||
public class DiceCup
|
||||
{
|
||||
private readonly List<Dice> _dices;
|
||||
|
||||
@ -16,5 +13,7 @@ public class DiceCup : IDiceCup
|
||||
new()
|
||||
};
|
||||
|
||||
public List<int> Roll => _dices.Select(d => d.Roll).ToList();
|
||||
[JsonInclude] public List<int> Values => _dices.Select(dice => dice.Value).ToList();
|
||||
|
||||
public void Roll() => _dices.ForEach(dice => dice.Roll());
|
||||
}
|
||||
|
@ -19,14 +19,12 @@ public interface IActionService
|
||||
|
||||
public class ActionService : IActionService
|
||||
{
|
||||
private readonly IDiceCup _diceCup;
|
||||
private readonly GameService _gameService;
|
||||
private readonly ILogger<ActionService> _logger;
|
||||
|
||||
public ActionService(ILogger<ActionService> logger, GameService gameService)
|
||||
{
|
||||
_logger = logger;
|
||||
_diceCup = new DiceCup();
|
||||
_gameService = gameService;
|
||||
}
|
||||
|
||||
@ -48,7 +46,8 @@ public class ActionService : IActionService
|
||||
|
||||
public List<int> RollDice()
|
||||
{
|
||||
var rolls = _diceCup.Roll;
|
||||
Group?.DiceCup.Roll();
|
||||
var rolls = Group?.DiceCup.Values ?? new List<int>();
|
||||
_logger.Log(LogLevel.Information, "Rolled [{}]", string.Join(", ", rolls));
|
||||
|
||||
return rolls;
|
||||
|
@ -18,6 +18,8 @@ public class Game // TODO handle disconnects and reconnects
|
||||
|
||||
[JsonIgnore] private Queue<DirectionalPosition> Spawns { get; }
|
||||
|
||||
[JsonIgnore] public DiceCup DiceCup { get; } = new();
|
||||
|
||||
[JsonInclude] public int Count => Players.Count;
|
||||
|
||||
[JsonInclude]
|
||||
|
Loading…
x
Reference in New Issue
Block a user