Skip to content

Instantly share code, notes, and snippets.

@void4
Created December 9, 2020 18:53
Show Gist options
  • Save void4/96b601a91eab5b5478c8f0018801bbd3 to your computer and use it in GitHub Desktop.
Save void4/96b601a91eab5b5478c8f0018801bbd3 to your computer and use it in GitHub Desktop.
import chess
import chess.pgn
import chess.engine
from chess import BLACK, WHITE
engine = chess.engine.SimpleEngine.popen_uci("./stockfish_20090216_x64_avx2")
import bz2
path = "lichess_db_standard_rated_2017-04.pgn.bz2"
f = bz2.open(path, "rt", encoding="ascii")
print("Opened", path)
#for i in range(1000):
# print(f.readline())
#exit(1)
white = 0
black = 0
while True:
game = chess.pgn.read_game(f)
term = game.headers["Termination"]
#if term in ["Normal", "Time forfeit", "Abandoned", "Rules infraction", "Unterminated"]:
if term != "Normal":
#if term not in ["Time forfeit"]:
#print(term)
continue
result = game.headers["Result"]
if result not in ["1-0", "0-1"]:
continue
lastmove = list(game.mainline())[-1]
# Avoid simulating the game if it is a checkmate
if lastmove.san().endswith("#"):
continue
board = game.board()
for move in game.mainline_moves():
board.push(move)
info = engine.analyse(board, chess.engine.Limit(time=0.1))
#print(info)
score = info["score"]
if score.is_mate():
#print(lastmove.san())
continue
newmin = False
if result[0] == "1":
# black resigned
if score.black().cp > black:
black = score.black().cp
newmin = True
else:
# white resigned
if score.white().cp > white:
white = score.white().cp
newmin = True
if newmin:
print(game.headers)
print(score, result, white, black)
print(board)
print(board.fen())
"""
board = chess.Board()
while not board.is_game_over():
result = engine.play(board, chess.engine.Limit(time=0.1))
board.push(result.move)
print(board)
engine.quit()
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment