-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_square_tension.py
More file actions
94 lines (78 loc) · 3.79 KB
/
project_square_tension.py
File metadata and controls
94 lines (78 loc) · 3.79 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import chess # python-chess.readthedocs.io
import chess.pgn
from collections import defaultdict
from visualize_board import plot_color_sum_per_square, PlotType, ScaleType
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()
class Piece:
def __init__(self, piece, square):
self.orig_square = square
self.color = piece.color
self.curr_square = square
self.tension_turns = 0
self.name = piece.symbol()
self.ded = False
def opposite_color(color):
return chess.BLACK if color == chess.WHITE else chess.WHITE
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()
pieces_stats = []
piece_map = board.piece_map()
for square, piece in piece_map.items():
pieces_stats.append(Piece(piece, square))
color_sum_per_square = defaultdict(int)
for move in game.mainline_moves():
is_capture = False
if board.is_capture(move):
is_capture = True
pieces_attacked = defaultdict(int)
for piece in pieces_stats:
is_piece_attacked = board.is_attacked_by(opposite_color(piece.color), piece.curr_square)
can_piece_be_captured = (board.turn == opposite_color(piece.color) and is_piece_attacked)
is_piece_captured = (move.to_square == piece.curr_square and is_capture)
if not piece.ded and can_piece_be_captured and not is_piece_captured:
piece.tension_turns += 1
if args.debug:
print('piece at ', chess.square_name(piece.curr_square), ' is in tension: ', piece.name)
board.push(move)
if args.debug:
print(board)
input()
# A piece died! Update it.
if is_capture:
for piece in pieces_stats:
if move.to_square == piece.curr_square and not piece.ded:
piece.ded = True
break
for piece in pieces_stats:
# Update the moved piece.
if move.from_square == piece.curr_square and not piece.ded:
piece.curr_square = move.to_square
break
for square in range(64):
if square >= 16 and square < (64-16):
color_sum_per_square[square] = -1
total_color_sum_per_square[square] = -1
else:
for piece in pieces_stats:
if square == piece.orig_square:
color_sum_per_square[square] = piece.tension_turns
total_color_sum_per_square[square] += piece.tension_turns
break
if args.games == 'first':
plot_color_sum_per_square(color_sum_per_square, title='Piece tension', zname='Turns in tension', cmap='viridis', plot_type=PlotType.Absolute, scale_type=ScaleType.Log)
exit()
plot_color_sum_per_square(total_color_sum_per_square, title='Piece tension', zname='Turns in tension', cmap='viridis', plot_type=PlotType.Absolute, scale_type=ScaleType.Log)