How to use the chess.pgn.read_game function in chess

To help you get started, we’ve selected a few chess examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rpdelaney / python-chess-annotator / tests / test_functions.py View on Github external
def test_commented_game(self):
        pgn_string = "{ Stockfish 8 64 POPCNT } 1. Nf3 { test comment } Nf6 2. g3 g6 { A05 King's Indian Attack: Symmetrical Defense } 3. Bg2 Bg7 4. O-O O-O 5. c4 d6 6. b3 e5 7. Bb2 c5 8. e3 Nc6 9. Nc3 Bf5 10. d4 e4 11. Ne1 Re8 12. Nc2 h5 13. Qd2 h4 14. Ba3 $6 { -1.13 } ( 14. h3 g5 15. g4 Bg6 16. Rad1 Qe7 17. Qe2 a6 18. Ba3 a5 { 0.19/25 } ) 14...  b6 $6 { -0.04 } ( 14... Nh7 15. Nd5 Ng5 16. Bb2 Rc8 17. Rac1 Ne7 18. Nf4 h3 19.  Bh1 { -1.11/24 } ) 15. Rfd1 $6 { -1.15 } ( 15. h3 d5 16. g4 Be6 17. cxd5 Nxd5 18. Nxe4 f5 19. gxf5 gxf5 { 0.00/26 } ) 15... Bg4 16. Rdc1 Qd7 17. b4 Qf5 18.  Bb2 Rad8 19. Nb5 Bf3 20. d5 Ne5 $6 { -1.66 } ( 20... Nxb4 21. Ne1 Bxg2 22.  Nxg2 Nd3 23. Nxh4 Qh3 24. Bxf6 Bxf6 25. f4 { -3.14/25 } ) 21. Bxe5 Rxe5 22.  Ne1 hxg3 23. fxg3 Bh6 24. Rab1 Kg7 $6 { -1.08 } ( 24... Qh5 25. Rb3 Rf5 26.  bxc5 dxc5 27. Rc2 Ng4 28. h3 Bxg2 29. Kxg2 { -2.48/24 } ) 25. Rb3 Qh5 26. h3 $6 { -3.08 } ( 26. bxc5 bxc5 27. Nxa7 Rh8 28. h4 Qg4 29. Nc6 Rh5 30. Qf2 Bd1 { -2.00/23 } ) 26... Nh7 $2 { -1.37 } ( 26... Rg5 27. Qf2 { -2.89/24 }) 27. g4 Bxg4 28. hxg4 Qxg4 29. Qd1 $4 { -5.69 } ( 29. Qb2 Ng5 30. Nxd6 Qg3 31. Nf5+ gxf5 32. Kf1 Nf3 33. Qf2 Nh2+ { -2.30/24 } ) 29... Qg3 30. Qe2 Ng5 31. Kh1 Rh8 32. Nxd6 Kg8 33. bxc5 Bf8+ 34. Kg1 Nh3+ 35. Kf1 Bxd6 36. cxd6 Rf5+ 37. Nf3 Rxf3+ 0-1"  # noqa E501
        pgn = StringIO(pgn_string)
        game = chess.pgn.read_game(pgn)
        result = annotator.clean_game(game)
        node = result.end()
        while True:
            assert len(node.variations) <= 1
            assert node.comment is None
            assert node.nags == []
            if node == game.root():
                break
            node = node.parent
github crypt3lx2k / Zerofish / pgn_to_records.py View on Github external
def run_pgn (pgn_file, n_games, data_dir):
    games = 0

    while n_games == 0 or games < n_games:
        game = chess.pgn.read_game(pgn_file)
        game.headers['Counter'] = games
        name = '{White}-{Black}-{ECO}-{Date}-{Counter}'.format (
            **game.headers
        ).replace(' ', '_')

        # Loop exit condition
        if game is None:
            break

        # Run through game generating labels
        actions, policies, indices, outcome, winner = run_game(game)

        # Save labels to disk
        self_play.write_records(data_dir, name, actions, policies, indices, outcome, winner)

        # 
github saikrishna-1996 / deep_pepper_chess / game / pretrain.py View on Github external
def get_board_position():
    pgn = open("./game/kasparov.pgn")
    board_positions = []
    try:
        while True:
            kasgame = chess.pgn.read_game(pgn)
            if kasgame is None:
                break
            board = kasgame.board()
            board_positions.append(board.copy())
            for move in kasgame.main_line():
                board.push(move)
                board_positions.append(board.copy())
    except Exception:
        print("We have {} board positions".format(len(board_positions)))
        return board_positions
github johncheetham / jcchess / jcchess / utils.py View on Github external
def paste_game_from_clipboard(action):
    gamestr = get_text_from_clipboard()
    if gamestr is None:
        print("Error invalid game data")
        return
    pgn = StringIO(gamestr)
    game = chess.pgn.read_game(pgn)
    gv.load_save.load_game_pgn(game)
github johncheetham / jcchess / jcchess / pgn.py View on Github external
def load_game_pgn(self, fname):
        
        pgn = open(fname)
        first_game = chess.pgn.read_game(pgn)
        pgn.close()
        
        # Iterate through the mainline of this embarrasingly short game.
        node = first_game
        while not node.is_end():
            next_node = node.variation(0)
            print(node.board().san(next_node.move))
            node = next_node
        
        return
        
        #FIXME
        self.fname = fname

        # check if it's a multi-game file
        gamecnt = 0
github clarkerubber / Cheat-Net / modules / analysis / AnalysableGame.py View on Github external
def __init__(self, assessment, pgn):
        try:
            from StringIO import StringIO
        except ImportError:
            from io import StringIO
        self.assessment = assessment # PlayerAssessment
        self.game = chess.pgn.read_game(StringIO(pgn['pgn']))
        self.analysed = None
github thomasahle / fastchess / tools / tune.py View on Github external
        for game in iter((lambda: chess.pgn.read_game(file)), None):
            board = game.board()
github 2014mchidamb / AdversarialChess / process_data.py View on Github external
def gen_player_data(infile, outfile, player_name):
	# Game data
	pgn = open(infile)
	cur_game = chess.pgn.read_game(pgn)
	
	# Output
	out = h5py.File(outfile+'.hdf5', 'w')
	f_boards, s_boards = [
		out.create_dataset(dname, (0, 8, 8, 12), dtype='b',
							maxshape=(None, 8, 8, 12),
							chunks=True)
		for dname in ['f_boards', 's_boards']]
	p_color = [
		out.create_dataset(dname, (0,), dtype='b',
							maxshape=(None,),
							chunks=True)
		for dname in ['p_color']][0]
	full_boards = []	

	# Loop through games 
github luweizhang / chess-ai / model / parse_game.py View on Github external
def read_games(fn):
    f = open(fn)

    while True:
        try:
            g = chess.pgn.read_game(f)
        except KeyboardInterrupt:
            raise
        except:
            continue

        if not g:
            break
        
        yield g

chess

A chess library with move generation and validation, Polyglot opening book probing, PGN reading and writing, Gaviota tablebase probing, Syzygy tablebase probing, and XBoard/UCI engine communication.

GPL-3.0
Latest version published 10 months ago

Package Health Score

72 / 100
Full package analysis