-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzwichenzug_analysis.py
57 lines (47 loc) · 2.12 KB
/
zwichenzug_analysis.py
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
from stockfish_utils import evaluate_position
import chess
import chess.pgn
def analyze_zwischenzugs(game, engine):
board = game.board()
white_zwischenzug_count = 0
black_zwischenzug_count = 0
white_zwischenzug_types = []
black_zwischenzug_types = []
for move in game.mainline_moves():
current_turn = "White" if board.turn else "Black"
board.push(move)
previous_eval = evaluate_position(engine, board)
previous_score = previous_eval['score'].relative
opponent_moves = list(board.legal_moves)
zwischenzug_detected = False
zwischenzug_types = []
for opponent in opponent_moves:
board.push(opponent)
eval_after = evaluate_position(engine, board)
score_after = eval_after['score'].relative
if abs(previous_score.score(mate_score=10000) - score_after.score(mate_score=10000)) > 50:
zwischenzug_detected = True
if board.is_check():
zwischenzug_types.append("Check")
elif board.is_capture(opponent):
zwischenzug_types.append("Capture")
elif board.gives_mate(opponent):
zwischenzug_types.append("Mate Threat")
elif any(board.is_attacked_by(not board.turn, square) for square in board.legal_moves):
zwischenzug_types.append("Threatening to Capture")
board.pop()
if zwischenzug_detected:
if current_turn == "White":
white_zwischenzug_count += 1
white_zwischenzug_types.extend(zwischenzug_types)
else:
black_zwischenzug_count += 1
black_zwischenzug_types.extend(zwischenzug_types)
white_zwischenzug_types = list(set(white_zwischenzug_types))
black_zwischenzug_types = list(set(black_zwischenzug_types))
return {
"White zwischenzug count": white_zwischenzug_count,
"White zwischenzug types": white_zwischenzug_types,
"Black zwischenzug count": black_zwischenzug_count,
"Black zwischenzug types": black_zwischenzug_types,
}