-
Notifications
You must be signed in to change notification settings - Fork 0
/
pieces.py
302 lines (272 loc) · 10.9 KB
/
pieces.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# pieces.py
# Contains definitions of chess pieces and functions to generate and execute moves
import unicodedata
from utils import other_color
# abstract class for chess piece
class Piece(object):
def __init__(self, board, color):
self.color = color
self.board = board
self.moved = False # if piece has ever been moved
self.en_passant_ready = False
self.unicode_str = None
def __str__(self):
color_letter = self.color[0]
type_letter = self.type_letter
return f" {unicodedata.lookup(self.unicode_str)} "
def __repr__(self):
return self.__str__()
# return all legal moves of a piece
def get_moves(self, board):
safe_moves = []
all_movements, all_captures = self.gen_moves()
all_moves = all_movements + all_captures
for move in all_moves:
if board.is_castle(move):
king_in_check = False
for column in range(move.origin[0], move.target[0]):
row = move.origin[1]
color = board[move.origin].color
if board.in_danger([column, row], color):
king_in_check = True
if not king_in_check:
safe_moves.append(move)
else :
board.try_move(move)
if not board.in_check(self.color):
safe_moves.append(move)
board.undo_move(move)
return safe_moves
class Move(object):
# generated by board functions
moved_piece = None
is_en_passant = False
color = None
board = None
# optional, user set
promote = "queen"
message = ""
def __init__(self, origin, target, capture=None):
# mandatory for bot moves
self.origin = origin
self.target = target
# generated by board function
self.capture = capture
def __str__(self):
return f"{self.moved_piece} moving from {chr(self.origin[0]+97)}{self.origin[1] +1} to {chr(self.target[0]+97)}{self.target[1] +1}{f' capturing{self.capture}' if self.capture else ''}"
def __repr__(self):
return self.__str__()
# piece classes
class Rook(Piece):
def __init__(self, board, color):
super().__init__(board,color)
self.type_letter = "R"
self.unicode_str = f"{other_color(color).upper()} CHESS ROOK"
def gen_moves(self):
location = self.board.get_location(self)
moves = []
captures = []
for y_offset in [-1, 0, 1]:
for x_offset in [-1, 0, 1]:
if abs(y_offset) + abs(x_offset) == 1:
new_location = [location[0] + x_offset, location[1] + y_offset]
while (
new_location[0] >= 0
and new_location[0] < 8
and new_location[1] >= 0
and new_location[1] < 8
):
target = self.board[new_location]
if target is None:
moves.append(Move(location, new_location))
elif target.color != self.color:
captures.append(Move(location, new_location, target))
break
elif target.color == self.color:
break
new_location = [
new_location[0] + x_offset,
new_location[1] + y_offset,
]
return moves, captures
class Pawn(Piece):
def __init__(self, board,color):
super().__init__(board,color)
self.type_letter = "P"
self.unicode_str = f"{other_color(color).upper()} CHESS PAWN"
def gen_moves(self):
location = self.board.get_location(self)
moves = []
captures = []
if self.color == "white":
direction = 1
elif self.color == "black":
direction = -1
# walk forwards
step_location = [location[0], location[1] + 1 * direction]
if self.board[step_location] == None:
moves.append(Move(location, step_location))
# two spaces from initial location
step2_location = [location[0], location[1] + 2 * direction]
if (
not self.moved
and self.board[step_location] is None
and self.board[step2_location] is None
):
moves.append(Move(location, step2_location))
# diagonal attacks
diagonal = {}
en_passant_victim_loc = {}
for i in [-1, 1]:
diagonal[i] = [location[0] + 1 * i, location[1] + 1 * direction]
target = self.board[diagonal[i]]
if target is not None:
if target.color != self.color:
captures.append(Move(location, diagonal[i], target))
# en passant
en_passant_victim_loc[i] = [location[0] + 1 * i, location[1]]
target = self.board[en_passant_victim_loc[i]]
if target is not None:
if (
target.color != self.color
and target is Pawn
and target.en_passant_ready == True
):
move = Move(location, diagonal[i], target)
move.is_en_passant = True
captures.append(move)
return moves, captures
class Bishop(Piece):
def __init__(self, board,color):
super().__init__(board,color)
self.type_letter = "B"
self.unicode_str = f"{other_color(color).upper()} CHESS BISHOP"
def gen_moves(self):
location = self.board.get_location(self)
moves = []
captures = []
for y_offset in [-1, 1]:
for x_offset in [-1, 1]:
new_location = [location[0] + x_offset, location[1] + y_offset]
while (
new_location[0] >= 0
and new_location[0] < 8
and new_location[1] >= 0
and new_location[1] < 8
):
target = self.board[new_location]
if target is None:
moves.append(Move(location, new_location))
elif target.color != self.color:
captures.append(Move(location, new_location, target))
break
elif target.color == self.color:
break
new_location = [
new_location[0] + x_offset,
new_location[1] + y_offset,
]
return moves, captures
class Queen(Piece):
def __init__(self, board,color):
super().__init__(board,color)
self.type_letter = "Q"
self.unicode_str = f"{other_color(color).upper()} CHESS QUEEN"
def gen_moves(self):
location = self.board.get_location(self)
moves = []
captures = []
for y_offset in [-1, 0, 1]:
for x_offset in [-1, 0, 1]:
new_location = [location[0] + x_offset, location[1] + y_offset]
while (
new_location[0] >= 0
and new_location[0] < 8
and new_location[1] >= 0
and new_location[1] < 8
):
if x_offset == 0 and y_offset == 0:
break
target = self.board[new_location[0], new_location[1]]
if target is None:
moves.append(Move(location, new_location))
elif target.color != self.color:
captures.append(Move(location, new_location, target))
break
elif target.color == self.color:
break
new_location = [
new_location[0] + x_offset,
new_location[1] + y_offset,
]
return moves, captures
class Knight(Piece):
def __init__(self, board,color):
super().__init__(board,color)
self.type_letter = "N"
self.unicode_str = f"{other_color(color).upper()} CHESS KNIGHT"
def gen_moves(self):
location = self.board.get_location(self)
moves = []
captures = []
for y_offset in [-2, -1, 1, 2]:
for x_offset in [-2, -1, 1, 2]:
if abs(y_offset) + abs(x_offset) == 3: # movement must be 2,1 or 1,2
new_location = [location[0] + x_offset, location[1] + y_offset]
if (
new_location[0] >= 0
and new_location[0] < 8
and new_location[1] >= 0
and new_location[1] < 8
):
target = self.board[new_location]
if target is None:
moves.append(Move(location, new_location))
elif target.color != self.color:
captures.append(Move(location, new_location, target))
return moves, captures
class King(Piece):
def __init__(self, board,color):
super().__init__(board,color)
self.type_letter = "K"
self.unicode_str = f"{other_color(color).upper()} CHESS KING"
def gen_moves(self):
location = self.board.get_location(self)
moves = []
captures = []
for y_offset in [-1, 0, 1]:
for x_offset in [-1, 0, 1]:
new_location = [location[0] + x_offset, location[1] + y_offset]
if (
new_location[0] >= 0
and new_location[0] < 8
and new_location[1] >= 0
and new_location[1] < 8
):
target = self.board[new_location]
if target is None:
moves.append(Move(location, new_location))
elif target.color != self.color:
captures.append(Move(location, new_location, target))
# castling
if self.moved is False:
row = location[1]
row_ahead = row + 1 if self.color == "white" else row - 1
# right
if self.board[7, row] is not None:
if (
self.board[7, row].moved is False
and self.board[5, row] is None
and self.board[6, row] is None
):
moves.append(Move(location, [6, row]))
# left
if self.board[0, row] is not None:
if (
self.board[0, row].moved is False
and self.board[1, row] is None
and self.board[2, row] is None
and self.board[3, row] is None
):
moves.append(Move(location, [2, row]))
return moves, captures