add deathgame unittests

This commit is contained in:
Christian Kauer 2023-12-18 21:11:13 +01:00
parent e477f42f82
commit 44fafd4e28
3 changed files with 113 additions and 10 deletions

View File

@ -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()
{
}
}
}

View File

@ -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)

View File

@ -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
/// <summary>
/// Sink at first throw in round
/// </summary>
FullSink, // Gosse beim Anwurf
Eliminated, // Ausgeschieden
Absent, // Am Spieltag nicht teilgenommen
ExpensePoint // Strafpunkt, z.B. im "Scheißspiel"
}
}