This commit is contained in:
Christian Kauer 2023-12-22 13:55:45 +01:00
parent 073fdda625
commit 64e009d401
18 changed files with 443 additions and 25 deletions

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameHandler.UnitTests
{
[TestFixture]
internal class GameServiceTests
{
[Test]
public void Start_StartsANewGame()
{
GameService service = new GameService();
service.Start();
}
}
}

View File

@ -172,9 +172,6 @@ namespace GameHandler.DeathGame
coffins.Insert(idx, previousUpdated);
}
var result = gm with { Coffins = coffins.ToArray(), Id = gm.Id + 1 };
return result;

View File

@ -0,0 +1,54 @@
using GameModel;
using GameModel.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameHandler
{
public class GameService
{
private bool _isStarted = false;
private ThrowHandler _th;
private BoardState _lastState;
public BoardState Start()
{
if (_isStarted)
{
throw new InvalidGameStateExcpetion("Game already started");
}
if (!_isStarted)
{
_isStarted = true;
}
_th = new ThrowHandler();
_lastState = BoardState.Create(ThrowMode.Decrease);
return _lastState;
}
public BoardState HandleThrow(PinThrow pinThrow)
{
if (!_isStarted)
{
throw new InvalidGameStateExcpetion("Game not started");
}
_lastState = _th.Update(_lastState, pinThrow);
return _lastState;
}
public void Stop()
{
if (!_isStarted)
{
throw new InvalidGameStateExcpetion("Game not started");
}
_isStarted = false;
}
}
}

View File

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameHandler
{
internal class MainHandler
{
}
}

View File

@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GameModel\GameModel.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1 @@
global using NUnit.Framework;

View File

@ -0,0 +1,20 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameModel.UnitTests.Helper
{
public static class IEnumerableExtension
{
public static IEnumerable AsWeakEnumerable(this IEnumerable source)
{
foreach (object o in source)
{
yield return o;
}
}
}
}

View File

@ -1,4 +1,4 @@
using GameHandler.UnitTests.Helper;
using GameModel.UnitTests.Helper;
using GameModel;
using GameModel.Exceptions;
using NuGet.Frameworks;
@ -6,7 +6,7 @@ using NUnit.Framework.Legacy;
using System.Collections;
using System.Numerics;
namespace GameHandler.UnitTests
namespace GameModel.UnitTests
{
[TestFixture]
internal class PinPictureTests

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameHandler.UnitTests
namespace GameModel.UnitTests
{
[TestFixture]
internal class PinThrowTests

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameModel.Exceptions
{
public class InvalidGameStateExcpetion : KoogleException
{
public InvalidGameStateExcpetion(string message) : base(message, null)
{
}
}
}

View File

@ -195,5 +195,27 @@ namespace GameModel
var states = new List<PinState>(new[] { PinState.Down, PinState.Down, PinState.Down, PinState.Down, PinState.Up, PinState.Down, PinState.Down, PinState.Down, PinState.Down }).ToArray();
return PinPicture.Create(states);
}
internal static PinPicture Create(string pindata)
{
if (!int.TryParse(pindata, out int dummy))
{
throw new InvalidDataException($"{pindata} cannot be parsed as throw");
}
var states = new[] {
pindata.Contains("1") ? PinState.Down : PinState.Up,
pindata.Contains("2") ? PinState.Down : PinState.Up,
pindata.Contains("3") ? PinState.Down : PinState.Up,
pindata.Contains("4") ? PinState.Down : PinState.Up,
pindata.Contains("5") ? PinState.Down : PinState.Up,
pindata.Contains("6") ? PinState.Down : PinState.Up,
pindata.Contains("7") ? PinState.Down : PinState.Up,
pindata.Contains("8") ? PinState.Down : PinState.Up,
pindata.Contains("9") ? PinState.Down : PinState.Up,
};
return PinPicture.Create(states);
}
}
}

View File

