From 12c9c14b97dafd2efaba6789b3912b463b99a2ce Mon Sep 17 00:00:00 2001 From: Christian Kauer Date: Sun, 24 Dec 2023 10:33:50 +0100 Subject: [PATCH] dev --- GameHandler/GameHandler/DeathGameHandler.cs | 11 +++++++++- GameHandler/GameHandler/FreeGameHandler.cs | 8 ++++++- GameHandler/GameService.cs | 9 +++++++- GameModel.UnitTests/PinThrowTests.cs | 24 +++++++++++++++++++++ GameModel/Contracts/IGameHandler.cs | 2 ++ GameModel/PinThrow.cs | 2 ++ KoogleCli/Program.cs | 13 +++++------ 7 files changed, 58 insertions(+), 11 deletions(-) diff --git a/GameHandler/GameHandler/DeathGameHandler.cs b/GameHandler/GameHandler/DeathGameHandler.cs index 58eb01c..678e8b3 100644 --- a/GameHandler/GameHandler/DeathGameHandler.cs +++ b/GameHandler/GameHandler/DeathGameHandler.cs @@ -73,6 +73,13 @@ namespace GameHandler.DeathGame return new DeathGameModel(1, coffins.OrderBy(_ => random.Next()).ToList().ToArray(), deathGameSettings); } + public IGameModel Update(PinThrow pinThrow, IGameModel gameModel, BoardState boardStateBeforeUpdate) + { + var isTrowIntoAllPins = boardStateBeforeUpdate.PinPicture.UpCount == 9; + var boardCleared = pinThrow.IsCleared; + return CalcNextModel(gameModel as DeathGameModel, pinThrow.PinCount, isTrowIntoAllPins, boardCleared); + } + public DeathGameModel CalcNextModel(DeathGameModel gm, int pinCount, bool isTrowIntoAllPins, bool boardCleared) { var current = gm.Coffins.First(); @@ -200,6 +207,8 @@ namespace GameHandler.DeathGame return gm.Coffins.First().PlayerId; } - public int ThrowsPerRount() => 3; + public int ThrowsPerRount() => IGameHandler.INFINIT_THROWS; + + } } diff --git a/GameHandler/GameHandler/FreeGameHandler.cs b/GameHandler/GameHandler/FreeGameHandler.cs index 2ddce14..54a9837 100644 --- a/GameHandler/GameHandler/FreeGameHandler.cs +++ b/GameHandler/GameHandler/FreeGameHandler.cs @@ -1,4 +1,5 @@ -using GameModel.Contract; +using GameModel; +using GameModel.Contract; using GameModel.Contracts; using GameModel.FreeGame; using System; @@ -27,5 +28,10 @@ namespace GameHandler.GameHandler { return new FreeGameModel(playerIds); } + + public IGameModel Update(PinThrow pinThrow, IGameModel gameModel, BoardState boardStateBeforeUpdate) + { + return gameModel; // TODO update model + } } } diff --git a/GameHandler/GameService.cs b/GameHandler/GameService.cs index 25c5451..cbd0401 100644 --- a/GameHandler/GameService.cs +++ b/GameHandler/GameService.cs @@ -12,6 +12,7 @@ using GameHandler.Parser; using GameModel.Contract; using GameModel.DeathGame; using GameModel.Contracts; +using System.Data; namespace GameHandler { @@ -72,10 +73,16 @@ namespace GameHandler throw new InvalidGameStateExcpetion("Game not started"); } + var boardStateBeforeUpdate = _lastState.ThrowState.BoardState; + pinThrow = AutoCompletePlayerId(pinThrow); var throwState = _th.Update(_lastState.ThrowState, pinThrow); - _lastState = _lastState with { ThrowState = throwState }; + var gameModel = _gh.Update(pinThrow, _lastState.GameModel, boardStateBeforeUpdate); + // todo: update expense model + + + _lastState = _lastState with { ThrowState = throwState, GameModel = gameModel }; return _lastState ; } diff --git a/GameModel.UnitTests/PinThrowTests.cs b/GameModel.UnitTests/PinThrowTests.cs index 047b38f..0154e6f 100644 --- a/GameModel.UnitTests/PinThrowTests.cs +++ b/GameModel.UnitTests/PinThrowTests.cs @@ -23,6 +23,30 @@ namespace GameModel.UnitTests Assert.That(t.PinCount, Is.EqualTo(9)); } + [Test] + public void IsCleared_IsFalseWhenAtLeastOnePinIsUp() + { + var pic = new PinPicture(PinState.Down, PinState.Down, PinState.Up, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down); + var t = PinThrow.Create(1, pic, false, true); + Assert.That(t.IsCleared, Is.False); + } + + [Test] + public void IsCleared_IsTrueWhenAllPinsAreDown() + { + var pic = new PinPicture(PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down); + var t = PinThrow.Create(1, pic, false, false); + Assert.That(t.IsCleared, Is.True); + } + + [Test] + public void IsCleared_IsFalseWhenAllPinsAreDownButSinkIsTrue() + { + var pic = new PinPicture(PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Down); + var t = PinThrow.Create(1, pic, false, true); + Assert.That(t.IsCleared, Is.False); + } + [Test] public void SinkThrow_IsNothing() { diff --git a/GameModel/Contracts/IGameHandler.cs b/GameModel/Contracts/IGameHandler.cs index cd14aaa..3d2a9b1 100644 --- a/GameModel/Contracts/IGameHandler.cs +++ b/GameModel/Contracts/IGameHandler.cs @@ -21,6 +21,8 @@ namespace GameModel.Contract bool FreePlayerSelection() => true; IGameModel InitGameModel(int[] playerIds, IGameSettings gameSettings); int GetCurrentPlayerId(IGameModel gameModel); + + IGameModel Update(PinThrow pinThrow, IGameModel gameModel, BoardState boardStateBeforeUpdate); } } diff --git a/GameModel/PinThrow.cs b/GameModel/PinThrow.cs index f6cdd63..7eedef5 100644 --- a/GameModel/PinThrow.cs +++ b/GameModel/PinThrow.cs @@ -19,6 +19,8 @@ namespace GameModel public bool IsNoWood => PicPicture.DownCount == 0 && !IsSink; + public bool IsCleared => PicPicture.UpCount == 0 && !IsSink; + public static PinThrow Create(int playerId, PinPicture picPicture, bool isBell, bool isSink) { return new PinThrow(playerId, picPicture, isBell, isSink); diff --git a/KoogleCli/Program.cs b/KoogleCli/Program.cs index 26535f0..f2c3ef1 100644 --- a/KoogleCli/Program.cs +++ b/KoogleCli/Program.cs @@ -132,17 +132,14 @@ void Show(GameState gameState) ShowPlayer(); - ShowThrow(gameState.ThrowState); + ShowThrow(gameState); ShowBoard(gameState.ThrowState.BoardState); - - - } -void ShowThrow(ThrowState throwState) +void ShowThrow(GameState gameState) { - int throwCount = throwState.ThrowCount; - int throwsPerRount = throwState.ThrowsPerRount; + int throwCount = gameState.ThrowState.ThrowCount; + int throwsPerRount = gameState.ThrowState.ThrowsPerRount; Text text1; if (throwsPerRount == IGameHandler.INFINIT_THROWS) { @@ -155,7 +152,7 @@ void ShowThrow(ThrowState throwState) var text1_2 = new Text($"Modus: {_gs.ThrowModeName}", new Style(Color.Red, Color.Black)); // Create three text elements - var paddedText_II = new Text("Spieler: ", new Style(Color.Green, Color.Black)); + var paddedText_II = new Text($"Spieler: {gameState.NextPlayerId}", new Style(Color.Green, Color.Black)); var paddedText_III = new Text("Spielzeit: ", new Style(Color.Blue, Color.Black)); // Apply padding to the three text elements