ChessDotNet.Pieces.Pawn.GetValidMoves C# (CSharp) Method

GetValidMoves() public method

public GetValidMoves ( Position from, bool returnIfAny, ChessGame game, bool>.Func gameMoveValidator ) : ReadOnlyCollection
from Position
returnIfAny bool
game ChessGame
gameMoveValidator bool>.Func
return ReadOnlyCollection
        public override ReadOnlyCollection<Move> GetValidMoves(Position from, bool returnIfAny, ChessGame game, Func<Move, bool> gameMoveValidator)
        {
            ChessUtilities.ThrowIfNull(from, "from");
            List<Move> validMoves = new List<Move>();
            Piece piece = game.GetPieceAt(from);
            int l0 = game.BoardHeight;
            int l1 = game.BoardWidth;
            int[][] directions;
            if (piece.Owner == Player.White)
            {
                directions = new int[][] { new int[] { 0, 1 }, new int[] { 0, 2 }, new int[] { 1, 1 }, new int[] { -1, 1 } };
            }
            else
            {
                directions = new int[][] { new int[] { 0, -1 }, new int[] { 0, -2 }, new int[] { -1, -1 }, new int[] { 1, -1 } };
            }
            foreach (int[] dir in directions)
            {
                if ((int)from.File + dir[0] < 0 || (int)from.File + dir[0] >= l1
                    || from.Rank + dir[1] < 1 || from.Rank + dir[1] > l0)
                    continue;
                Move move = new Move(from, new Position(from.File + dir[0], from.Rank + dir[1]), piece.Owner);
                List<Move> moves = new List<Move>();
                if ((move.NewPosition.Rank == 8 && move.Player == Player.White) || (move.NewPosition.Rank == 1 && move.Player == Player.Black))
                {
                    foreach (char pieceChar in ValidPromotionPieces.Where(x => char.IsUpper(x)))
                    {
                        moves.Add(new Move(move.OriginalPosition, move.NewPosition, move.Player, pieceChar));
                    }
                }
                else
                {
                    moves.Add(move);
                }
                foreach (Move m in moves)
                {
                    if (gameMoveValidator(m))
                    {
                        validMoves.Add(m);
                        if (returnIfAny)
                            return new ReadOnlyCollection<Move>(validMoves);
                    }
                }
            }
            return new ReadOnlyCollection<Move>(validMoves);
        }
    }