Yep
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
using Shogi.Domain.Other;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
{
|
||||
[DebuggerDisplay("{WhichPiece} {Owner}")]
|
||||
public abstract record class Piece : IRulesLifecycle<Piece>
|
||||
public abstract record class Piece
|
||||
{
|
||||
public static Piece Create(WhichPiece piece, WhichPlayer owner, bool isPromoted = false)
|
||||
{
|
||||
@@ -65,7 +64,7 @@ namespace Shogi.Domain.ValueObjects
|
||||
var position = start;
|
||||
while (Vector2.Distance(start, position) < Vector2.Distance(start, end))
|
||||
{
|
||||
position += path.NormalizedDirection;
|
||||
position += path.Step;
|
||||
steps.Add(position);
|
||||
|
||||
if (path.Distance == Distance.OneStep) break;
|
||||
@@ -76,51 +75,7 @@ namespace Shogi.Domain.ValueObjects
|
||||
return steps;
|
||||
}
|
||||
|
||||
return Array.Empty<Vector2>();
|
||||
return [];
|
||||
}
|
||||
|
||||
#region IRulesLifecycle
|
||||
public RulesLifecycleResult OnMoveValidation(MoveValidationContext<Piece> ctx)
|
||||
{
|
||||
var paths = this.MoveSet;
|
||||
|
||||
var matchingPaths = paths.Where(p => p.NormalizedDirection == Vector2.Normalize(ctx.To - ctx.From));
|
||||
if (!matchingPaths.Any())
|
||||
{
|
||||
return new RulesLifecycleResult(IsError: true, "Piece cannot move like that.");
|
||||
}
|
||||
|
||||
var multiStepPaths = matchingPaths.Where(p => p.Distance == Distance.MultiStep).ToArray();
|
||||
foreach (var path in multiStepPaths)
|
||||
{
|
||||
// Assert that no pieces exist along the from -> to path.
|
||||
var isPathObstructed = GetPositionsAlongPath(ctx.From, ctx.To, path)
|
||||
.Any(pos => ctx.BoardState[(int)pos.X, (int)pos.Y] != null);
|
||||
if (isPathObstructed)
|
||||
{
|
||||
return new RulesLifecycleResult(IsError: true, "Piece cannot move through other pieces.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var pieceAtTo = ctx.BoardState[(int)ctx.To.X, (int)ctx.To.Y];
|
||||
if (pieceAtTo?.Owner == this.Owner)
|
||||
{
|
||||
return new RulesLifecycleResult(IsError: true, "Cannot capture your own pieces.");
|
||||
}
|
||||
|
||||
return new RulesLifecycleResult(IsError: false);
|
||||
|
||||
static IEnumerable<Vector2> GetPositionsAlongPath(Vector2 from, Vector2 to, Path path)
|
||||
{
|
||||
var next = from;
|
||||
while (next != to && next.X >= 0 && next.X < 9 && next.Y >= 0 && next.Y < 9)
|
||||
{
|
||||
next += path.NormalizedDirection;
|
||||
yield return next;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user