コード例 #1
0
ファイル: analyze.py プロジェクト: nathanjhaveri/chessanalyze
def parse_game(pgn_text, pgn_extract_path):
    p = Popen([pgn_extract_path, "-Wuci"],
              stdin=PIPE,
              stdout=PIPE,
              stderr=PIPE)
    formatted_uci, stderr = p.communicate(pgn_text)

    game_pgn = pgn.loads(pgn_text)[0]
    game = pgn.loads(formatted_uci)[0]

    return game_pgn, game
コード例 #2
0
def make_dfs(filename, playername):

    pgn_text = open(filename).read()
    pgn_game = pgn.PGNGame()

    dat = pgn.loads(pgn_text)  # Returns a list of PGNGame
    sdat = pgn.dumps(pgn_game)  # Returns a string with a pgn game

    f_g = dat[0]
    games = list()

    for game in dat:
        keys = [key for key in dir(game) if not '_' in key]
        game_dict = dict()
        for key in keys:
            game_dict[key] = game.__getattribute__(key)
        games.append(game_dict)

    df = pd.DataFrame(games)
    split_timecontrol(df)

    df.whiteelo = pd.to_numeric(df.whiteelo, errors='coerce')
    df.blackelo = pd.to_numeric(df.blackelo, errors='coerce')

    #df.dropna(inplace=True)

    me_black = df.loc[df.black == playername]
    me_white = df.loc[df.white == playername]

    return me_black, me_white, df
コード例 #3
0
ファイル: game.py プロジェクト: jorgedclopes/chess-analytics
    def load_all_from_pgn(pgn_string) -> List[ChessGame]:
        games = [
            ChessGame.load_from_pgn_game(pgn_game)
            for pgn_game in pgn.loads(pgn_string)
        ]

        return games
コード例 #4
0
def load_parse_and_pickle_games(infile, outfile):
	games = None
	with codecs.open(infile, encoding='ascii', errors='replace') as f:
		contents = f.read()
		games = pgn.loads(contents)

	pickle.dump(games, open(outfile, 'wb'))
	return games
コード例 #5
0
ファイル: xfcc.py プロジェクト: ducksauce/chess-tools
def get_pgn():
    p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
    p.wait()
    pgn_text = p.stdout.read().replace('\r','\n')
    game = pgn.PGNGame()
    game = pgn.loads(pgn_text)[0]

    moves = strip_pgn(game)
    return moves
コード例 #6
0
ファイル: db.py プロジェクト: percentcer/chess.com
def pgns2csv(dir, name='games.csv'):
    """
    Reads all pgns in a given directory and compiles them into a csv
    @param dir: directory that houses the pgns
    """
    pgn_lists = [pgn.loads(open(os.path.join(dir, p)).read()) for p in os.listdir(dir) if p.endswith('.pgn')]
    games     = [game for sublist in pgn_lists for game in sublist]
    with open(name, 'w', newline='') as csvfile:
        writer = PGNWriter(csvfile)
        for game in games:
            writer.writegame(game)
コード例 #7
0
    def test_loads(self):
        '''Tests ``loads`` function'''

        text = '''
        [Site "Belgrade, Serbia Yugoslavia|JUG"]
        [Date "1234.32.32"]

        1. e4 e5 2. Nf3 Nc6 
        3. Bb5 1-0'''

        games = pgn.loads(text)
        assert len(games) == 1
コード例 #8
0
ファイル: test_pngparser.py プロジェクト: alephnull/pgnparser
    def test_loads(self):
        '''Tests ``loads`` function'''

        text = '''
        [Site "Belgrade, Serbia Yugoslavia|JUG"]
        [Date "1234.32.32"]

        1. e4 e5 2. Nf3 Nc6 
        3. Bb5 1-0'''

        games = pgn.loads(text)
        assert len(games) == 1
コード例 #9
0
ファイル: r2_chess_pgn.py プロジェクト: Charvelau/rosbook
 def playGame(self, pgn_filename):
   game = pgn.loads(open(pgn_filename).read())[0]
   self.setGrasp("pre-pinch")
   self.setSquare("a1", 0.15)
   for move in game.moves:
     self.setSquare(move[0:2], 0.10)
     self.setSquare(move[0:2], 0.015)
     self.setGrasp("pinch")
     self.setSquare(move[0:2], 0.10)
     self.setSquare(move[2:4], 0.10)
     self.setSquare(move[2:4], 0.015)
     self.setGrasp("pre-pinch")
     self.setSquare(move[2:4], 0.10)
