Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create pgn from a game play #63

Closed
literadix opened this issue Jan 13, 2016 · 2 comments
Closed

How to create pgn from a game play #63

literadix opened this issue Jan 13, 2016 · 2 comments

Comments

@literadix
Copy link

Hello!

I am a new user of this API. Thank you very much. It is really a great work. I would like to ask a question which I could not solve: how can I create a PGN output from a running game: I have written following simple program as tournment of two compuer chess engines:

##########################################
# a very simple tournment between two uci engines
##########################################
import chess
import chess.uci

board = chess.Board()
engine = chess.uci.popen_engine("stockfish")
engine.uci()

while True:

    board.reset()

    while not board.is_game_over():
        engine.position(board)
        bestmove, ponder = engine.go(movetime=1000)
        # print bestmove
        board.push(bestmove)

    print board
    print board.fen()
    print board.result()

Now I would like not only to have a fen represenation when a game has finished, but also a pgn export. So my question is: how can I record all moves and checks and mates etc during a game play, so that I could have a pgn represantation of all moves when the game has ended?

A simple example would be welcome. You are also welcome to use my example and your pgn extension as general example for other users.

Thank you very much,

Maciej

@niklasf
Copy link
Owner

niklasf commented Jan 13, 2016

Hi.

Example 1: Add game nodes during play.

##########################################
# a very simple tournment between two uci engines
##########################################
import chess
import chess.uci
import chess.pgn

board = chess.Board()
engine = chess.uci.popen_engine("stockfish")
engine.uci()

while True:

    board.reset()

    # Create a game and set headers.
    game = chess.pgn.Game()
    game.headers["White"] = engine.name
    game.headers["Black"] = engine.name
    game.setup(board)  # Not required for the standard
                       # starting position.

    node = game

    while not board.is_game_over():
        engine.position(board)
        bestmove, ponder = engine.go(movetime=100)
        print bestmove
        board.push(bestmove)

        node = node.add_variation(bestmove) # Add game node

    print board
    print board.fen()
    print board.result()

    game.headers["Result"] = board.result()
    print game

Example 2: Create a game from the recorded moves.

def board_to_game(board):
    game = chess.pgn.Game()

    # Undo all moves.
    switchyard = collections.deque()
    while board.move_stack:
        switchyard.append(board.pop())

    game.setup(board)
    node = game

    # Replay all moves.
    while switchyard:
        move = switchyard.pop()
        node = node.add_variation(move)
        board.push(move)

    game.headers["Result"] = board.result()
    return game

Especially the second one should be easier. I'll see how I can improve this and update the documentation.

niklasf added a commit that referenced this issue Jan 13, 2016
@literadix
Copy link
Author

Niklas,

thank you very much. This is exactly what I have been looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants