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 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 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 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); }
private List <Position> FilterTakenPositionsAndEnemies(List <Position> positions, ChessColor chessColor) { var filtered = new List <Position>(); foreach (var position in positions) { var isEnemy = chessBoard .IsEnemyOnPosition(position, chessColor.Opposite()); if (isEnemy) { filtered.Add(position); continue; } if (!chessBoard.IsPositionTaken(position)) { filtered.Add(position); } } return(filtered); }