コード例 #10
0
ファイル: parsePgn.py プロジェクト: souleiman2/ChessAI
 def __init__(self, file_game, file_data):
     '''
     L'utilité de cette classe sera de parser les files où il y a les données de milliers de games d'échecs pour que sa devienne lisible par notre programme string -> int[int[], int[]] pour lastPosition et position
     Ce programme ne sera pas exécuter en même temps que le programme d'échec (c'est runner indépendemment juste pour creer le data.txt qui sera utile au programme)
     :param file_game: c'est tout simplement le nom du fichier à parser
     '''
     pgn_text = open(file_game + ".pgn").read()
     self.file_data = file_data
     self.games = pgn.loads(pgn_text)
     self.board = None
     self.initPiece()
     self.tourBlanc = None
     self.promotedPiece = None
コード例 #11
0
 def playGame(self, pgn_filename):
     game = pgn.loads(open(pgn_filename).read())[0]
     self.setGrasp("pre-pinch")
     self.setSquare("a1", 0.15)
     for move in game.moves:
         self.setSquare(move[0:2], 0.10)
         self.setSquare(move[0:2], 0.015)
         self.setGrasp("pinch")
         self.setSquare(move[0:2], 0.10)
         self.setSquare(move[2:4], 0.10)
         self.setSquare(move[2:4], 0.015)
         self.setGrasp("pre-pinch")
         self.setSquare(move[2:4], 0.10)
コード例 #12
0
ファイル: xfcc.py プロジェクト: btrent/chess-tools
def get_pgn():
    global white_name, black_name

    p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
    p.wait()
    pgn_text = p.stdout.read().replace('\r', '\n')
    game = pgn.PGNGame()
    game = pgn.loads(pgn_text)[0]
    white_name = make_comparable(game.white)
    black_name = make_comparable(game.black)

    moves = strip_pgn(game)
    return moves
コード例 #13
0
def cleanup(file):
    print(file)
    pgn_text_file = open(os.getcwd() + '\\Games\\' + file, encoding="Latin-1")
    pgn_text = pgn_text_file.read()
    #print(pgn_text[0:100])
    games = pgn.loads(pgn_text)
    return games


#file = "rvkopen15r1.pgn"
#file = "rvkopen14r5.pgn"
#games = cleanup(file)
#for game in games:
#    print(game.moves)
コード例 #14
0
def pgns2csv(dir, name='games.csv'):
    """
    Reads all pgns in a given directory and compiles them into a csv
    @param dir: directory that houses the pgns
    """
    pgn_lists = [
        pgn.loads(open(os.path.join(dir, p)).read()) for p in os.listdir(dir)
        if p.endswith('.pgn')
    ]
    games = [game for sublist in pgn_lists for game in sublist]
    with open(name, 'w', newline='') as csvfile:
        writer = PGNWriter(csvfile)
        for game in games:
            writer.writegame(game)
コード例 #15
0
def cleanup(file):
    print(file)
    pgn_text_file = open(os.getcwd() + '\\Games\\' + file, encoding="Latin-1")
    pgn_text = pgn_text_file.read()
    #print(pgn_text[0:100])
    games = pgn.loads(pgn_text)
    return games


#file = "rvkopen15r1.pgn"
#file = "rvkopen14r5.pgn"
#games = cleanup(file)
#for game in games:
#    print(game.moves)
コード例 #16
0
ファイル: lss_move.py プロジェクト: btrent/chess-tools
def get_pgn():
    global white_player
    global black_player

    p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
    p.wait()
    pgn_text = p.stdout.read().replace('\r','\n')
    game = pgn.PGNGame()
    game = pgn.loads(pgn_text)[0]

    white_player = game.white.split(',')[0]
    black_player = game.black.split(',')[0]

    moves = strip_pgn(game)
    return moves
