This commit is contained in:
2024-10-26 22:09:41 -05:00
parent 550942281b
commit f8bf967581
4 changed files with 107 additions and 53 deletions

View File

@@ -25,8 +25,6 @@ namespace Shogi.Domain.ValueObjects
public WhichPiece WhichPiece { get; }
public WhichPlayer Owner { get; private set; }
public bool IsPromoted { get; private set; }
public bool IsUpsideDown => Owner == WhichPlayer.Player2;
protected Piece(WhichPiece piece, WhichPlayer owner, bool isPromoted = false)
{
WhichPiece = piece;
@@ -60,7 +58,7 @@ namespace Shogi.Domain.ValueObjects
{
var steps = new List<Vector2>(10);
var path = MoveSet.GetNearestPath(start, end);
var path = GetNearestPath(MoveSet, start, end);
var position = start;
while (Vector2.Distance(start, position) < Vector2.Distance(start, end))
{
@@ -77,5 +75,25 @@ namespace Shogi.Domain.ValueObjects
return [];
}
private static Path GetNearestPath(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.Step, end);
var shortestDistance = Vector2.Distance(start + shortestPath.Step, end);
if (distance < shortestDistance)
{
shortestPath = path;
}
}
return shortestPath;
}
}
}