예제 #1
0
        internal static string ToString(Board board)
        {
            if (board == default(Board))
            {
                Debug.Assert(false, "Null or Empty FEN");
                return(string.Empty);
            }

            StringBuilder fenString = new StringBuilder();

            #region Pieces
            for (RankE rank = RankE.Rank_8; ; --rank)
            {
                byte emptySqr = 0;
                for (FileE file = FileE.File_A; file <= FileE.File_H; ++file)
                {
                    bool isPiece = false;
                    for (ColorE color = ColorE.White; color <= ColorE.Black; ++color)
                    {
                        Bitboard occupy = board.Occupies[(byte)color];
                        SquareE  sqr    = Square._Square(file, rank);
                        if (BitBoard.GetSquare(occupy, sqr))
                        {
                            for (PieceE type = PieceE.Pawn; type <= PieceE.King; ++type)
                            {
                                Bitboard piece = board.Pieces[(byte)type];
                                if (BitBoard.GetSquare(piece, sqr))  // occupy & piece
                                {
                                    isPiece = true;
                                    if (emptySqr > 0)
                                    {
                                        fenString.Append(emptySqr);
                                        emptySqr = 0;
                                    }
                                    fenString.Append(Piece.mapPiece[(byte)color, (byte)type]);
                                    break;
                                }
                            }
                        }
                    }
                    if (!isPiece)
                    {
                        ++emptySqr;
                    }
                }
                if (emptySqr > 0)
                {
                    fenString.Append(emptySqr);
                    emptySqr = 0;
                }

                if (rank == RankE.Rank_1)
                {
                    break;
                }

                fenString.Append(rankSep);
            }
            #endregion

            fenString.Append(recordSep);

            #region OnMove
            switch (board.OnMove)
            {
            case ColorE.White: fenString.Append('w'); break;

            case ColorE.Black: fenString.Append('b'); break;

            case ColorE.NoColor:
            default: fenString.Append('-'); break;
            }
            #endregion

            fenString.Append(recordSep);

            #region Castle
            if (board.Castle[(byte)ColorE.White].AnyCastle ||
                board.Castle[(byte)ColorE.Black].AnyCastle)
            {
                if (board.Castle[(byte)ColorE.White].ShortCastle)
                {
                    fenString.Append('K');
                }
                if (board.Castle[(byte)ColorE.White].LongCastle)
                {
                    fenString.Append('Q');
                }
                if (board.Castle[(byte)ColorE.Black].ShortCastle)
                {
                    fenString.Append('k');
                }
                if (board.Castle[(byte)ColorE.Black].LongCastle)
                {
                    fenString.Append('q');
                }
            }
            else
            {
                fenString.Append('-');
            }
            #endregion

            fenString.Append(recordSep);

            #region EnPassant

            SquareE ep = board.EnPassant;
            if (Square.IsValid(ep))
            {
                fenString.Append(ep.ToString().ToLower());
            }
            else
            {
                fenString.Append('-');
            }
            #endregion

            fenString.Append(recordSep);

            #region HalfMove Clock

            fenString.Append(board.HalfMoveClock);
            #endregion

            fenString.Append(recordSep);

            #region FullMove Counter

            fenString.Append(board.FullMoveCount);
            #endregion

            return(fenString.ToString());
        }
예제 #2
0
        public string GetFenFlags()
        {
            string s = EnPassant == SquareE.NoSquare ? "-" : EnPassant.ToString().ToLower();

            return(s + " " + GetFenCastlingFlags());
        }