コード例 #17
0
ファイル: chesstail.py プロジェクト: edwkar/chesstail
    def run(self):
        should_continue = True
        last_moves = None

        iteration = -1
        while should_continue:
            iteration += 1

            try:
                text = read_url(CHESS_COM +
                                'echess/download_pgn?id=%d' % self._game_id)
            except Exception as e:
                debug_log('ERROR: failed to read game data from chess.com :(')
                #print e
                #print type(e)
                sleep(ERROR_SLEEP_TIME)
                continue

            game = pgn.loads(text)[0]
            moves = self._trim_moves(game.moves)
            num_moves = len(moves)

            if iteration == 0:
                debug_log('started tracking game %d between %s and %s' % (
                          self._game_id,
                          game.white,
                          game.black,))

            msg_end = None

            if game.result in ['1-0', '0-1', '1/2-1/2']:
                msg_end = 'ended with score %s after %d moves' % (game.result,
                                                                  num_moves,)
                should_continue = False
            elif moves and moves != last_moves:
                msg_end = '%2d. %s%s' % (1 + (num_moves-1) // 2,
                                         '' if num_moves % 2 == 1 else '...',
                                         moves[-1],)
            last_moves = moves[:]

            if msg_end and iteration != 0:
                with PRINT_LOCK:
                    sys.stdout.write('%25s | %-16s\n' % (
                            '%s vs. %s' % (game.white, game.black),
                            msg_end,))
                    sys.stdout.flush()

            sleep(POLLING_INTERVAL)
コード例 #18
0
def parse_pgn(filename):
    path = os.path.join(filename)
    pgn_text = open(path).read()
    #pgn_game = pgn.PGNGame()
    games = pgn.loads(pgn_text)
    for game in games:
        #print game.moves
        #moves = game.moves[0]+" "+game.moves[1]+" "+game.moves[2]+" "+game.moves[3]
        #print game.result
        if (game.result != None and game.moves != None and game.white != None):
            res = parse_res(game.result)
            eco_ = eco.from_moves(game.moves)[0][0]
            dataset = create_eco_res()
            playername = game.white
            eco_res = cal_eco_res(dataset, eco_, res)
            save_eco(playername, eco_res)
コード例 #19
0
ファイル: main.py プロジェクト: quake0day/Equinight
def parse_pgn(filename):
    path = os.path.join(filename)
    pgn_text = open(path).read()
    #pgn_game = pgn.PGNGame()
    games = pgn.loads(pgn_text)
    for game in games:
        #print game.moves
        #moves = game.moves[0]+" "+game.moves[1]+" "+game.moves[2]+" "+game.moves[3]
        #print game.result
        if(game.result != None and game.moves != None and game.white != None):
            res = parse_res(game.result)
            eco_ = eco.from_moves(game.moves)[0][0]
            dataset = create_eco_res()
            playername = game.white
            eco_res = cal_eco_res(dataset, eco_, res)
            save_eco(playername, eco_res)
コード例 #20
0
ファイル: kurnikbot.py プロジェクト: michal3141/kurnikbot
    def tap(self, keycode, character, press):
        # If 'n' is pressed then next move is played by allmighty SF
        if character == 'n' and press:
            print '---------------------------'
            try:
                pgn_text = get_pgn()
                game = pgn.loads(pgn_text)[0]
                if game.white == me: side = Player.WHITE
                else: side = Player.BLACK 
            except:
                print 'Issue with PGN:', pgn_text
                return

            print 'Moves: ', game.moves

            board = Bitboard()
            for move in game.moves:
                if move != '*':
                    board.push_san(move)

            fen_pos = board.fen()
            print 'FEN: ', fen_pos

            ## Set fen position to stockfish, find the best move

            stock.setposition_fen(fen_pos)
            move_dict = stock.bestmove()
            print 'move_dict: ', move_dict

            move = move_dict['move']

            ## Try to play the best move
            try:
                make_move(move, side)
            except:
                print 'Problem playing move!'

        # By pressing number from 1-9 you can set how long SF could possibly
        # think on a particular move (in seconds)
        elif character in DIGITS and press:
            print 'pressed: ', character
            stock.movetime = int(character) * 1000
