1
+ import itertools
1
2
import re
2
3
3
4
import sunfish
@@ -22,8 +23,9 @@ def gen_legal_moves(pos):
22
23
Also the position after moving is included. '''
23
24
for move in pos .gen_moves ():
24
25
pos1 = pos .move (move )
26
+ # If we just checked for opponent moves capturing the king, we would miss
27
+ # captures in case of illegal castling.
25
28
if not any (pos1 .value (m ) >= sunfish .MATE_LOWER for m in pos1 .gen_moves ()):
26
- #if not any(pos1.board[j] == 'k' or j == pos1.kp for i,j in pos1.gen_moves()):
27
29
yield move , pos1
28
30
29
31
def mrender (pos , m ):
@@ -66,7 +68,7 @@ def renderSAN(pos, move):
66
68
srcs = [a for (a ,b ),_ in gen_legal_moves (pos ) if pos .board [a ] == pos .board [i ] and b == j ]
67
69
srcs_file = [a for a in srcs if (a - sunfish .A1 ) % 10 == (i - sunfish .A1 ) % 10 ]
68
70
srcs_rank = [a for a in srcs if (a - sunfish .A1 ) // 10 == (i - sunfish .A1 ) // 10 ]
69
- assert len ( srcs ) > 0
71
+ assert srcs , 'No moves compatible with {}' . format ( move )
70
72
if len (srcs ) == 1 : src = ''
71
73
elif len (srcs_file ) == 1 : src = csrc [0 ]
72
74
elif len (srcs_rank ) == 1 : src = csrc [1 ]
@@ -133,7 +135,7 @@ def renderFEN(pos, half_move_clock=0, full_move_clock=1):
133
135
pos = pos .rotate ()
134
136
board = '/' .join (pos .board .split ())
135
137
board = re .sub (r'\.+' , (lambda m : str (len (m .group (0 )))), board )
136
- castling = '' .join (c for c , t in zip ('KQkq' , pos .wc [::- 1 ]+ pos .bc ) if t ) or '-'
138
+ castling = '' .join (itertools . compress ('KQkq' , pos .wc [::- 1 ]+ pos .bc )) or '-'
137
139
ep = sunfish .render (pos .ep ) if not pos .board [pos .ep ].isspace () else '-'
138
140
clock = '{} {}' .format (half_move_clock , full_move_clock )
139
141
return ' ' .join ((board , color , castling , ep , clock ))
0 commit comments