public void VlidateMove(IFigure figure, IBoard board, Move move) { Position from = move.From; Position to = move.To; if (from.Row != to.Row && from.Col != to.Col) { throw new InvalidOperationException("Rook cannot move this way!"); } if (to.Col > from.Col) { this.RightChecker(board, from, to); return; } else if (to.Col < from.Col) { this.LeftChecker(board, from, to); return; } else if (to.Row > from.Row) { this.TopChecker(board, from, to); return; } else if (to.Row < from.Row) { this.DownChecker(board, from, to); return; } else { throw new InvalidOperationException("Rook cannot move this way!"); } }
// TODO: Castling checking public void VlidateMove(IFigure figure, IBoard board, Move move) { Position from = move.From; Position to = move.To; bool condition = ( ((from.Col + 1) == to.Col) || ((from.Col - 1) == to.Col) || ((from.Row + 1) == to.Row) || ((from.Row - 1) == to.Row) || (((from.Row + 1) == to.Row) && ((from.Col + 1) == to.Col)) || (((from.Row - 1) == to.Row) && ((from.Col + 1) == to.Col)) || (((from.Row - 1) == to.Row) && ((from.Col - 1) == to.Col)) || (((from.Row + 1) == to.Row) && ((from.Col - 1) == to.Col)) ); if (condition) { return; } else { throw new InvalidOperationException("King cannot move this way!"); } }
public void VlidateMove(IFigure figure, IBoard board, Move move) { Position from = move.From; Position to = move.To; if ((from.Row + 2) == to.Row && (from.Col - 1) == to.Col) { return; } else if ((from.Row + 2) == to.Row && (from.Col + 1) == to.Col) { return; } else if ((from.Row + 1) == to.Row && (from.Col + 2) == to.Col) { return; } else if ((from.Row - 1) == to.Row && (from.Col + 2) == to.Col) { return; } else if ((from.Row - 2) == to.Row && (from.Col + 1) == to.Col) { return; } else if ((from.Row - 2) == to.Row && (from.Col - 1) == to.Col) { return; } else if ((from.Row - 1) == to.Row && (from.Col - 2) == to.Col) { return; } else if ((from.Row + 1) == to.Row && (from.Col - 2) == to.Col) { return; } else { throw new InvalidOperationException("Knight cannot move this way!"); } }
public void VlidateMove(IFigure figure, IBoard board, Move move) { Position from = move.From; Position to = move.To; var rowDistance = Math.Abs(move.From.Row - move.To.Row); var colDistance = Math.Abs(move.From.Col - move.To.Col); if (rowDistance != colDistance) { throw new InvalidOperationException("Bishop cannot move this way!"); } if (to.Row > from.Row) { if (to.Col > from.Col) { this.TopRightChecker(board, from, to); return; } if (to.Col < from.Col) { this.TopLeftChecker(board, from, to); return; } } if (to.Row < from.Row) { if (to.Col > from.Col) { this.DownRightChecker(board, from, to); return; } if (to.Col < from.Col) { this.DownLeftChecker(board, from, to); return; } } throw new InvalidOperationException("Bishop cannot move this way!"); }
public void VlidateMove(IFigure figure, IBoard board, Move move) { var color = figure.Color; var other = figure.Color == ChessColor.White ? ChessColor.Black : ChessColor.White; var from = move.From; var to = move.To; var figureAtPosition = board.GetFigureAtPosition(to); if (color == ChessColor.White && to.Row < from.Row) { throw new InvalidOperationException(PawnBackwardsErrorMessage); } if (color == ChessColor.Black && to.Row > from.Row) { throw new InvalidOperationException(PawnBackwardsErrorMessage); } if (color == ChessColor.White) { if (from.Row + 1 == to.Row && this.CheckDiagonalMove(from, to)) { if (this.CheckOtherFigureIfValid(board, to, other)) { return; } } if (from.Row == 2 && from.Col == to.Col) { if (from.Row + 2 == to.Row && figureAtPosition == null) { return; } } if (from.Row + 1 == to.Row && from.Col == to.Col) { if (figureAtPosition == null) { return; } } } else if (color == ChessColor.Black) { if (from.Row - 1 == to.Row && this.CheckDiagonalMove(from, to)) { if (this.CheckOtherFigureIfValid(board, to, other)) { return; } } if (from.Row == 7 && from.Col == to.Col) { if (from.Row - 2 == to.Row && figureAtPosition == null) { return; } } if (from.Row - 1 == to.Row && from.Col == to.Col) { if (figureAtPosition == null) { return; } } } throw new InvalidOperationException(PawnInvalidMove); }