Skip to content

Instantly share code, notes, and snippets.

Last active July 3, 2018 18:52
Show Gist options
  • Save ianfab/9724ca4f26d86870ba45e66e7de02cb8 to your computer and use it in GitHub Desktop.
Save ianfab/9724ca4f26d86870ba45e66e7de02cb8 to your computer and use it in GitHub Desktop.
Analyze chess960 starting positions for chess or chess variants.
import argparse
import csv
import chess, chess.variant, chess.uci
def parse_args():
parser = argparse.ArgumentParser(description='Evaluate atomic960 positions.')
parser.add_argument('-e', '--engine', required=True, help='Path to engine.')
parser.add_argument('-o', '--output', required=True, help='Output csv file.')
parser.add_argument('-d', '--depth', type=int, default=10, help='Search depth.')
parser.add_argument('-m', '--memory', type=int, default=16, help='Hash size in MB.')
parser.add_argument('-a', '--start', type=int, default=0, help='Start from this position.')
parser.add_argument('-b', '--stop', type=int, default=959, help='Stop at this position.')
parser.add_argument('-v', '--variant', type=str, default='atomic', help='Variant name.')
return parser.parse_args()
def main(args):
board = chess.variant.find_variant(args.variant)()
engine = chess.uci.popen_engine(args.engine)
info_handler = chess.uci.InfoHandler()
engine.setoption({'UCI_Variant': board.uci_variant, 'UCI_Chess960': True, 'Hash': args.memory})
with open(args.output, 'w') as csv_file:
columns = ['index', 'FEN', 'depth', 'score']
csv_writer = csv.DictWriter(csv_file, fieldnames=columns, dialect='excel-tab')
print('Starting to analyze positions %d to %d.\nSettings: depth=%d, hash=%dMB'
% (args.start, args.stop, args.depth, args.memory))
for i in range(args.start, args.stop + 1):
print('Analyzing position %d.' % i)
score = (["score"][1].cp if["score"][1].cp is not None
else ('#%s' %["score"][1].mate))
csv_writer.writerow({'index': i, 'FEN': board.fen(), 'depth': args.depth, 'score': score})
print('Done.\nOutput written to file %s.' % args.output)
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment