-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_square_control.py
More file actions
42 lines (32 loc) · 1.57 KB
/
project_square_control.py
File metadata and controls
42 lines (32 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import chess # python-chess.readthedocs.io
import chess.pgn
from collections import defaultdict
from visualize_board import plot_color_sum_per_square
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-g', '--games', help="Either 'first' to analyze only the first game or 'all' for an aggregate", default='first')
parser.add_argument('-d', '--debug', help="Whether to debug", action='store_true', default=False)
args = parser.parse_args()
total_color_sum_per_square = defaultdict(int)
# Obtained from http://smallchess.com/Games/
with open("Magnus_Carlsen.pgn") as pgn:
number_of_games = 0
while True:
game = chess.pgn.read_game(pgn)
if not game:
break
number_of_games += 1
if number_of_games % 100 == 0:
print('Number of games loaded: ', number_of_games)
board = game.board()
color_sum_per_square = defaultdict(int)
for move in game.mainline_moves():
board.push(move)
piece_map = board.piece_map()
for square, piece in piece_map.items():
color_sum_per_square[square] += (1 if piece.color == chess.WHITE else -1)
total_color_sum_per_square[square] += (1 if piece.color == chess.WHITE else -1)
if args.games == 'first':
plot_color_sum_per_square(color_sum_per_square, title='Relative difference in player control')
exit()
plot_color_sum_per_square(total_color_sum_per_square, title='Relative difference in player control')