コード例 #21
0
def resultat(fichier):
	try :
		texte = open(fichier).read()
	except:
		print "Fichier : "+fichier+" n'existe pas"
		return
	print "   Analise de "+fichier
	jeux = pgn.loads(texte)
	for jeu in jeux:
		res= "L'evenement "+ jeu.event
		res += " a eu lieu a " + jeu.site
		res += " le " + jeu.date
		blanc = jeu.white.split(", ")
		if len(blanc) == 2:
			res += "\nIl a vu s'affronter "+ blanc[1] +" "+blanc[0]
		else :
			res += "\nIl a vu s'affronter "+ blanc[0]
		noir = jeu.black.split(", ")
		if len(noir) == 2:
			res += " et "+ noir[1]+ " "+ noir[0]
		else :
			res += " et "+ noir[0]
			
		res+="\n\n"
		if jeu.result == "1-0":
			res += "victoire de "
			if len(blanc) == 2:
				res += blanc[1] +" "+blanc[0]
			else :
				res +=blanc[0]
		elif jeu.result == "0-1":
			res += "victoire de "
			if len(noir) == 2:
				res +=noir[1]+ " "+ noir[0]
			else :
				res +=noir[0]
		elif jeu.result == "1/2-1/2":
			res += "match nul"
		elif jeu.result == "*":
			res += "parti en cour"
		print res+"\n"
コード例 #22
0
def read_pgnx(data, verbose=False, min_annot=3):
    """Read pgnx file, filter games with fewer than min_annot annotations"""

    game_strs = data.split("<==>\n")
    games = []

    for game_str in game_strs:
        # try to parse the line
        try:
            player, rating, pgn_str = game_str.split("\n",2)
        except:
            if verbose:
                stderr.write("game unparsable: %s\n" % game_str)
            continue

        # remove punctuation from rating, e.g., 900+
        rating = rating.strip(punctuation)

        # skip non-numerical ratings
        if not rating.isdigit():
            if verbose:
                stderr.write("dropping game with non-integer rating: %s\n" % rating)
            continue
        rating = int(rating)

        # skip ratings that are unrealistic
        if rating > 3000 or rating < 500:
            if verbose:
                stderr.write("rating unrealistic: %d\n" % rating)
            continue

        # load the pgn and extract annotations
        game_pgn = pgn.loads(pgn_str)[0]
        annotations = [move for move in game_pgn.moves if move.strip().startswith("{")]
        if len(annotations) < min_annot:
            if verbose:
                stderr.write("dropping game with too few annotations\n")
            continue
        games.append((player, rating, game_pgn, annotations))
    
    return games
コード例 #23
0
ファイル: chess_play.py プロジェクト: butwhywhy/ASCIIchess
    def fromPGN(cls, PGN):
        import pgn
        pgnfields = pgn.loads(PGN)[0]

        FEN = pgnfields.fen
        position = Position.fromFEN(FEN) if FEN else None

        game = cls(init_pos=position, 
                event=pgnfields.event,
                white_player=pgnfields.white, 
                black_player=pgnfields.black)

        results = [WHITE_WINS, BLACK_WINS, DRAW]
        for m in pgnfields.moves:
            # Omitting comments ...
            if '{' in m:
                continue
            # and result
            if m in results:
                break
            game.move(m)
        return game
コード例 #24
0
ファイル: precalc.py プロジェクト: ernop/chessui-960
def calculate_blunders(pgnpath):
    global engine
    FIRST = 0
    assert os.path.exists(pgnpath)
    pgntext = open(pgnpath, 'r').read()
    gamepgn= pgn.loads(pgntext)[0]
    #test gameresult.
    log.info('got game %s', gamepgn)
    LAST = GLOBAL_LAST or len(gamepgn.moves)
    states = {}
    for n in range(FIRST, LAST):
        try:
            move = gamepgn.moves[n]
            if move in ['1-0', '0-1', '1/2-1/2']:
                break
            states[n] = engine.evalposition(gamepgn, movenum=n)
        except Exception, e:
            import ipdb;ipdb.set_trace()
            try:
                states[n] = engine.evalposition(gamepgn, movenum=n)
            except Exception, ee:
                pass
            break
コード例 #25
0
 def read_files(self):
     """Read games from specified files"""
     for f in self.filenames:
         self.games.extend(pgn.loads(open(f).read()))
コード例 #26
0
import pgn
import sys

# Event, Site, Date, Round, White, Black and Result.


def sanitize(s):
    return s.replace("\t", " ")


for fn in sys.argv[1:]:
    with file(fn) as f:
        for game in pgn.loads(f.read()):
            try:
                print("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}".format(
                    sanitize(game.event), sanitize(game.site),
                    sanitize(game.date), sanitize(game.round),
                    sanitize(game.white), sanitize(game.black),
                    sanitize(game.result), sanitize(game.whiteelo),
                    sanitize(game.blackelo)))
            except AttributeError:
                pass
