src/Engine/Utilities/ChessFormatter.cs
/// <summary>
/// Format a chess move in standard algebraic notation. https://en.wikipedia.org/wiki/Algebraic_notation_(chess).
/// </summary>
/// <param name="move">The move to convert to a string.</param>
/// <returns>A standard algebraic notation description of the move.</returns>
Format a chess move in standard algebraic notation.
Format a chess move in standard algebraic notation .
public static string MoveToString(Move move)
        {
            var output = new StringBuilder();
            if (move.ShortCastle)
            {
                return "O-O";
            }
            if (move.LongCastle)
            {
                return "O-O-O";
            }
            var pieceMoved = PieceToString(move.PieceMoved);
            output.Append(
                string.IsNullOrEmpty(pieceMoved) ?
                (move.Capture ? FileToString(move.OriginalPosition.File) : string.Empty) :
                pieceMoved);
            if (move.AmbiguousOriginalFile)
            {
                output.Append(FileToString(move.OriginalPosition.File));
            }
            if (move.AmbiguousOriginalRank)
            {
                output.Append(RankToString(move.OriginalPosition.Rank));
            }
            if (move.Capture)
            {
                output.Append("x");
            }
            output.Append($"{move.FinalPosition}");
            if (move.PiecePromotedTo != null)
            {
                output.Append($"={PieceToString(move.PiecePromotedTo.Value)}");
            }
            if (move.Checkmates)
            {
                output.Append("#");
            }
            else if (move.Checks)
            {
                output.Append("+");
            }
            return output.ToString();
        }
public static string MoveToString ( Move move ) { var output = new StringBuilder ( ) ; if ( move . ShortCastle ) { return " " ; } if ( move . LongCastle ) { return " " ; } var pieceMoved = PieceToString ( move . PieceMoved ) ; output . Append ( string . IsNullOrEmpty ( pieceMoved ) ? ( move . Capture ? FileToString ( move . OriginalPosition . File ) : string . Empty ) : pieceMoved ) ; if ( move . AmbiguousOriginalFile ) { output . Append ( FileToString ( move . OriginalPosition . File ) ) ; } if ( move . AmbiguousOriginalRank ) { output . Append ( RankToString ( move . OriginalPosition . Rank ) ) ; } if ( move . Capture ) { output . Append ( " " ) ; } output . Append ( $" { move . FinalPosition } " ) ; if ( move . PiecePromotedTo != null ) { output . Append ( $" = { PieceToString ( move . PiecePromotedTo . Value ) } " ) ; } if ( move . Checkmates ) { output . Append ( " " ) ; } else if ( move . Checks ) { output . Append ( " " ) ; } return output . ToString ( ) ; }
Format a chess move in standard algebraic notation.
Format a chess move in standard algebraic notation .
// Begin with the piece type unless the piece was a pawn, // in which case the original file is used for captures and an // empty string for moves.
A standard algebraic notation description of the move.
A standard algebraic notation description of the move .
move
Move
The move to convert to a string.
The move to convert to a string .