Пример #1
0
        // this parsing is too simplistic to catch all the nuances of proper PGN, but it works for simple examples,
        // and it might even get complex examples correct(but it shouldn't relied upon)
        public static void parseBody(string body, ChessGame game)
        {
            body = removeComments(body);
            body = body.Replace("\n", " ");
            body = body.Replace("\r", " ");
            var parts = body.Split(" ");
            var moves = game.Moves();

            foreach (var part in parts)
            {
                if (part.Contains("."))
                {
                    continue;
                }
                var move = moves.Find(move => move.san == part.Trim());
                if (move != null)
                {
                    game.Move(move.move);
                    moves = game.Moves();
                }
            }
        }
Пример #2
0
        public static ChessState GameToState(ChessGame game)
        {
            var chessState = new ChessState();
            //var moves = board.GetMoves().Where(move => board.IsLegalMove(move));
            var moves = game.Moves();

            for (int column = 0; column < 8; column++)
            {
                for (int row = 0; row < 8 * BoardStateOffset.ROW_OFFSET; row += BoardStateOffset.ROW_OFFSET)
                {
                    var   position = row + column;
                    Piece piece    = game.board.GetPiece(position);
                    if (piece != Piece.EMPTY)
                    {
                        var from       = BoardPosition.x88PositionToCoordinate(position);
                        var chessPiece = new ChessPiece()
                        {
                            piece   = PieceParser.ToChar(piece).ToString(),
                            row     = from.row,
                            column  = from.column,
                            isWhite = (piece & Piece.IS_WHITE) == Piece.IS_WHITE
                        };

                        chessState.pieces.Add(chessPiece);

                        foreach (var move in moves)
                        {
                            if (move.move.fromPosition == position)
                            {
                                var to = BoardPosition.x88PositionToCoordinate(move.move.targetPosition);
                                chessPiece.options.Add(new PieceOption()
                                {
                                    row         = to.row,
                                    column      = to.column,
                                    isPromotion = (Piece)move.move.promotion != Piece.EMPTY,
                                    isCastle    = ((MoveFlags)move.move.moveFlags & MoveFlags.CASTLING) == MoveFlags.CASTLING,
                                    isEnpassant = (((MoveFlags)move.move.moveFlags & MoveFlags.ENPASSANT) == MoveFlags.ENPASSANT),
                                    san         = move.san
                                });
                            }
                        }
                    }
                }
            }
            var winner = game.Winner();

            if (winner == Winner.DRAW)
            {
                chessState.isDraw = true;
            }
            else if (winner == Winner.WINNER_BLACK)
            {
                chessState.blackWins = true;
            }
            else if (winner == Winner.WINNER_WHITE)
            {
                chessState.whiteWins = true;
            }

            chessState.fen = game.FEN;

            return(chessState);
        }