This commit is contained in:
2024-09-26 21:11:47 -05:00
parent 6b5bb96de7
commit 81dd267290
16 changed files with 636 additions and 339 deletions

View File

@@ -1,22 +1,15 @@
using System.Numerics;
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing
{
/// <summary>
/// Directions are relative to the perspective of Player 1.
/// Up points towards player 1. Down points towards player 2.
/// </summary>
public static class Direction
{
public static readonly Vector2 Up = new(0, 1);
public static readonly Vector2 Down = new(0, -1);
public static readonly Vector2 Left = new(-1, 0);
public static readonly Vector2 Right = new(1, 0);
public static readonly Vector2 UpLeft = new(-1, 1);
public static readonly Vector2 UpRight = new(1, 1);
public static readonly Vector2 DownLeft = new(-1, -1);
public static readonly Vector2 DownRight = new(1, -1);
public static readonly Vector2 KnightLeft = new(-1, 2);
public static readonly Vector2 KnightRight = new(1, 2);
}
}
public static class Direction
{
public static readonly Vector2 Forward = new(0, 1);
public static readonly Vector2 Backward = new(0, -1);
public static readonly Vector2 Left = new(-1, 0);
public static readonly Vector2 Right = new(1, 0);
public static readonly Vector2 ForwardLeft = new(-1, 1);
public static readonly Vector2 ForwardRight = new(1, 1);
public static readonly Vector2 BackwardLeft = new(-1, -1);
public static readonly Vector2 BackwardRight = new(1, -1);
public static readonly Vector2 KnightLeft = new(-1, 2);
public static readonly Vector2 KnightRight = new(1, 2);
}

View File

@@ -1,41 +1,33 @@
using System.Diagnostics;
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
[DebuggerDisplay("{Direction} - {Distance}")]
public record Path(Vector2 Direction, Distance Distance = Distance.OneStep)
{
[DebuggerDisplay("{Direction} - {Distance}")]
public class Path
{
public Vector2 Direction { get; }
public Distance Distance { get; }
public Path(Vector2 direction, Distance distance = Distance.OneStep)
{
Direction = direction;
Distance = distance;
}
public Path Invert() => new(Vector2.Negate(Direction), Distance);
}
public static class PathExtensions
{
public static Path GetNearestPath(this IEnumerable<Path> paths, Vector2 start, Vector2 end)
{
if (!paths.DefaultIfEmpty().Any())
{
throw new ArgumentException("No paths to get nearest path from.");
}
var shortestPath = paths.First();
foreach (var path in paths.Skip(1))
{
var distance = Vector2.Distance(start + path.Direction, end);
var shortestDistance = Vector2.Distance(start + shortestPath.Direction, end);
if (distance < shortestDistance)
{
shortestPath = path;
}
}
return shortestPath;
}
}
public Path Invert() => new(Vector2.Negate(Direction), Distance);
}
public static class PathExtensions
{
public static Path GetNearestPath(this IEnumerable<Path> paths, Vector2 start, Vector2 end)
{
if (!paths.DefaultIfEmpty().Any())
{
throw new ArgumentException("No paths to get nearest path from.");
}
var shortestPath = paths.First();
foreach (var path in paths.Skip(1))
{
var distance = Vector2.Distance(start + path.Direction, end);
var shortestDistance = Vector2.Distance(start + shortestPath.Direction, end);
if (distance < shortestDistance)
{
shortestPath = path;
}
}
return shortestPath;
}
}