@ -19,9 +19,9 @@ namespace GameModel
public bool IsNoWood => PicPicture.DownCount == 0 && !IsSink;
public static PinThrow Create(int PlayerId, PinPicture PicPicture, bool IsBell, bool IsSink)
public static PinThrow Create(int playerId, PinPicture picPicture, bool isBell, bool isSink)
{
return new PinThrow(PlayerId, PicPicture, IsBell, IsSink);
return new PinThrow(playerId, picPicture, isBell, isSink);
}
public static PinThrow Create(int PlayerId, bool IsBell, bool IsSink)
@ -29,5 +29,16 @@ namespace GameModel
var p = PinPicture.Create();
return Create(PlayerId, p, IsBell, IsSink);
}
public static PinThrow Create(string pindata, bool isBell, bool isSink, int playerId)
{
if (!int.TryParse(pindata, out int dummy))
{
throw new InvalidDataException($"{pindata} cannot be parsed as throw");
}
var pic = PinPicture.Create(pindata);
return PinThrow.Create(playerId, pic, isBell, isSink);
}
}
}

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Spectre.Console" Version="0.48.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GameHandler\GameHandler.csproj" />
<ProjectReference Include="..\GameModel\GameModel.csproj" />
</ItemGroup>
</Project>

16
KoogleCli/Model/Option.cs Normal file
View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KoogleCli.Model
{
internal record Option(string Name,Action Action)
{
public override string ToString()
{
return Name;
}
}
}

View File

@ -0,0 +1,13 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KoogleCli.Model
{
public record ThrowData(int PlayerId, string Pindata, bool Bell, bool Sink, bool Abort)
{
}
}

View File

@ -0,0 +1,25 @@
using KoogleCli.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace KoogleCli.Parser
{
public static class ThrowDataParser
{
public static ThrowData Parse(string data)
{
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException("data");
var player = data.Substring(0, 1);
var playerId = int.Parse(player);
var pindata = data.Substring(2, data.Length -2);
return new ThrowData(playerId, Regex.Match(pindata, @"\d+").Value, data.Contains("b"), data.Contains("s"), data.Contains("e"));
}
}
}

176
KoogleCli/Program.cs Normal file
View File

