Create engine/endgame.py
Browse files- engine/endgame.py +90 -0
engine/endgame.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Endgame Detection for Nexus-Core
|
| 3 |
+
Simplified endgame handling
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import chess
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class EndgameDetector:
|
| 10 |
+
"""Basic endgame phase detection"""
|
| 11 |
+
|
| 12 |
+
def __init__(self):
|
| 13 |
+
self.phase = 'middlegame'
|
| 14 |
+
|
| 15 |
+
def detect_phase(self, board: chess.Board) -> str:
|
| 16 |
+
"""Detect game phase"""
|
| 17 |
+
|
| 18 |
+
# Count material
|
| 19 |
+
total_material = 0
|
| 20 |
+
piece_values = {
|
| 21 |
+
chess.PAWN: 1,
|
| 22 |
+
chess.KNIGHT: 3,
|
| 23 |
+
chess.BISHOP: 3,
|
| 24 |
+
chess.ROOK: 5,
|
| 25 |
+
chess.QUEEN: 9
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
for piece_type in piece_values:
|
| 29 |
+
count = len(board.pieces(piece_type, chess.WHITE))
|
| 30 |
+
count += len(board.pieces(piece_type, chess.BLACK))
|
| 31 |
+
total_material += count * piece_values[piece_type]
|
| 32 |
+
|
| 33 |
+
# Phase detection
|
| 34 |
+
if board.fullmove_number < 10:
|
| 35 |
+
self.phase = 'opening'
|
| 36 |
+
elif total_material <= 16:
|
| 37 |
+
self.phase = 'endgame'
|
| 38 |
+
else:
|
| 39 |
+
self.phase = 'middlegame'
|
| 40 |
+
|
| 41 |
+
return self.phase
|
| 42 |
+
|
| 43 |
+
def is_known_draw(self, board: chess.Board) -> bool:
|
| 44 |
+
"""Check for known draws"""
|
| 45 |
+
|
| 46 |
+
if board.is_insufficient_material():
|
| 47 |
+
return True
|
| 48 |
+
|
| 49 |
+
if board.halfmove_clock >= 100:
|
| 50 |
+
return True
|
| 51 |
+
|
| 52 |
+
return False
|
| 53 |
+
|
| 54 |
+
def adjust_evaluation(self, board: chess.Board, eval_score: float) -> float:
|
| 55 |
+
"""Adjust evaluation for endgame"""
|
| 56 |
+
|
| 57 |
+
phase = self.detect_phase(board)
|
| 58 |
+
|
| 59 |
+
if self.is_known_draw(board):
|
| 60 |
+
return 0.0
|
| 61 |
+
|
| 62 |
+
# Endgame adjustments
|
| 63 |
+
if phase == 'endgame':
|
| 64 |
+
# King activity bonus
|
| 65 |
+
king_activity = self._king_activity_bonus(board)
|
| 66 |
+
eval_score += king_activity
|
| 67 |
+
|
| 68 |
+
return eval_score
|
| 69 |
+
|
| 70 |
+
def _king_activity_bonus(self, board: chess.Board) -> float:
|
| 71 |
+
"""King activity in endgame"""
|
| 72 |
+
bonus = 0.0
|
| 73 |
+
|
| 74 |
+
for color in [chess.WHITE, chess.BLACK]:
|
| 75 |
+
king_sq = board.king(color)
|
| 76 |
+
if king_sq is None:
|
| 77 |
+
continue
|
| 78 |
+
|
| 79 |
+
# Distance to center
|
| 80 |
+
rank, file = divmod(king_sq, 8)
|
| 81 |
+
center_distance = abs(rank - 3.5) + abs(file - 3.5)
|
| 82 |
+
|
| 83 |
+
activity = (7 - center_distance) * 5
|
| 84 |
+
|
| 85 |
+
if color == chess.WHITE:
|
| 86 |
+
bonus += activity
|
| 87 |
+
else:
|
| 88 |
+
bonus -= activity
|
| 89 |
+
|
| 90 |
+
return bonus
|