Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LangtonsAnt und Strange Chessboard in C# #29

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions katas/LangtonAnt/solutions/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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;
}

}


}
}
Original file line number Diff line number Diff line change
@@ -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

}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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();

Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions katas/LangtonAnt/solutions/LangtonsAntClient/LangtonsAntClient.sln
Original file line number Diff line number Diff line change
@@ -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
Loading