Пример #1
0
        /// <summary>
        /// Returns a text drawing of the position with the given comments displayed.
        /// </summary>
        /// <param name="comments">The comments to display.</param>
        /// <returns>A text drawing of the position with the given comments displayed</returns>
        public String ToString(params String[] comments)
        {
            StringBuilder sb    = new StringBuilder("   +------------------------+ ", 400);
            Int32         index = 0;

            if (index < comments.Length)
            {
                sb.Append(comments[index++]);
            }

            for (Int32 rank = 0; rank < 8; rank++)
            {
                sb.Append(Environment.NewLine);
                sb.Append(' ');
                sb.Append(8 - rank);
                sb.Append(" |");
                for (Int32 file = 0; file < 8; file++)
                {
                    Int32 piece = Square[file + rank * 8];
                    if (piece != Piece.Empty)
                    {
                        sb.Append((piece & Colour.Mask) == Colour.White ? '<' : '[');
                        sb.Append(Stringify.PieceInitial(piece));
                        sb.Append((piece & Colour.Mask) == Colour.White ? '>' : ']');
                    }
                    else
                    {
                        sb.Append((file + rank) % 2 == 1 ? ":::" : "   ");
                    }
                }
                sb.Append("| ");
                if (index < comments.Length)
                {
                    sb.Append(comments[index++]);
                }
            }

            sb.Append(Environment.NewLine);
            sb.Append("   +------------------------+ ");
            if (index < comments.Length)
            {
                sb.Append(comments[index++]);
            }

            sb.Append(Environment.NewLine);
            sb.Append("     a  b  c  d  e  f  g  h   ");
            if (index < comments.Length)
            {
                sb.Append(comments[index++]);
            }

            return(sb.ToString());
        }
Пример #2
0
        /// <summary>
        /// Returns the FEN string that describes the position.
        /// </summary>
        /// <returns>The FEN string that describes the position.</returns>
        public String GetFEN()
        {
            StringBuilder sb = new StringBuilder();

            for (Int32 rank = 0; rank < 8; rank++)
            {
                Int32 spaces = 0;
                for (Int32 file = 0; file < 8; file++)
                {
                    Int32 square = file + rank * 8;
                    if (Square[square] == Piece.Empty)
                    {
                        spaces++;
                    }
                    else
                    {
                        if (spaces > 0)
                        {
                            sb.Append(spaces);
                            spaces = 0;
                        }
                        String piece = Stringify.PieceInitial(Square[square]);
                        if ((Square[square] & Colour.Mask) == Colour.Black)
                        {
                            piece = piece.ToLowerInvariant();
                        }
                        sb.Append(piece);
                    }
                }
                if (spaces > 0)
                {
                    sb.Append(spaces);
                }
                if (rank < 7)
                {
                    sb.Append('/');
                }
            }

            sb.Append(' ');
            sb.Append(SideToMove == Colour.White ? 'w' : 'b');
            sb.Append(' ');

            if (CastleKingside[Colour.White] > 0)
            {
                sb.Append('K');
            }
            if (CastleQueenside[Colour.White] > 0)
            {
                sb.Append('Q');
            }
            if (CastleKingside[Colour.Black] > 0)
            {
                sb.Append('k');
            }
            if (CastleQueenside[Colour.Black] > 0)
            {
                sb.Append('q');
            }
            if (sb[sb.Length - 1] == ' ')
            {
                sb.Append('-');
            }
            sb.Append(' ');

            if (EnPassantSquare != InvalidSquare)
            {
                sb.Append(Stringify.Square(EnPassantSquare));
            }
            else
            {
                sb.Append('-');
            }
            sb.Append(' ');

            sb.Append(FiftyMovesClock);
            sb.Append(' ');
            sb.Append(HalfMoves / 2 + 1);

            return(sb.ToString());
        }