コード例 #27
0
            directory[row[4]] = (section, row[3])
            section_set[section].add(row[4])


# Sort the twic files numerically so that the games appear
# in chronological order in the output files
def twic_key(a):
    return int(os.path.splitext(os.path.basename(a))[0][len('twic'):])


# Loop over all TWIC games, finding the interesting ones
# and adding them to the appropriate output file(s)
for pgn_filename in sorted(glob.glob(os.path.join('twic', '*pgn')),
                           key=twic_key):
    print(pgn_filename)
    games = pgn.loads(open(pgn_filename).read())
    for game in games:
        white_id = getattr(game, 'whitefideid', None)
        black_id = getattr(game, 'blackfideid', None)
        white_file = interesting.get(white_id)
        black_file = interesting.get(black_id)
        if white_file:
            found.add(white_id)
            print('Found game {} in {}'.format(game, pgn_filename))
            print(game.dumps() + '\n\n', file=white_file)
        if black_file and black_file != white_file:
            found.add(black_id)
            print('Found game {} in {}'.format(game, pgn_filename))
            print(game.dumps() + '\n\n', file=black_file)

# Come up with some stats on how well "covered" the field is
コード例 #28
0
         if 'result "1-0"' in content.lower():
             white += 1
         if 'result "0-1"' in content.lower():
             black += 1
         if 'event' in content.lower():
             games += 1
 draw = games - (black + white)
 print("This file has " + str(games) + " games being analyzed.")
 print("White has won " + str(white) + " games/ " +
       str(100 * float(white) / float(games)) + "%")
 print("Black has won " + str(black) + " games/ " +
       str(100 * float(black) / float(games)) + "%")
 print("There has been " + str(draw) + " draws/ " +
       str(100 * float(draw) / float(games)) + "%")
 with open(os.path.join(roots, fName)) as cFile:
     aGames = pgn.loads(cFile.read())
     countList = []
     for game in aGames:
         counts = "{} vs {}, {} moves.".format(game.white, game.black,
                                               len(game.moves))
         print(counts)
         countList += [len(game.moves)]
     print("The min value of moves for all games in " + fName +
           " = {}".format(min(countList)))
     print("The median value of moves for all games in " + fName +
           " = {}".format(median(countList)))
     print("The mean value of moves for all games in " + fName +
           " = {}".format(mean(countList)))
     print("The max value of moves for all games in " + fName +
           " = {}".format(max(countList)))
     print("The 50th percentile value for moves in " + fName +
コード例 #29
0
ファイル: benny.py プロジェクト: cybermouflons/CCSC-CTF-2021
def parse_pgn(pgn_string: str, prng: PRNG, internal_pypi_url: str,
              public_pypi_url: str):

    # Generate "random" package name
    package_name = prng.next_as_hex()

    # Download dependencies...
    logs = ""

    internal_pypi_host = internal_pypi_url.split("//")[1].split("/")[0].split(
        ":")[0]
    public_pypi_host = public_pypi_url.split("//")[1].split("/")[0].split(
        ":")[0]
    command = [
        'pip', 'install', '--index-url', f'{public_pypi_url}/simple',
        '--extra-index-url', f'{internal_pypi_url}/simple/', '--trusted-host',
        f'{internal_pypi_host}', '--trusted-host', f'{public_pypi_host}',
        package_name
    ]

    logs += "=> " + " ".join(command) + "\n"
    process = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    out, err = process.communicate()
    logs += out.decode("utf-8").rstrip()
    logs += err.decode("utf-8").rstrip()

    if err != b"":
        return False, [], logs

    # Parse pgn
    logs += "\n=> Benny is running his python scripts ...."
    thrown_exception = False
    try:
        import pgn
        # importlib.reload(pgn)

        parsed_games = pgn.loads(pgn_string)
        logs += "\n=> Result from parsing: {0}".format(parsed_games)
    except Exception as e:
        logs += "\n[ERROR] Error parsing provided pgn ...."
        logs += "\n{0}: {1}".format(type(e).__name__, e)
        thrown_exception = True

    # Try to delete module
    try:
        del sys.modules["pgn"]
        del pgn
    except Exception:
        pass

    if thrown_exception:
        return False, [], logs

    logs += "\n[SUCCESS] PGN parsed successfully!"

    # Uninstall depdencies
    command = ['pip', 'uninstall', '-y', package_name]

    logs += "\n=> " + " ".join(command) + "\n"
    process = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    out, err = process.communicate()
    logs += out.decode("utf-8").rstrip()
    logs += err.decode("utf-8").rstrip()

    return True, parsed_games, logs
コード例 #30
0
def parse_games(database, depth, custom_branching, color, name):
    database = open(database)
    pgn_text = database.read()
    database.close()

    color = color.lower()

    games = pgn.loads(pgn_text)

    all_games = []
    all_ratios = []
    kick_depth = 0

    name = name.split(' ')
    #od
    if len(name) >= 2:
        for i in range(len(name)):
            name.append(name[i][0])

    for game in games:
        if color == 'white' or color == 'w':
            other_name = str(game.white)
            other_name = other_name.replace(',', ' ')
            other_name = other_name.replace('  ', ' ')
            other_name = other_name.split(' ')

            if set(other_name).issubset(set(name)):
                if len(game.moves) >= depth + 2:
                    all_games.append(game.moves)

        elif color == 'black' or color == 'b':
            other_name = str(game.black)
            other_name = other_name.replace(',', ' ')
            other_name = other_name.replace('  ', ' ')
            other_name = other_name.split(' ')

            if set(other_name).issubset(set(name)):
                if len(game.moves) >= depth + 2:
                    all_games.append(game.moves)

        else:
            if name == ['']:
                if len(game.moves) >= depth + 2:
                    all_games.append(game.moves)

            else:
                other_name = str(game.white)
                other_name = other_name.replace(',', ' ')
                other_name = other_name.replace('  ', ' ')
                other_name = other_name.split(' ')

                other_name2 = str(game.black)
                other_name2 = other_name2.replace(',', ' ')
                other_name2 = other_name2.replace('  ', ' ')
                other_name2 = other_name2.split(' ')

                if set(other_name).issubset(
                        set(name)) or set(other_name2).issubset(set(name)):
                    if len(game.moves) >= depth + 2:
                        all_games.append(game.moves)

    for i in range(len(all_games)):
        all_games[i].pop(-2)
        all_ratios.append(all_games[i][-1])
        all_games[i].pop(-1)

    if custom_branching:
        a = input('Custom Branching: ')
        a = list(a.split())

        del_list = []
        kick_depth = len(a)

        for i in range(len(all_games)):
            b = all_games[i][0:len(a)]
            if a == b:
                pass
            else:
                del_list.append(i)

        all_games = [
            all_games[i] for i in range(len(all_games)) if i not in del_list
        ]
        all_ratios = [
            all_ratios[i] for i in range(len(all_ratios)) if i not in del_list
        ]

    return all_games, all_ratios, kick_depth
コード例 #31
0
ファイル: headers.py プロジェクト: bodhi-baum/ChessData
import pgn
import sys

n = 0
bad = 0
# Event, Site, Date, Round, White, Black and Result.

for game in pgn.loads(file(sys.argv[1]).read()):
    try:
        print("{},{},{},{},{},{},{},{},{}".format(game.event, game.site,
                                                  game.date, game.round,
                                                  game.white, game.black,
                                                  game.result, game.whiteelo,
                                                  game.blackelo))
    except AttributeError:
        bad += 1
        print("{}".format(game.dumps()))
    n += 1

print("n={}".format(n))
print("bad={}".format(bad))
コード例 #32
0
            interesting[row[4]] = outfile
            directory[row[4]] = (section, row[3])
            section_set[section].add(row[4])


# Sort the twic files numerically so that the games appear
# in chronological order in the output files
def twic_key(a):
    return int(os.path.splitext(os.path.basename(a))[0][len("twic") :])


# Loop over all TWIC games, finding the interesting ones
# and adding them to the appropriate output file(s)
for pgn_filename in sorted(glob.glob(os.path.join("twic", "*pgn")), key=twic_key):
    print(pgn_filename)
    games = pgn.loads(open(pgn_filename).read())
    for game in games:
        white_id = getattr(game, "whitefideid", None)
        black_id = getattr(game, "blackfideid", None)
        white_file = interesting.get(white_id)
        black_file = interesting.get(black_id)
        if white_file:
            found.add(white_id)
            print("Found game {} in {}".format(game, pgn_filename))
            print(game.dumps() + "\n\n", file=white_file)
        if black_file and black_file != white_file:
            found.add(black_id)
            print("Found game {} in {}".format(game, pgn_filename))
            print(game.dumps() + "\n\n", file=black_file)

# Come up with some stats on how well "covered" the field is
コード例 #33
0
def parseGames(gameText):
  game = pgn.PGNGame()
  games = pgn.loads(gameText)
  return games
コード例 #34
0
ファイル: pgn_parser.py プロジェクト: Nixonite/chesscomputer
# get pgn here: https://github.com/renatopp/pgnparser
# download and install locally with easy_install

import pgn
import sys

if(len(sys.argv) < 2 ):
	print("usage: python pgn_parser.py <path/to/file.pgn>")
	exit()

pgn_text = open(sys.argv[1]).read()
games = pgn.loads(pgn_text)

print("PGN parsed successfully")
print("{0} games found".format(len(games)))
コード例 #35
0
ファイル: util.py プロジェクト: ashudeep/ConvChess
def get_all_games(file_name):
	pgn_file = open(file_name)
	pgn_text = pgn_file.read()
	pgn_file.close()
	return pgn.loads(pgn_text)
コード例 #36
0
ファイル: PGN.py プロジェクト: crawfonw/ChessPye
def get_list_of_moves_from_pgn(pgn_path): #includes comments & W/L/D result
    pgn_text = open(pgn_path).read()
    pgn_games = pgn.loads(pgn_text)
    return [game.moves for game in pgn_games]
コード例 #37
0
# -*- coding: utf-8 -*-
# @Time    : 2018/1/26 17:21
# @Author  : play4fun
# @File    : pgnparser1.py
# @Software: PyCharm
"""
pgnparser1.py:
"""

import pgn

# pgn_text = open('morphy.pgn').read()
pgn_text = open('example.pgn').read()
pgn_game = pgn.PGNGame()

x = pgn.loads(pgn_text)
print(x)  # Returns a list of PGNGame
# [<PGNGame "Fischer, Robert J." vs "Spassky, Boris V.">]
d = pgn.dumps(pgn_game)
print(d)  # Returns a string with a pgn game
'''
[<PGNGame "Fischer, Robert J." vs "Spassky, Boris V.">]
[Event "?"]
[Site "?"]
[Date "?"]
[Round "?"]
[White "?"]
[Black "?"]
[Result "?"]
'''
コード例 #38
0
ファイル: headers.py プロジェクト: rozim/ChessData
import pgn
import sys

n = 0
bad = 0
# Event, Site, Date, Round, White, Black and Result.

for game in pgn.loads(file(sys.argv[1]).read()):
    try:
        print("{},{},{},{},{},{},{},{},{}".format(
            game.event,
            game.site,
            game.date,
            game.round,
            game.white,
            game.black,
            game.result,
            game.whiteelo,
            game.blackelo))
    except AttributeError:
        bad += 1
        print("{}".format(game.dumps()))
    n += 1          

print("n={}".format(n))
print("bad={}".format(bad))    


コード例 #39
0
ファイル: util.py プロジェクト: hal2001/ConvChess
def get_all_games(file_name):
    pgn_file = open(file_name)
    pgn_text = pgn_file.read()
    pgn_file.close()
    return pgn.loads(pgn_text)
コード例 #40
0
ファイル: stats.py プロジェクト: adamkasztenny/chess-stats
import glob

pgn_text = ''
print('Opening pgn files')

# the example on https://github.com/renatopp/pgnparser for large pgn files did not work
# as a result, this is pretty slow (even with only 1 1mb pgn file)

# thanks to http://stackoverflow.com/questions/5013532/open-file-by-filename-wildcard
path = "data/*.pgn"
for filename in glob.glob(path):
    with open(filename, 'r') as f:
        pgn_text += f.read()

pgn_games = pgn.PGNGame()
games = pgn.loads(pgn_text)
number_of_games = len(games)

print(str(number_of_games) + " games loaded.\n")

results = map(lambda x: x.result, games)


def win_rate(result):
    return (float(len(filter(lambda x: x == result, results))) /
            number_of_games) * 100


white_wins = win_rate('1-0')
black_wins = win_rate('0-1')
draws = win_rate('1/2-1/2')