Skip to content
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
26 changes: 26 additions & 0 deletions katas/LangtonAnt/solutions/steveDilling/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Backend/bin/Debug/net6.0/Backend.dll",
"args": [],
"cwd": "${workspaceFolder}/Backend",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "integratedTerminal",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions katas/LangtonAnt/solutions/steveDilling/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Backend/Backend.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Backend/Backend.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Backend/Backend.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
75 changes: 75 additions & 0 deletions katas/LangtonAnt/solutions/steveDilling/Backend/Ant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace Backend;

public class Ant
{
public Ant(AntDirection direction, int xPos, int yPos)
{
Direction = direction;
XPos = xPos;
YPos = yPos;
}
public AntDirection Direction { private set; get; }
public int XPos { private set; get; }
public int YPos { private set; get; }

public void ChangeDirection(FieldColor color)
{
int newDirection = 0;
if (color == FieldColor.White)
{
newDirection = (int)Direction+1;
if (newDirection > (int)AntDirection.West)
{
newDirection = (int)AntDirection.North;
}
}
else
{
newDirection = (int)Direction - 1;
if (newDirection < (int)AntDirection.North)
{
newDirection = (int)AntDirection.West;
}
}

Direction = (AntDirection)newDirection;
}

public bool CanMove(int fieldDimension)
{
switch (Direction)
{
case AntDirection.North:
return YPos - 1 >= 0;
case AntDirection.East:
return XPos + 1 < fieldDimension;
case AntDirection.South:
return YPos + 1 < fieldDimension;
case AntDirection.West:
return XPos - 1 >= 0;
default:
return true;
}
}

public void Move()
{
switch (Direction)
{
case AntDirection.North:
YPos -= 1;
break;
case AntDirection.East:
XPos += 1;
break;
case AntDirection.South:
YPos += 1;
break;
case AntDirection.West:
XPos -= 1;
break;
default:
return ;
}
}
}
10 changes: 10 additions & 0 deletions katas/LangtonAnt/solutions/steveDilling/Backend/Backend.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
16 changes: 16 additions & 0 deletions katas/LangtonAnt/solutions/steveDilling/Backend/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Backend;

public enum AntDirection
{
None = 0,
North = 1,
East,
South,
West
}

public enum FieldColor
{
Black = 0,
White = 1
}
18 changes: 18 additions & 0 deletions katas/LangtonAnt/solutions/steveDilling/Backend/Field.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Backend;

public class Field
{
public Field(FieldColor color)
{
Color = color;
}

public FieldColor Color { private set; get; }

public void ChangeColor()
{
Color = Color == FieldColor.Black
? FieldColor.White
: FieldColor.Black;
}
}
20 changes: 20 additions & 0 deletions katas/LangtonAnt/solutions/steveDilling/Backend/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Backend;

public static class Helper
{
public static string GetAntDirection(AntDirection direction)
{
switch(direction)
{
case AntDirection.North:
return "n";
case AntDirection.East:
return "o";
case AntDirection.South:
return "s";
case AntDirection.West:
return "w";
default: return "n";
}
}
}
128 changes: 128 additions & 0 deletions katas/LangtonAnt/solutions/steveDilling/Backend/Map.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;

namespace Backend;

public class Map
{
private readonly Field[,] _map;
private readonly int _moves;
private readonly Ant _ant;

private readonly List<string> _moveHistory;

private Map()
{
_ant = new(AntDirection.None, 0, 0);
_map = new Field[1, 1];
_moves = 100;
_moveHistory = new();
}

public Map(int dimension, int startX, int startY, AntDirection antDirection, int moves) : this()
{
_ant = new(antDirection, startX, startY);
_moves = moves;
_map = new Field[dimension, dimension];

//Init array
for (int y = 0; y < dimension; y++)
{
for (int x = 0; x < dimension; x++)
{
_map.SetValue(new Field(FieldColor.White), x, y);
}
}
}

public void CalculateMoves()
{
for (int step = 1; step <= _moves; step++)
{
Field field = _map[_ant.XPos, _ant.YPos];

//1. Change direction
bool canMove = false;
while (!canMove)
{
_ant.ChangeDirection(field.Color);
canMove = _ant.CanMove(_map.GetLength(0));
}

//2. Change color
field.ChangeColor();

//3. Perform move
_ant.Move();

//4. Create map string
var mapString = CreateMapString();
Console.WriteLine(" Move {0}: {1}", step, mapString);
//4. Save map
_moveHistory.Add(mapString);
}
}

private string CreateMapString()
{
string result = "";
for (int y = 0; y < _map.GetLength(1); y++)
{
for (int x= 0; x < _map.GetLength(0); x++)
{
if (result != string.Empty)
{
result += ",";
}

if (_ant.XPos == x && _ant.YPos == y)
{
if (_ant.Direction != AntDirection.None)
{
switch (_ant.Direction)
{
case AntDirection.East:
result += "o";
break;
case AntDirection.North:
result += "n";
break;
case AntDirection.South:
result += "s";
break;
case AntDirection.West:
result += "w";
break;
}
}
}

result += _map[x, y].Color == FieldColor.Black ? "s" : "w";
}
}

return result;
}

public bool SaveHistory(string filePath)
{
try
{
using FileStream fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write);
using StreamWriter streamWriter = new StreamWriter(fileStream);
foreach (string historyLine in _moveHistory)
{
streamWriter.WriteLine(historyLine);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}

return true;
}
}
Loading