-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
264 lines (208 loc) · 6.74 KB
/
Copy pathutils.py
File metadata and controls
264 lines (208 loc) · 6.74 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import termcolor
from pieces import PIECES, WHITE, BLACK, get_castles, WHITE_KC, WHITE_QC, BLACK_KC, BLACK_QC
def color_sign(color):
'''
Returns 1 for white and -1 for black.
Used in many functions, e.g.: get_board_moves
'''
return 1 if color == WHITE else -1
def normalize_cell(cell, move_up_color):
'''
Normalizes cell relative to a8 square.
'''
if move_up_color == BLACK:
cell = (7 - cell[0], 7 - cell[1])
return cell
def h_name(cell):
return cell[1] + 1
def v_name(cell):
return chr(ord('a') + cell[0])
def name_to_cell(name):
cell = (ord(name[0]) - ord('a'), int(name[1]) - 1)
return cell
def cell_name(cell):
return '{}{}'.format(v_name(cell), h_name(cell))
def format_move(board, move):
if (move['piece'] == 'king' and
abs(move['position'][0] - move['new_position'][0]) == 2):
# Castles
if move['new_position'][0] == 6:
return 'O-O'
return 'O-O-O'
if move['piece'] == 'pawn':
if move['captured_piece']:
piece_title = v_name(move['position'])
else:
piece_title = ''
else:
piece_title = PIECES[move['piece']]['title']
ambiguous = ''
ambiguous_move = False
ambiguous_file = False
ambiguous_rank = False
# FIXME: pawn captures should not be considered
for move_ in board.get_board_moves():
if (move_ != move and
move_['piece'] == move['piece'] and
move_['new_position'] == move['new_position'] and
move_['new_piece'] == move['new_piece']):
ambiguous_move = True
if move['position'][0] == move_['position'][0]:
ambiguous_file = True
if move['position'][1] == move_['position'][1]:
ambiguous_rank = True
if ambiguous_move:
if not ambiguous_file:
ambiguous = v_name(move['position'])
elif not ambiguous_rank:
ambiguous = h_name(move['position'])
else:
ambiguous = cell_name(move['position'])
revert_info = board.make_move(move)
check_mate = ''
if board.is_check(opposite=True):
check_mate = '+'
if board.is_mate():
check_mate = '#'
board.revert_move(revert_info)
return '{piece_title}{ambiguous}{is_take}{new_position}{new_piece_title}{check_mate}'.format(
piece_title=piece_title,
ambiguous=ambiguous,
is_take='x' if move['captured_piece'] else '',
new_position=cell_name(move['new_position']),
new_piece_title=('=' + PIECES[move['new_piece']]['title']) if move['piece'] != move['new_piece'] else '',
check_mate=check_mate
)
def moves_stringify(board, moves, ind=1):
if len(moves) == 0:
return ''
# !!!
moves = list(moves)
result = ''
revert_infos = []
if board.move_color == BLACK:
move = moves.pop()
result = format_move(board, move)
revert_infos.append(board.make_move(move))
ind += 1
while moves:
move = moves.pop()
if result:
result += ' '
result += '{}.{}'.format(ind, format_move(board, move))
revert_infos.append(board.make_move(move))
if moves:
move = moves.pop()
result += ' {}'.format(format_move(board, move))
revert_infos.append(board.make_move(move))
ind += 1
while revert_infos:
board.revert_move(revert_infos.pop())
return result
def print_board(board):
BOARD_COLORS = {
WHITE: 'cyan',
BLACK: 'grey'
}
for r in xrange(7, -1, -1):
line = ''
for c in xrange(8):
cell = (c, r)
cell = normalize_cell(cell, board.move_up_color)
p = board.pieces.get(cell)
if p is None:
line += '.'
else:
line += termcolor.colored(PIECES[p[0]]['title'], BOARD_COLORS[p[1]])
print line
print get_fen_from_board(board)
def get_fen_from_board(board):
fen_1 = []
for r in xrange(7, -1, -1):
row = ""
empty = 0
for c in xrange(8):
cell = (c, r)
if cell in board.pieces:
if empty:
row += str(empty)
piece, color = board.pieces[cell]
title = PIECES[piece]['title']
if color == WHITE:
title = title.upper()
else:
title = title.lower()
row += title
empty = 0
else:
empty += 1
if empty:
row += str(empty)
fen_1.append(row)
# TODO: provide k/q castles
castles = ''
if board.castles[WHITE_KC]:
castles += 'K'
if board.castles[WHITE_QC]:
castles += 'Q'
if board.castles[BLACK_KC]:
castles += 'k'
if board.castles[BLACK_QC]:
castles += 'q'
if not castles:
castles = '-'
fen = '{} {} {} {} - -'.format(
'/'.join(fen_1),
'w' if board.move_color == WHITE else 'b',
castles,
cell_name(board.en_passant) if board.en_passant else '-')
return fen
def get_board_from_fen(fen):
from board import Board
p1, p2, p3, p4, _, _ = fen.split(' ')
board_list = list(reversed(p1.split('/')))
pieces = {}
for r in xrange(7, -1, -1):
c = 0
for l in board_list[r]:
if l.isdigit():
c += int(l)
else:
for piece, piece_info in PIECES.items():
if piece_info['title'].lower() == l.lower():
color = BLACK if l == l.lower() else WHITE
pieces[(c, r)] = (piece, color)
break
c += 1
move_color = WHITE if p2 == 'w' else BLACK
castles = get_castles(
white_kc='K' in p3, white_qc='Q' in p3, black_kc='k' in p3, black_qc='q' in p3)
en_passant = None
if p4 != '-':
en_passant = name_to_cell(p4)
return Board(
pieces=pieces,
move_color=move_color,
en_passant=en_passant,
castles=castles
)
def get_color_pieces(pieces, leave_color):
return {
position: (piece, color)
for position, (piece, color) in pieces.items()
if color == leave_color
}
def update_castles(castles, positions):
if ((4, 0) in positions or
(7, 0) in positions):
castles[WHITE_KC] = False
if ((4, 0) in positions or
(0, 0) in positions):
castles[WHITE_QC] = False
if ((4, 7) in positions or
(7, 7) in positions):
castles[BLACK_KC] = False
if ((4, 7) in positions or
(0, 7) in positions):
castles[BLACK_QC] = False
return castles