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 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); }
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); }