public void ValidateMove(IFigure figure, IBoard board, Move move) { var from = move.From; var to = move.To; var horizontalMovement = Math.Abs(from.Row - to.Row); var verticalMovement = Math.Abs(from.Col - to.Col); if (horizontalMovement == 0 || verticalMovement == 0) { if (horizontalMovement == 0) { char colIndex = from.Col; char colDirection = (char)(from.Col < to.Col ? 1 : -1); while (true) { colIndex += colDirection; if (to.Col == colIndex) { MovementValidator.CheckForFigureOnTheWay(figure, board, to); return; } var position = Position.FromChessCordinates(move.From.Row, colIndex); MovementValidator.CheckForFigureOnTheWay(figure, board, position); } } else { int rowIndex = from.Row; int rowDirection = from.Row < to.Row ? 1 : -1; while (true) { rowIndex += rowDirection; if (to.Row == rowIndex) { MovementValidator.CheckForFigureOnTheWay(figure, board, to); return; } var position = Position.FromChessCordinates(rowIndex, move.From.Col); MovementValidator.CheckForFigureOnTheWay(figure, board, position); } } } else { if (figure.GetType().Name != "Queen") { throw new InvalidOperationException($"{figure.GetType().Name} cannot move that way!"); } } }
public void ValidateMove(IFigure figure, IBoard board, Move move) { var rowDistance = Math.Abs(move.From.Row - move.To.Row); var colDistance = Math.Abs(move.From.Col - move.To.Col); // TODO: extract to method var other = figure.Color == ChessColor.White ? ChessColor.Black : ChessColor.White; if (rowDistance != colDistance) { throw new InvalidOperationException($"{figure.GetType().Name} cannot move this way!"); } var from = move.From; var to = move.To; int rowIndex = from.Row; char colIndex = from.Col; int rowDirection = from.Row < to.Row ? 1 : -1; char colDirection = (char)(from.Col < to.Col ? 1 : -1); while (true) { rowIndex += rowDirection; colIndex += colDirection; if (to.Row == rowIndex && to.Col == colIndex) { MovementValidator.CheckForFigureOnTheWay(figure, board, to); return; } var position = Position.FromChessCordinates(rowIndex, colIndex); MovementValidator.CheckForFigureOnTheWay(figure, board, position); } }