public bool IsLegalCastle(Square[][] board, Move move)
{
var source = board[move.StartRow][move.StartColumn];
var destination = board[move.EndRow][move.EndColumn];
var direction = GetMovementModifier(move.ColumnChange);
var rook = direction > 0
? board[move.EndRow][7].ChessPiece
: board[move.EndRow][0].ChessPiece;
if (move.RowChange != 0)
throw new Exception("Illegal move.");
if (MoveCount > 0)
throw new Exception("You may only castle if your king has not moved yet.");
if (destination.ChessPiece != null)
throw new Exception("You may not castle to an occupied square.");
if (rook == null || rook.PieceType != Data.Enum.PieceType.Rook || rook.MoveCount > 0)
throw new Exception("The rook on that side has already moved.");
if (HasCollision(board, move))
throw new Exception("There is a piece in the way of the castle.");
if (IsInCheck(board))
throw new Exception("You may not castle while in check.");
if (destination.TargetedByTeam(board, GetOppositeTeam()))
throw new Exception("You may not castle into check.");
return true;
}