diff --git a/katas/LangtonAnt/solutions/.gitkeep b/katas/LangtonAnt/solutions/.gitkeep new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/katas/LangtonAnt/solutions/.gitkeep @@ -0,0 +1 @@ + diff --git a/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend.csproj b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend.csproj new file mode 100644 index 0000000..5e963bc --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend/Ant.cs b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend/Ant.cs new file mode 100644 index 0000000..2f34b47 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend/Ant.cs @@ -0,0 +1,71 @@ +using static LangtonsAntAPI.LangtonsAntBackend.Statics; + +namespace LangtonsAntAPI.LangtonsAntBackend +{ + public class Ant + { + public Ant(int iPositionX, int iPositionY, string iDirection) + { + PositionX = iPositionX; + PositionY = iPositionY; + Direction = (EDirection)Enum.Parse(typeof(EDirection), iDirection); + + } + + public int PositionX { get; private set; } + + public int PositionY { get; private set; } + + public EDirection Direction { get; private set; } + + + public void Move(ETurn iTurn) + { + //set new direction + if (iTurn == ETurn.Left) + { + if (Direction == EDirection.n) + { + Direction = EDirection.w; + } + else + { + Direction--; + + } + + } + else if (iTurn == ETurn.Right) + { + if (Direction == EDirection.w) + { + Direction = EDirection.n; + } + else + { + Direction++; + } + } + + //set new Position + switch (Direction) + { + case EDirection.n: + PositionY--; + break; + case EDirection.o: + PositionX++; + break; + case EDirection.s: + PositionY++; + break; + case EDirection.w: + PositionX--; + break; + } + + } + + + } +} diff --git a/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend/LangtonsAntMain.cs b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend/LangtonsAntMain.cs new file mode 100644 index 0000000..25fc390 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend/LangtonsAntMain.cs @@ -0,0 +1,139 @@ +using System.ComponentModel.DataAnnotations; +using static LangtonsAntAPI.LangtonsAntBackend.Statics; + +namespace LangtonsAntAPI.LangtonsAntBackend +{ + public class LangtonsAntMain + { + #region Properties & Fields + #region StartParams + public int EdgeLength { get; set; } + + public int NumberOfSteps { get; set; } + + public int StartX { get; set; } + + public int StartY { get; set; } + + public string StartDirection { get; set; } + + #endregion StartParams + + private bool[,]? _field; //true = black, false = white + + private Ant? _ant; + public int StepCounter { get; set; } + + public string ResultText { get; set; } + + public string ErrMessage { get; set; } + #endregion Properties + + #region Methods + + + + public void Initialize() + { + //Todo: Check Parameters first + _field = new bool[EdgeLength, EdgeLength]; + _ant = new Ant(iPositionX: StartX, iPositionY: StartY, iDirection: StartDirection); + StepCounter = 0; + GenerateErrorText(); + GenerateResultString(); + } + + + public void NextStep() + { + if (_ant == null || _field == null) + return; + + GenerateErrorText(); + if (!String.IsNullOrEmpty(ErrMessage)) + return; + + bool isBlack = _field[_ant.PositionX, _ant.PositionY]; + + //Invert Square Color + _field[_ant.PositionX, _ant.PositionY] = !_field[_ant.PositionX, _ant.PositionY]; + + //Move Ant + if (isBlack) + { + _ant.Move(ETurn.Left); + } + else + { + _ant.Move(ETurn.Right); + } + + StepCounter++; + GenerateResultString(); + } + + public void GenerateResultString() + { + if (_ant == null || _field == null) + return; + ResultText = String.Empty; + for (int y = 0; y < EdgeLength; y++) + { + for (int x = 0; x < EdgeLength; x++) + { + //Write Ant + if (_ant.PositionX == x && _ant.PositionY == y) + { + ResultText += _ant.Direction; + } + //Write Field Color + if (_field[x, y]) + { + ResultText += "s,"; + } + else + { + ResultText += "w,"; + } + + } + } + } + + + private void GenerateErrorText() + { + ErrMessage = String.Empty; + if (IsOutOfBounds()) + { + ErrMessage = "Der Rand wurde erreicht!"; + } + + if (IsOver()) + { + ErrMessage += "Die finale Anzahl der Schritte wurde erreicht!"; + } + + } + + public bool IsOutOfBounds() + { + if (_ant == null) + return false; + if (_ant.PositionX >= EdgeLength || _ant.PositionY >= EdgeLength || _ant.PositionX < 0 || _ant.PositionY < 0) + return true; + return false; + } + private bool IsOver() + { + if (StepCounter >= NumberOfSteps) + return true; + return false; + } + + + + #endregion Methods + + } +} diff --git a/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend/Statics.cs b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend/Statics.cs new file mode 100644 index 0000000..a458964 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/LangtonsAntBackend/Statics.cs @@ -0,0 +1,17 @@ +namespace LangtonsAntAPI.LangtonsAntBackend +{ + public static class Statics + { + public enum EDirection + { + n,//North + o,//East + s,//South + w//West + } + public enum ETurn + { + Left, Right + } + } +} diff --git a/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/Program.cs b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/Program.cs new file mode 100644 index 0000000..b69e00e --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/Program.cs @@ -0,0 +1,48 @@ +using LangtonsAntAPI.LangtonsAntBackend; +using System; +using System.Collections.ObjectModel; +using System.Runtime.CompilerServices; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +var app = builder.Build(); + +// Configure the HTTP request pipeline. + +LangtonsAntMain langtonsAntMain = new(); + +app.MapGet("api/langtonsant/", () => +{ + langtonsAntMain.NextStep(); + return Results.Created($"api/langtonsant/", langtonsAntMain); +}); + + +app.MapPost("api/langtonsant", (LangtonsAntMain iLangtonsAntMain) => +{ + return CreateNewGame(iLangtonsAntMain); +}); + +app.MapPut("api/langtonsant", (LangtonsAntMain iLangtonsAntMain) => +{ + return CreateNewGame(iLangtonsAntMain); +}); + +app.MapDelete("api/langtonsant/", () => +{ + langtonsAntMain = new LangtonsAntMain(); + return Results.Ok(); + +}); + +IResult CreateNewGame(LangtonsAntMain iLangtonsAntMain) +{ + langtonsAntMain = iLangtonsAntMain; + langtonsAntMain.Initialize(); + return Results.Created($"api/langtonsant/", langtonsAntMain); +} + +app.Run(); + diff --git a/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/Properties/launchSettings.json b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/Properties/launchSettings.json new file mode 100644 index 0000000..484584b --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:56246", + "sslPort": 44300 + } + }, + "profiles": { + "LangtonsAnt": { + "commandName": "Project", + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7213;http://localhost:5128", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/appsettings.Development.json b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/appsettings.json b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAnt/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAntBackend.sln b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAntBackend.sln new file mode 100644 index 0000000..594914f --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntBackend/LangtonsAntBackend.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33723.286 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangtonsAntBackend", "LangtonsAnt\LangtonsAntBackend.csproj", "{E2FC832A-788C-4BFB-B271-CD59DC38D2D1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E2FC832A-788C-4BFB-B271-CD59DC38D2D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E2FC832A-788C-4BFB-B271-CD59DC38D2D1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E2FC832A-788C-4BFB-B271-CD59DC38D2D1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E2FC832A-788C-4BFB-B271-CD59DC38D2D1}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F790C48A-0E1E-4F1E-A713-DABA34D96906} + EndGlobalSection +EndGlobal diff --git a/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient.sln b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient.sln new file mode 100644 index 0000000..9a5f606 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient.sln @@ -0,0 +1,27 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31611.283 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LangtonsAntClient", "LangtonsAntClient\LangtonsAntClient.csproj", "{29C6FC97-B7C7-4A8C-A8FC-CE27768D00A6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {29C6FC97-B7C7-4A8C-A8FC-CE27768D00A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {29C6FC97-B7C7-4A8C-A8FC-CE27768D00A6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {29C6FC97-B7C7-4A8C-A8FC-CE27768D00A6}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {29C6FC97-B7C7-4A8C-A8FC-CE27768D00A6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {29C6FC97-B7C7-4A8C-A8FC-CE27768D00A6}.Release|Any CPU.Build.0 = Release|Any CPU + {29C6FC97-B7C7-4A8C-A8FC-CE27768D00A6}.Release|Any CPU.Deploy.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {61F7FB11-1E47-470C-91E2-47F8143E1572} + EndGlobalSection +EndGlobal diff --git a/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/App.xaml b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/App.xaml new file mode 100644 index 0000000..c7a5de7 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/App.xaml @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/App.xaml.cs b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/App.xaml.cs new file mode 100644 index 0000000..d0d9dce --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/App.xaml.cs @@ -0,0 +1,11 @@ +namespace LangtonsAntClient; + +public partial class App : Application +{ + public App() + { + InitializeComponent(); + + MainPage = new AppShell(); + } +} diff --git a/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/AppShell.xaml b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/AppShell.xaml new file mode 100644 index 0000000..654025b --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/AppShell.xaml @@ -0,0 +1,14 @@ + + + + + + diff --git a/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/AppShell.xaml.cs b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/AppShell.xaml.cs new file mode 100644 index 0000000..8dcdab1 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/AppShell.xaml.cs @@ -0,0 +1,9 @@ +namespace LangtonsAntClient; + +public partial class AppShell : Shell +{ + public AppShell() + { + InitializeComponent(); + } +} diff --git a/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/DataService/RestDataService.cs b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/DataService/RestDataService.cs new file mode 100644 index 0000000..f60a5b4 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/DataService/RestDataService.cs @@ -0,0 +1,90 @@ +using LangtonsAntClient.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace LangtonsAntClient.DataService +{ + class RestDataService + { + private readonly HttpClient _httpClient; + private readonly string _baseAddress; + private readonly string _url; + private readonly JsonSerializerOptions _jsonSerializerOptions; + + public RestDataService() + { + _httpClient = new HttpClient(); + + _baseAddress = "http://localhost:5128"; + _url = $"{_baseAddress}/api/langtonsant"; + + _jsonSerializerOptions = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + } + + + + public async Task InitializeLangtonsAntBackend(LangtonsAnt iLangtonsAnt) + { + if (Connectivity.Current.NetworkAccess != NetworkAccess.Internet) + { + iLangtonsAnt.ErrMessage = "Keine Internetverbindung"; + return iLangtonsAnt; + } + + try + { + string jsonLangtonsAnt = JsonSerializer.Serialize(iLangtonsAnt, _jsonSerializerOptions); + StringContent content = new StringContent(jsonLangtonsAnt, Encoding.UTF8, "application/json"); + + HttpResponseMessage response = await _httpClient.PostAsync($"{_url}", content); + + if (response.IsSuccessStatusCode) + { + string responseString = await response.Content.ReadAsStringAsync(); + iLangtonsAnt = JsonSerializer.Deserialize(responseString, _jsonSerializerOptions); + } + } + catch (Exception ex) + { + iLangtonsAnt.ErrMessage = $"Exception: {ex.Message}"; + } + + return iLangtonsAnt; + + } + public async Task GetNextStepBackend() + { + LangtonsAnt langtonsAnt = new LangtonsAnt(); + if (Connectivity.Current.NetworkAccess != NetworkAccess.Internet) + { + langtonsAnt.ErrMessage = "Keine Internetverbindung"; + return langtonsAnt; + } + + try + { + HttpResponseMessage response = await _httpClient.GetAsync($"{_url}/"); + + if (response.IsSuccessStatusCode) + { + string responseString = await response.Content.ReadAsStringAsync(); + langtonsAnt = JsonSerializer.Deserialize(responseString, _jsonSerializerOptions); + } + } + catch (Exception ex) + { + langtonsAnt.ErrMessage = $"Exception: {ex.Message}"; + } + + return langtonsAnt; + } + } +} diff --git a/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/LangtonsAntClient.csproj b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/LangtonsAntClient.csproj new file mode 100644 index 0000000..9e73636 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/LangtonsAntClient.csproj @@ -0,0 +1,54 @@ + + + + net6.0-android;net6.0-ios;net6.0-maccatalyst + $(TargetFrameworks);net6.0-windows10.0.19041.0 + + + Exe + LangtonsAntClient + true + true + enable + + + LangtonsAntClient + + + com.companyname.langtonsantclient + d64117ab-9621-41cd-8b04-abd79d8112a0 + + + 1.0 + 1 + + 14.2 + 14.0 + 21.0 + 10.0.17763.0 + 10.0.17763.0 + 6.5 + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/MainPage.xaml b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/MainPage.xaml new file mode 100644 index 0000000..96d23a8 --- /dev/null +++ b/katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient/MainPage.xaml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Norden + Osten + Süden + Westen + + + +