[python] king's square at checkmate
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import chess.pgn
- import pandas as pd
- import time
- # king position at checkmate
- def parse_checkmate_squares(filepath):
- pgns = open(filepath)
- # checkmate_indices = []
- checkmate_games = []
- checkmate_squares = []
- checkmated_color = []
- checkmate_lists = [checkmate_games, checkmate_squares, checkmated_color]
- i = 0
- while i < 1000000:
- gm = chess.pgn.read_game(pgns)
- try:
- if '#' in gm.end().san():
- checkmated_color.append(gm.end().turn())
- checkmate_games.append(gm.headers['Site'])
- checkmate_squares.append(list(gm.end().board().pieces(chess.KING, checkmated_color[-1]))[0])
- except AttributeError: # some cases, like abandons, where no moves are ever played which causes errors
- continue
- i += 1
- color_map = {True:'White', False:'Black'}
- col_names = ['game', 'square', 'color']
- df = pd.DataFrame(checkmate_lists)
- df = df.transpose()
- df.columns = col_names
- df['color'] = df['color'].map(color_map)
- pgns.close()
- return df
- start_time = time.time()
- filepath = #pgns from lichess
- df = parse_checkmate_squares(filepath)
- print("--- %s seconds ---" % (time.time() - start_time))
- df.to_csv('checkmate_squares.csv')
Editor
You can edit this paste and save as new: