public bool IsChecked(ChessColor kingColor) { var king = chessBoard.GetKing(kingColor); return(chessBoard.Pieces .Where(p => p.Color != kingColor) .Any(p => movement .GetAvailableMoves(p) .Any(m => m.FinishedPosition == king.Position) )); }
public IEnumerable <ChessMove> GetAvailableLegalMoves(IReadOnlyChessPiece chessPiece) { var filteredMoves = new List <ChessMove>(); foreach (var chessMove in movement.GetAvailableMoves(chessPiece)) { chessBoard.Move(chessMove); if (!checkDetector.IsChecked(chessPiece.Color)) { filteredMoves.Add(chessMove); } chessBoard.ReverseLastMove(); } return(filteredMoves); }
private bool ArePositionsBeingAttacked(IReadOnlyChessPiece king) { int y = king.Position.Y; int lastX = king.Position.X - 2; var enemyPieces = chessBoard.Pieces .Where(p => p.Color == king.Color.Opposite()) .ToList(); for (int x = king.Position.X; x >= lastX; x--) { var position = new Position(x, y); foreach (var piece in enemyPieces) { var isAttacked = movement .GetAvailableMoves(piece) .Any(m => m.FinishedPosition == position); if (isAttacked) { return(true); } } } return(false); }