From 4785f62650bcd5c9857d6c041af2febef7be4b45 Mon Sep 17 00:00:00 2001 From: Christian Kauer Date: Wed, 20 Dec 2023 16:27:48 +0100 Subject: [PATCH] dev --- GameHandler.UnitTests/PinPictureTests.cs | 8 ++++ GameHandler.UnitTests/ThrowHandlerTests.cs | 44 +++++++++++++++++++++- GameHandler/ThrowHandler.cs | 12 +++++- GameModel/PinPicture.cs | 18 +++++---- 4 files changed, 71 insertions(+), 11 deletions(-) diff --git a/GameHandler.UnitTests/PinPictureTests.cs b/GameHandler.UnitTests/PinPictureTests.cs index d125fa3..734195a 100644 --- a/GameHandler.UnitTests/PinPictureTests.cs +++ b/GameHandler.UnitTests/PinPictureTests.cs @@ -180,5 +180,13 @@ namespace GameHandler.UnitTests Assert.That(p9.DownCount, Is.EqualTo(8)); Assert.That(p10.DownCount, Is.EqualTo(9)); } + + [Test] + public void DownCount_MatchesDownState() + { + var p = PinPicture.Create(2,PinState.Down); + Assert.That(p.DownCount, Is.EqualTo(1)); + Assert.That(p.UpCount, Is.EqualTo(8)); + } } } diff --git a/GameHandler.UnitTests/ThrowHandlerTests.cs b/GameHandler.UnitTests/ThrowHandlerTests.cs index 0f505c6..d483f5b 100644 --- a/GameHandler.UnitTests/ThrowHandlerTests.cs +++ b/GameHandler.UnitTests/ThrowHandlerTests.cs @@ -1,4 +1,5 @@ -using System; +using GameModel; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +7,48 @@ using System.Threading.Tasks; namespace GameHandler.UnitTests { + [TestFixture] public class ThrowHandlerTests { + ThrowHandler _th; + + [SetUp] + public void SetUp() + { + _th = new ThrowHandler(); + } + + [Test] + public void CreateFullPicAfterEachThrowInRepositionMode() + { + var bs = _th.Init(ThrowMode.Reposition); + + var bs2 = _th.Update(bs, new PinThrow(PinPicture.Create(2,PinState.Down),false,false)); + + Assert.That(bs.PinPicture.UpCount, Is.EqualTo(9)); + } + + [Test] + public void SinkThrowReturnsSameGameStateAsBefore() + { + var bs = _th.Init(ThrowMode.Reposition); + + var bs2 = _th.Update(bs, new PinThrow(PinPicture.Create(2, PinState.Down), false, false)); + var bs3 = _th.Update(bs, new PinThrow(PinPicture.Create(2, PinState.Down), false, true)); + + Assert.That(bs2, Is.EqualTo(bs3)); + } + + [Test] + public void CreateFullPicAfterBoardIsClearedInDecreaseMode() + { + var bs = _th.Init(ThrowMode.Decrease); + + var bs2 = _th.Update(bs, new PinThrow(PinPicture.Create(2, PinState.Down), false, false)); + Assert.That(bs2.PinPicture.DownCount, Is.EqualTo(1)); + + var bs3 = _th.Update(bs, new PinThrow(PinPicture.CreateAllPins(), false, false)); + Assert.That(bs3.PinPicture.DownCount, Is.EqualTo(0)); + } } } diff --git a/GameHandler/ThrowHandler.cs b/GameHandler/ThrowHandler.cs index 2168028..386e7c4 100644 --- a/GameHandler/ThrowHandler.cs +++ b/GameHandler/ThrowHandler.cs @@ -37,8 +37,16 @@ namespace GameHandler } else { - return currentState with { PinPicture = currentState.PinPicture + pinThrow }; + var result = currentState with { PinPicture = currentState.PinPicture + pinThrow }; + if (result.PinPicture.UpCount == 0) + { + return currentState with { PinPicture = PinPicture.Create() }; + } + else + { + return result; + } } } } -} \ No newline at end of file +} diff --git a/GameModel/PinPicture.cs b/GameModel/PinPicture.cs index da21035..b0e2db1 100644 --- a/GameModel/PinPicture.cs +++ b/GameModel/PinPicture.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using System.Net.NetworkInformation; +using System.Numerics; using System.Reflection.Metadata.Ecma335; using System.Text; using System.Threading.Tasks; @@ -61,17 +62,12 @@ namespace GameModel // throw new InvalidPinPictureException(); //} //} - - List? _pins = null; + private List pins { get - { - if (_pins == null) - { - _pins = new List(new[] { PinState1, PinState2, PinState3, PinState4, PinState5, PinState6, PinState7, PinState8, PinState9 }); - } - return _pins; + { + return new List(new[] { PinState1, PinState2, PinState3, PinState4, PinState5, PinState6, PinState7, PinState8, PinState9 }); } } @@ -182,5 +178,11 @@ namespace GameModel { return PinPicture.Create(states[0], states[1], states[2], states[3], states[4], states[5], states[6], states[7], states[8]); } + + public static PinPicture CreateAllPins() + { + var states = new List(new[] { PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down }).ToArray(); + return PinPicture.Create(states); + } } }