public override bool IsValidMove(Move move, ChessGame game)
{
ChessUtilities.ThrowIfNull(move, "move");
ChessUtilities.ThrowIfNull(game, "game");
Position origin = move.OriginalPosition;
Position destination = move.NewPosition;
PositionDistance posDelta = new PositionDistance(origin, destination);
if (posDelta.DistanceX != 0 && posDelta.DistanceY != 0)
return false;
bool increasingRank = destination.Rank > origin.Rank;
bool increasingFile = (int)destination.File > (int)origin.File;
if (posDelta.DistanceX == 0)
{
int f = (int)origin.File;
for (int r = origin.Rank + (increasingRank ? 1 : -1);
increasingRank ? r < destination.Rank : r > destination.Rank;
r += increasingRank ? 1 : -1)
{
if (game.GetPieceAt((File)f, r) != null)
{
return false;
}
}
}
else // (posDelta.DeltaY == 0)
{
int r = origin.Rank;
for (int f = (int)origin.File + (increasingFile ? 1 : -1);
increasingFile ? f < (int)destination.File : f > (int)destination.File;
f += increasingFile ? 1 : -1)
{
if (game.GetPieceAt((File)f, r) != null)
{
return false;
}
}
}
return true;
}