src/Engine/ChessEngine.cs
/// <summary>
/// Gets possible (unvalidated) moves for a pawn based on board position.
/// </summary>
/// <remarks>If the pawn would be promoted, indicate queen promotion with the intention that the caller will see this and adjust, if necessary.</remarks>
/// <param name="pawn">The pawn to move.</param>
/// <returns>Possible squares to move to (considering the position of the pawn and other pieces, but not considering check).</returns>
Gets possible (unvalidated) moves for a pawn based on board position.
Gets possible ( unvalidated ) moves for a pawn based on board position .
private IEnumerable<Move> GetPawnMoves(ChessPiece pawn)
        {
            var whitePawn = ChessFormatter.IsPieceWhite(pawn.PieceType);
            var rankProgresion = whitePawn ? 1 : -1;
            var finalPosition = new BoardPosition(pawn.Position.File, pawn.Position.Rank + rankProgresion);
            if (Game.GetPiece(finalPosition) == null)
            {
                yield return CreateMoveFromPiece(pawn, finalPosition, false);
                finalPosition = new BoardPosition(pawn.Position.File, pawn.Position.Rank + (2 * rankProgresion));
                if (pawn.Position.Rank == (whitePawn ? 1 : 6) && Game.GetPiece(finalPosition) == null)
                {
                    yield return CreateMoveFromPiece(pawn, finalPosition, false);
                }
            }
            var captureFiles = new[] { pawn.Position.File - 1, pawn.Position.File + 1 };
            foreach (var captureFile in captureFiles)
            {
                finalPosition = new BoardPosition(captureFile, pawn.Position.Rank + rankProgresion);
                var pieceAtDestination = Game.GetPiece(finalPosition);
                if (Game.EnPassantTarget == finalPosition || (pieceAtDestination != null && ChessFormatter.IsPieceWhite(pieceAtDestination.PieceType) != whitePawn))
                {
                    yield return CreateMoveFromPiece(pawn, finalPosition, true);
                }
            }
        }
private IEnumerable < Move > GetPawnMoves ( ChessPiece pawn ) { var whitePawn = ChessFormatter . IsPieceWhite ( pawn . PieceType ) ; var rankProgresion = whitePawn ? 1 : - 1 ; var finalPosition = new BoardPosition ( pawn . Position . File , pawn . Position . Rank + rankProgresion ) ; if ( Game . GetPiece ( finalPosition ) == null ) { yield return CreateMoveFromPiece ( pawn , finalPosition , false ) ; finalPosition = new BoardPosition ( pawn . Position . File , pawn . Position . Rank + ( 2 * rankProgresion ) ) ; if ( pawn . Position . Rank == ( whitePawn ? 1 : 6 ) && Game . GetPiece ( finalPosition ) == null ) { yield return CreateMoveFromPiece ( pawn , finalPosition , false ) ; } } var captureFiles = new [ ] { pawn . Position . File - 1 , pawn . Position . File + 1 } ; foreach ( var captureFile in captureFiles ) { finalPosition = new BoardPosition ( captureFile , pawn . Position . Rank + rankProgresion ) ; var pieceAtDestination = Game . GetPiece ( finalPosition ) ; if ( Game . EnPassantTarget == finalPosition || ( pieceAtDestination != null && ChessFormatter . IsPieceWhite ( pieceAtDestination . PieceType ) != whitePawn ) ) { yield return CreateMoveFromPiece ( pawn , finalPosition , true ) ; } } }
Gets possible (unvalidated) moves for a pawn based on board position.
Gets possible ( unvalidated ) moves for a pawn based on board position .
// Check one-space advance // Check two-space advance // Check captures
Possible squares to move to (considering the position of the pawn and other pieces, but not considering check).
Possible squares to move to ( considering the position of the pawn and other pieces but not considering check ) .
pawn
ChessPiece
The pawn to move.
The pawn to move .
remarks
If the pawn would be promoted, indicate queen promotion with the intention that the caller will see this and adjust, if necessary.
If the pawn would be promoted indicate queen promotion with the intention that the caller will see this and adjust if necessary .