代码之家  ›  专栏  ›  技术社区  ›  Basj

如何得到一个棋盘从一个最终的立场从读\游戏?

  •  1
  • Basj  · 技术社区  · 4 年前

    这里我们成功解析了一个PGN游戏:

    import chess, chess.svg, chess.pgn, io
    game = chess.pgn.read_game(io.StringIO("1. e4 e5 2. Nf3 *"))
    print(game)
    

    board() 仍然是初始位置。如何获得最终位置?

    print(game.board())
    # r n b q k b n r
    # p p p p p p p p
    # . . . . . . . .
    # . . . . . . . .
    # . . . . . . . .
    # . . . . . . . .
    # P P P P P P P P
    # R N B Q K B N R
    
    1 回复  |  直到 4 年前
        1
  •  2
  •   Basj    4 年前

    python-chess documentation 给出以下示例:

    >>> import chess.pgn
    >>>
    >>> pgn = open("data/pgn/kasparov-deep-blue-1997.pgn")
    >>>
    >>> first_game = chess.pgn.read_game(pgn)
    >>> second_game = chess.pgn.read_game(pgn)
    >>>
    >>> first_game.headers["Event"] 
    'IBM Man-Machine, New York USA'
    >>>
    >>> # Iterate through all moves and play them on a board.
    >>> board = first_game.board()
    >>> for move in first_game.mainline_moves(): 
    ...     board.push(move) 
    ...
    >>> board 
    Board('4r3/6P1/2p2P1k/1p6/pP2p1R1/P1B5/2P2K2/3r4 b - - 0 45') 
    

    mainline_moves 把他们推到一块木板上 game.board() ),然后打印那块板。

    或者,您可以使用 game.end()

    game.end().board()
    
    推荐文章