public IEnumerable <ChessMove> GetAvailableMoves(IReadOnlyChessPiece chessPiece) { var availableMoves = new List <ChessMove>(); if (chessPiece.PieceType != ChessPieceType.King) { return(availableMoves); } foreach (var position in GetNearestPositions(chessPiece.Position)) { var isEnemyPresent = chessBoard.IsEnemyOnPosition(position, chessPiece.Color.Opposite()); if (isEnemyPresent) { availableMoves.Add(new ChessMove(chessPiece.Position, position)); continue; } var isPositionTaken = chessBoard.IsPositionTaken(position); if (!isPositionTaken) { availableMoves.Add(new ChessMove(chessPiece.Position, position)); } } return(availableMoves); }
private IReadOnlyChessPiece GetQueensideRook(IReadOnlyChessPiece king) { return(chessBoard.Pieces .FirstOrDefault(p => p.Color == king.Color && p.PieceType == ChessPieceType.Rook && p.HasMoved == false && p.Position == new Position(queensideRookXPosition, king.Position.Y))); }
private bool ArePositionsEmpty(IReadOnlyChessPiece king) { int y = king.Position.Y; for (int x = king.Position.X - 1; x > 0; x--) { if (chessBoard.IsPositionTaken(new Position(x, y))) { return(false); } } return(true); }
private bool ArePositionsEmpty(IReadOnlyChessPiece king) { int y = king.Position.Y; for (int x = king.Position.X + 1; x < kingsideRookXPosition; x++) { if (chessBoard.IsPositionTaken(new Position(x, y))) { return(false); } } return(true); }
private List <Position> GetUpperPositions(IReadOnlyChessPiece chessPiece) { var positions = new List <Position>(); for (int y = chessPiece.Position.Y + 1; y < boardSize; y++) { var position = new Position(chessPiece.Position.X, y); if (AddPositionToList(chessPiece, position, positions)) { break; } } return(positions); }
private List <Position> GetLeftPositions(IReadOnlyChessPiece chessPiece) { var positions = new List <Position>(); for (int x = chessPiece.Position.X - 1; x >= 0; x--) { var position = new Position(x, chessPiece.Position.Y); if (AddPositionToList(chessPiece, position, positions)) { break; } } return(positions); }
private List <Position> GetRightPositions(IReadOnlyChessPiece chessPiece) { var positions = new List <Position>(); for (int x = chessPiece.Position.X + 1; x < boardSize; x++) { var position = new Position(x, chessPiece.Position.Y); if (AddPositionToList(chessPiece, position, positions)) { break; } } return(positions); }
private List <Position> GetLowerPositions(IReadOnlyChessPiece chessPiece) { var positions = new List <Position>(); for (int y = chessPiece.Position.Y - 1; y >= 0; y--) { var position = new Position(chessPiece.Position.X, y); if (AddPositionToList(chessPiece, position, positions)) { break; } } return(positions); }
private List <Position> GetPositions(IReadOnlyChessPiece chessPiece) { var positions = new List <Position>(8); int x = chessPiece.Position.X; int y = chessPiece.Position.Y; foreach ((var first, var second) in possibleCombinations) { positions.Add(new Position(x + first, y + second)); positions.Add(new Position(x + second, y + first)); } return(FilterTakenPositionsAndEnemies( FilterInvalidPositions(positions), chessPiece.Color)); }
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 void AddNormalMoves(IReadOnlyChessPiece chessPiece, int yDirection, List <ChessMove> availableMoves) { var possibleDestination = new Position(chessPiece.Position.X, chessPiece.Position.Y + yDirection); if (chessBoard.IsPositionTaken(possibleDestination)) { return; } var chessMove = new ChessMove(chessPiece.Position, possibleDestination); availableMoves.Add(chessMove); AddDoubleMove(chessPiece, yDirection, availableMoves); }
private List <Position> GetLeftUpperPositions(IReadOnlyChessPiece chessPiece) { var positions = new List <Position>(); int x = chessPiece.Position.X - 1; int y = chessPiece.Position.Y + 1; for (; x >= 0 && y < boardSize; x--, y++) { var position = new Position(x, y); if (AddPositionToList(chessPiece, position, positions)) { break; } } return(positions); }
private List <Position> GetRightLowerPositions(IReadOnlyChessPiece chessPiece) { var positions = new List <Position>(); int x = chessPiece.Position.X + 1; int y = chessPiece.Position.Y - 1; for (; x < boardSize && y >= 0; x++, y--) { var position = new Position(x, y); if (AddPositionToList(chessPiece, position, positions)) { break; } } return(positions); }
public IEnumerable <ChessMove> GetAvailableMoves(IReadOnlyChessPiece chessPiece) { var availableMoves = new List <ChessMove>(); if (chessPiece.PieceType != ChessPieceType.Knight) { return(availableMoves); } foreach (var position in GetPositions(chessPiece)) { var chessMove = new ChessMove(chessPiece.Position, position); availableMoves.Add(chessMove); } return(availableMoves); }
public IEnumerable <ChessMove> GetAvailableMoves(IReadOnlyChessPiece chessPiece) { var availableMoves = new List <ChessMove>(); if (chessPiece.PieceType != ChessPieceType.Pawn) { return(availableMoves); } int yDirection = CalculateYDirection(chessPiece.Color); AddNormalMoves(chessPiece, yDirection, availableMoves); AddPossibleRightCapture(chessPiece, yDirection, availableMoves); AddPossibleLeftCapture(chessPiece, yDirection, availableMoves); return(availableMoves); }
public IEnumerable <ChessMove> GetAvailableMoves(IReadOnlyChessPiece chessPiece) { var availableMoves = new List <ChessMove>(); if (chessPiece.PieceType != ChessPieceType.Pawn) { return(availableMoves); } if (!IsPieceOnCorrectRow(chessPiece)) { return(availableMoves); } if (chessBoard.History.ChessMoves.Count == 0) { return(availableMoves); } var lastMove = chessBoard.History.ChessMoves[^ 1];
private bool AddPositionToList(IReadOnlyChessPiece chessPiece, Position position, List <Position> positions) { var isEnemy = chessBoard .IsEnemyOnPosition(position, chessPiece.Color.Opposite()); if (isEnemy) { positions.Add(position); return(true); } if (chessBoard.IsPositionTaken(position)) { return(true); } positions.Add(position); return(false); }
public IEnumerable <ChessMove> GetAvailableMoves(IReadOnlyChessPiece chessPiece) { var availableMoves = new List <ChessMove>(); if (chessPiece.PieceType != ChessPieceType.Bishop && chessPiece.PieceType != ChessPieceType.Queen) { return(availableMoves); } foreach (var position in GetLeftUpperPositions(chessPiece) .Union(GetRightLowerPositions(chessPiece))) { var chessMove = new ChessMove(chessPiece.Position, position); availableMoves.Add(chessMove); } return(availableMoves); }
private void AddPossibleRightCapture(IReadOnlyChessPiece chessPiece, int yDirection, List <ChessMove> availableMoves) { var xPosition = chessPiece.Position.X + 1; if (xPosition >= boardSize) { return; } var possibleCapturePosition = new Position(xPosition, chessPiece.Position.Y + yDirection); if (!chessBoard.IsEnemyOnPosition(possibleCapturePosition, chessPiece.Color.Opposite())) { return; } var chessMove = new ChessMove(chessPiece.Position, possibleCapturePosition); availableMoves.Add(chessMove); }
public IEnumerable <ChessMove> GetAvailableMoves(IReadOnlyChessPiece chessPiece) { var availableMoves = new List <ChessMove>(); if (chessPiece.PieceType != ChessPieceType.King) { return(availableMoves); } if (chessPiece.HasMoved) { return(availableMoves); } var rook = GetQueensideRook(chessPiece); if (rook == null) { return(availableMoves); } if (!ArePositionsEmpty(chessPiece)) { return(availableMoves); } if (ArePositionsBeingAttacked(chessPiece)) { return(availableMoves); } var castlingMove = new ChessMove(chessPiece.Position, rook.Position); availableMoves.Add(castlingMove); return(availableMoves); }
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); }
public IEnumerable <ChessMove> GetAvailableMoves(IReadOnlyChessPiece chessPiece) { return(movements .SelectMany(m => m.GetAvailableMoves(chessPiece)) .ToList()); }
public ChessPieceWrapper(IReadOnlyChessPiece chessPiece) { ChessPiece = chessPiece; }