From 44fafd4e285dc3f47c7dbd0c809c50969f7cdd38 Mon Sep 17 00:00:00 2001 From: Christian Kauer Date: Mon, 18 Dec 2023 21:11:13 +0100 Subject: [PATCH] add deathgame unittests --- .../DeathGame/DeathGameHandlerTests.cs | 79 ++++++++++++++++++- GameHandler/DeathGame/DeathGameHandler.cs | 19 +++-- GameModel/ExpenseTrigger.cs | 25 ++++++ 3 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 GameModel/ExpenseTrigger.cs diff --git a/GameHandler.UnitTests/DeathGame/DeathGameHandlerTests.cs b/GameHandler.UnitTests/DeathGame/DeathGameHandlerTests.cs index 4a5f9ed..2cad44d 100644 --- a/GameHandler.UnitTests/DeathGame/DeathGameHandlerTests.cs +++ b/GameHandler.UnitTests/DeathGame/DeathGameHandlerTests.cs @@ -1,4 +1,5 @@ using GameHandler.DeathGame; +using GameModel; using GameModel.DeathGame; using NuGet.Frameworks; using System; @@ -72,8 +73,11 @@ namespace GameHandler.UnitTests.DeathGame public void InvalidFirstThrow_RaisesExpense() { var gm = _gh.InitGameModel(_players, _settings); - - var gmAfter = _gh.CalcNextModel(gm, 1, false, false); + ExpenseTrigger tr = ExpenseTrigger.Circle; + _gh.ExpenseTriggered += (trigger) => tr = trigger; + var newGm = _gh.CalcNextModel(gm, 2, false, true); + + Assert.That(tr, Is.EqualTo(ExpenseTrigger.FirstThrowFail)); } @@ -95,6 +99,31 @@ namespace GameHandler.UnitTests.DeathGame Assert.That(newGm.Coffins.Last().XCount, Is.EqualTo(1)); } + [Test] + public void AfterThrowWithoutPins_SomeFirstPlayerHasAnLine() + { + var gm = _gh.InitGameModel(_players, _settings); + + var newGm = _gh.CalcNextModel(gm, 0, false, false); + Assert.That(newGm.Coffins.Last().XCount, Is.EqualTo(1)); + } + + [Test] + public void ThirdX_ChangesToAddedLineAndZeroX() + { + var gm = _gh.InitGameModel(_players, _settings); + var coffins = gm.Coffins.ToList(); + var first = gm.Coffins.First() with { XCount = 2 }; + coffins.RemoveAt(0); + coffins.Insert(0, first); + var gm2 = gm with { Coffins = coffins.ToArray(), Id = 2 }; + + var newGm = _gh.CalcNextModel(gm2, 2, true, true); + + Assert.That(newGm.Coffins.Last().XCount, Is.EqualTo(0)); + Assert.That(newGm.Coffins.Last().LineCount, Is.EqualTo(first.LineCount + 1)); + } + [Test] public void FirstPlayerIsLast_AfterModelUpdate() { @@ -120,5 +149,51 @@ namespace GameHandler.UnitTests.DeathGame Assert.That(prevNew.LineCount, Is.EqualTo(prev.LineCount + 1)); } + + [Test] + public void LineLimit_CausesDeath() + { + var gm = _gh.InitGameModel(_players, _settings); + Coffin? deadCoffin = null; + _gh.CoffinCompleted += (c) => deadCoffin = c; + + var coffins = gm.Coffins.ToList(); + var first = gm.Coffins.First() with { LineCount = _settings.MaxCoffinSize }; + coffins.RemoveAt(0); + coffins.Insert(0, first); + var gm2 = gm with { Coffins = coffins.ToArray(), Id = 2 }; + + var newGm = _gh.CalcNextModel(gm2, 0, false, false); + + + + Assert.That(deadCoffin.PlayerId, Is.EqualTo(first.PlayerId)); + } + + [Test] + public void PreviousPlayerIsDead_WhenCurrentClearsBoard() + { + var gm = _gh.InitGameModel(_players, _settings); + Coffin? deadCoffin = null; + _gh.CoffinCompleted += (c) => deadCoffin = c; + + var coffins = gm.Coffins.ToList(); + var last = gm.Coffins.Last() with { LineCount = _settings.MaxCoffinSize }; + coffins.RemoveAt(gm.Coffins.Length-1); + coffins.Insert(gm.Coffins.Length - 1, last); + var gm2 = gm with { Coffins = coffins.ToArray(), Id = 2 }; + + var newGm = _gh.CalcNextModel(gm2, 2, false, true); + + + + Assert.That(deadCoffin.PlayerId, Is.EqualTo(last.PlayerId)); + } + + [Test] + public void NexxtPlayerIsDead_WhenCurrentCausesThirdX() + { + + } } } diff --git a/GameHandler/DeathGame/DeathGameHandler.cs b/GameHandler/DeathGame/DeathGameHandler.cs index cb569df..8103a2c 100644 --- a/GameHandler/DeathGame/DeathGameHandler.cs +++ b/GameHandler/DeathGame/DeathGameHandler.cs @@ -1,4 +1,5 @@ -using GameModel.DeathGame; +using GameModel; +using GameModel.DeathGame; using System; using System.Collections.Generic; using System.Linq; @@ -8,6 +9,7 @@ using System.Threading.Tasks; namespace GameHandler.DeathGame { public delegate void CoffinCompleted(Coffin coffin); + public delegate void ExpenseTriggered(ExpenseTrigger expenseType); public class DeathGameHandler { const int MIN_PLAYER_COUNT = 3; @@ -16,6 +18,7 @@ namespace GameHandler.DeathGame const int MAX_COFFIN_SIZE = 12; public event CoffinCompleted CoffinCompleted; + public event ExpenseTriggered ExpenseTriggered; private static Random random = new Random(); @@ -70,6 +73,7 @@ namespace GameHandler.DeathGame incX = true; if (pinCount < 3) { + ExpenseTriggered?.Invoke(ExpenseTrigger.FirstThrowFail); incLine = true; } } @@ -95,13 +99,6 @@ namespace GameHandler.DeathGame var xCount = current.XCount; var lineCount = current.LineCount; - - if ((current.XCount == 2) && incX) - { - xCount = 0; - lineCount++; - } - if (incLine) { lineCount++; @@ -111,6 +108,12 @@ namespace GameHandler.DeathGame { xCount++; } + + if (xCount == 3) + { + xCount = 0; + lineCount++; + } currentUpdated = current with { LineCount = lineCount, XCount = xCount }; if (lineCount > gm.deathGameSettings.MaxCoffinSize) diff --git a/GameModel/ExpenseTrigger.cs b/GameModel/ExpenseTrigger.cs new file mode 100644 index 0000000..e75ae70 --- /dev/null +++ b/GameModel/ExpenseTrigger.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GameModel +{ + public enum ExpenseTrigger + { + Sink, // Gosse + Bell, // Klingel + NoWood, // kein Holz + FirstThrowFail, // Anwurffehler + Circle, // Kranz + NinePins, // alle Neune + /// + /// Sink at first throw in round + /// + FullSink, // Gosse beim Anwurf + Eliminated, // Ausgeschieden + Absent, // Am Spieltag nicht teilgenommen + ExpensePoint // Strafpunkt, z.B. im "Scheißspiel" + } +}