@ -0,0 +1,176 @@
// See https://aka.ms/new-console-template for more information
using CommandLine;
using GameHandler;
using GameModel;
using KoogleCli.Model;
using KoogleCli.Parser;
using Spectre.Console;
using static System.Runtime.InteropServices.JavaScript.JSType;
AnsiConsole.MarkupLine("Welcome to [green]koogle[/]");
do
{
var option = AnsiConsole.Prompt(
new SelectionPrompt<Option>()
.Title("Was willst du tun?")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down)[/]")
.AddChoices(new[] {
new Option("Neues Spiel",NewGameAction),
new Option("Stammdaten", MasterDataAction),
new Option("Beenden", null)
}));
option.Action?.Invoke();
if (option.Name == "Beenden")
{
break;
}
} while (true) ;
void MasterDataAction()
{
Console.WriteLine("todo");
}
GameService _gs;
void NewGameAction()
{
_gs = new GameService();
var bs = _gs.Start();
ShowBoard(bs);
do
{
var stringData = AnsiConsole.Prompt(
new TextPrompt<string>("Wurf?")
.PromptStyle("green")
.ValidationErrorMessage("[red]That's not a valid throw[/]")
.Validate(data =>
{
try
{
ThrowDataParser.Parse(data);
}
catch (Exception)
{
return false;
}
return true;
}));
var throwData = ThrowDataParser.Parse(stringData);
if (throwData.Abort)
{
_gs.Stop();
break;
}
bs = _gs.HandleThrow(PinThrow.Create(throwData.Pindata, throwData.Bell, throwData.Sink, throwData.PlayerId));
ShowBoard(bs);
} while (true) ;
}
void ShowBoard(BoardState board)
{
AnsiConsole.Clear();
var grid = new Grid();
grid.AddColumn();
grid.AddColumn();
grid.AddColumn();
grid.AddColumn();
grid.AddColumn();
Padder pad_1, pad_2, pad_3, pad_4, pad_5;
GetRow(1, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
GetRow(2, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
GetRow(3, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
GetRow(4, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
GetRow(5, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
// Write grid and it's padded contents to the Console
AnsiConsole.Write(grid);
};
static void GetRow(int row, out Padder pad_1, out Padder pad_2, out Padder pad_3, out Padder pad_4, out Padder pad_5, BoardState board)
{
string txt1 = "0";
string txt2 = "0";
string txt3 = "0";
string txt4 = "0";
string txt5 = "0";
if (row == 1)
{
txt1 = " ";
txt2 = " ";
txt3 = board.PinPicture.PinState1 == PinState.Down ? "0" : "1";
txt4 = " ";
txt5 = " ";
}
if (row == 2)
{
txt1 = " ";
txt2 = board.PinPicture.PinState2 == PinState.Down ? "0" : "2";
txt3 = " ";
txt4 = board.PinPicture.PinState3 == PinState.Down ? "0" : "3";
txt5 = " ";
}
if (row == 3)
{
txt1 = board.PinPicture.PinState4 == PinState.Down ? "0" : "4";
txt2 = " ";
txt3 = board.PinPicture.PinState5 == PinState.Down ? "0" : "5";
txt4 = " ";
txt5 = board.PinPicture.PinState6 == PinState.Down ? "0" : "6";
}
if (row == 4)
{
txt1 = " ";
txt2 = board.PinPicture.PinState7 == PinState.Down ? "0" : "7";
txt3 = " ";
txt4 = board.PinPicture.PinState8 == PinState.Down ? "0" : "8";
txt5 = " ";
}
if (row == 5)
{
txt1 = " ";
txt2 = " ";
txt3 = board.PinPicture.PinState9 == PinState.Down ? "0" : "9";
txt4 = " ";
txt5 = " ";
}
// Create three text elements
var paddedText_1 = new Text(txt1, new Style(txt1 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
var paddedText_2 = new Text(txt2, new Style(txt2 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
var paddedText_3 = new Text(txt3, new Style(txt3 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
var paddedText_4 = new Text(txt4, new Style(txt4 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
var paddedText_5 = new Text(txt5, new Style(txt5 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
// Apply padding to the three text elements
pad_1 = new Padder(paddedText_1); //.PadRight(16).PadBottom(0).PadTop(4);
pad_2 = new Padder(paddedText_2); //.PadBottom(0).PadTop(2);
pad_3 = new Padder(paddedText_3); //.PadLeft(16).PadBottom(0).PadTop(0);
pad_4 = new Padder(paddedText_4); //.PadLeft(16).PadBottom(0).PadTop(0);
pad_5 = new Padder(paddedText_5); //.PadLeft(16).PadBottom(0).PadTop(0);
}

View File

@ -3,13 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34202.233
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameHandler.UnitTests", "GameHandler.UnitTests\GameHandler.UnitTests.csproj", "{E2F3CE36-0051-4C9A-B3FF-0BB44292B756}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GameHandler.UnitTests", "GameHandler.UnitTests\GameHandler.UnitTests.csproj", "{E2F3CE36-0051-4C9A-B3FF-0BB44292B756}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameHandler", "GameHandler\GameHandler.csproj", "{4A541722-86AD-492F-AADA-CFB4935CDB83}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GameHandler", "GameHandler\GameHandler.csproj", "{4A541722-86AD-492F-AADA-CFB4935CDB83}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameModel", "GameModel\GameModel.csproj", "{9B3CADB6-C335-46D1-B98B-07E73D53E16B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GameModel", "GameModel\GameModel.csproj", "{9B3CADB6-C335-46D1-B98B-07E73D53E16B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameContract", "GameContract\GameContract.csproj", "{68897747-A9D4-4E45-A20C-6AB7E7AB22FD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GameContract", "GameContract\GameContract.csproj", "{68897747-A9D4-4E45-A20C-6AB7E7AB22FD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameModel.UnitTests", "GameModel.UnitTests\GameModel.UnitTests.csproj", "{C752388E-815A-4911-AC75-B6C27337D81A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KoogleCli", "KoogleCli\KoogleCli.csproj", "{3FF45A02-42F9-4E75-993B-6582DD2A22BF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -33,6 +37,14 @@ Global
{68897747-A9D4-4E45-A20C-6AB7E7AB22FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68897747-A9D4-4E45-A20C-6AB7E7AB22FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68897747-A9D4-4E45-A20C-6AB7E7AB22FD}.Release|Any CPU.Build.0 = Release|Any CPU
{C752388E-815A-4911-AC75-B6C27337D81A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C752388E-815A-4911-AC75-B6C27337D81A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C752388E-815A-4911-AC75-B6C27337D81A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C752388E-815A-4911-AC75-B6C27337D81A}.Release|Any CPU.Build.0 = Release|Any CPU
{3FF45A02-42F9-4E75-993B-6582DD2A22BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3FF45A02-42F9-4E75-993B-6582DD2A22BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3FF45A02-42F9-4E75-993B-6582DD2A22BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3FF45A02-42F9-4E75-993B-6582DD2A22BF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE