-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_analysis.py
More file actions
87 lines (75 loc) · 3.03 KB
/
basic_analysis.py
File metadata and controls
87 lines (75 loc) · 3.03 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
import chess
def analyze_game(game, engine, eco_database):
results = {}
# Temel bilgiler
results['White'] = game.headers.get("White", "Unknown")
results['Black'] = game.headers.get("Black", "Unknown")
results['WhiteElo'] = game.headers.get("WhiteElo", "Unknown")
results['BlackElo'] = game.headers.get("BlackElo", "Unknown")
results['Result'] = game.headers.get("Result", "Unknown")
# Tahta ve hamleler
board = game.board()
move_count = 0
white_castled = "Not Castled"
black_castled = "Not Castled"
is_miniature = "No"
for move in game.mainline_moves():
move_count += 1
if board.is_kingside_castling(move):
if board.turn:
white_castled = "Short"
else:
black_castled = "Short"
elif board.is_queenside_castling(move):
if board.turn:
white_castled = "Long"
else:
black_castled = "Long"
board.push(move)
results['WhiteCastling'] = white_castled
results['BlackCastling'] = black_castled
results['TotalMoves'] = move_count // 2
# ECO bilgisi
from eco_utils import get_opening_name_and_code
eco_code, opening_name = get_opening_name_and_code(board, eco_database)
results['OpeningName'] = opening_name
results['ECOCode'] = eco_code
# Sonuç analizi
result = game.headers.get("Result", "Unknown")
if result == "1/2-1/2":
# Check type of draw
if board.is_stalemate():
draw_type = "Stalemate"
elif board.is_repetition():
draw_type = "Threefold Repetition"
elif board.is_insufficient_material():
draw_type = "Insufficient Material"
else:
draw_type = "Agreement (Anlaşmalı Berabere)"
results['DrawType'] = draw_type
if result in ["1-0", "0-1"]:
evaluation = engine.analyse(board, chess.engine.Limit(time=1))
score = evaluation['score'].relative
last_move = board.pop()
if board.is_checkmate():
winning_method = "Checkmate"
else:
winning_method = "Resignation"
board.push(last_move)
results['WinningMethod'] = winning_method
if move_count // 2 <= 25:
is_miniature = "Yes"
results['Miniature'] = is_miniature
if score.is_mate():
mate_distance = score.mate()
if not board.turn:
mate_distance *= -1
if (result == "1-0" and mate_distance < 0 ) or (result == "0-1" and mate_distance > 0):
results['ResignationAnalysis'] = "Incorrect, resigning side had a forced mate."
elif (result == "1-0" and mate_distance > 0) or (result == "0-1" and mate_distance < 0):
results['ResignationAnalysis'] = "Correct, opponent has a forced mate."
else:
results['ResignationAnalysis'] = "Inconsistent evaluation."
else:
results['ResignationAnalysis'] = "Evaluation does not detect a forced mate."
return results