Exemple #1
0
        /*
         * Return a Forsyth-Edwards Notation string corresponding to a Position object
         */
        public static String convertPositionToFEN(Position position)
        {
            StringBuilder feNotation = new StringBuilder();
            //Piece placement
            for (int rank = 7; rank >= 0; rank--)
            {
                int emptySquares = 0;
                for (int file = 0; file < 8; file++)
                {
                    PieceType piece = position.getPiece(Position.getSquare(file, rank));
                    if (piece == PieceType.Empty)
                    {
                        emptySquares++;
                    }
                    else
                    {
                        if (emptySquares > 0)
                        {
                            feNotation.Append(emptySquares);
                            emptySquares = 0;
                        }
                        switch (piece)
                        {
                            case PieceType.K:   feNotation.Append('K'); break;
                            case PieceType.Q:   feNotation.Append('Q'); break;
                            case PieceType.R:   feNotation.Append('R'); break;
                            case PieceType.B:   feNotation.Append('B'); break;
                            case PieceType.N:   feNotation.Append('N'); break;
                            case PieceType.P:   feNotation.Append('P'); break;
                            case PieceType.k:   feNotation.Append('k'); break;
                            case PieceType.q:   feNotation.Append('q'); break;
                            case PieceType.r:   feNotation.Append('r'); break;
                            case PieceType.b:   feNotation.Append('b'); break;
                            case PieceType.n:   feNotation.Append('n'); break;
                            case PieceType.p:   feNotation.Append('p'); break;
                            default: throw new ParserError("Error creating FEN String");
                        }
                    }
                }
                if (emptySquares > 0)
                {
                    feNotation.Append(emptySquares);
                }
                if (rank > 0)
                {
                    feNotation.Append("/");
                }
            }
            //Active Colour
            feNotation.Append(position.whiteMove ? " w " : " b ");

            //Castling Rights
            Boolean anyCastle = false;
            if (position.h1Castle())
            {
                feNotation.Append('K');
                anyCastle = true;
            }
            if (position.a1Castle())
            {
                feNotation.Append('Q');
                anyCastle = true;
            }
            if (position.h8Castle())
            {
                feNotation.Append('k');
                anyCastle = true;
            }
            if (position.a8Castle())
            {
                feNotation.Append('q');
                anyCastle = true;
            }
            if (!anyCastle)
            {
                feNotation.Append('-');
            }

            //En Passant Target Square
            {
                feNotation.Append(" ");
                if (position.getEpSquare() >= 0)
                {
                    int file = Position.getFile(position.getEpSquare());
                    int rank = Position.getRank(position.getEpSquare());
                    feNotation.Append((char)(file + 'a'));
                    feNotation.Append((char)(rank + '1'));
                }
                else
                {
                    feNotation.Append('-');
                }
            }

            //Move Counters
            feNotation.Append(' ');
            feNotation.Append(position.halfMoveClock);
            feNotation.Append(' ');
            feNotation.Append(position.fullMoveCounter);

            return feNotation.ToString();
        }