-
Notifications
You must be signed in to change notification settings - Fork 1
/
pieces.py
384 lines (323 loc) · 16.3 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from helpers import Move
class Piece(object):
"""
The interface class for different pieces. It stores the position, name, color and
allied colors of the piece. And it provides multiple functions to interact with
the pieces.
"""
def __init__(self, piece_name, color, team):
"""
Initializes a piece by setting the position, name, color and allied colors
of the piece.
Args:
piece_name (str): The name of piece consisting out of a capital letter
color (str): The color of the piece
allied_colors (str): The colors of the current piece and the allied pieces
Returns:
None
"""
self.piece_name = piece_name
self.color = color
self.team = team
def __str__(self):
"""
Returns the name of the piece displayed as the color of the piece
Args: None
Returns (str):
The name of the piece displayed in the color of the piece
"""
return " " + self.piece_name + " "
def get_legal_moves(self):
"""
Interface method to get all possible moves for a certain piece
"""
pass
def get_team(self):
"""
Returns the allied colors of the piece
Args: None
Returns: allied_colors (str): The allied colors of the piece
"""
return self.team
def get_color(self):
"""
Returns the color of the piece
Args: None
Returns: color (str): The color of the piece
"""
return self.color
def get_piece_name(self):
"""
Returns the name of the piece
Args: None
Returns: piece_name (str): The name of the piece existing out of a capital letter
"""
return self.piece_name
def get_score(self):
pass
class Pawn(Piece):
"""
The class for the Pawn piece. It sets the position, piece_name, color,
direction, diag_left_direction, diag_right_direction, allied_colors and start_position
of the piece.
"""
def __init__(self, piece_name, color, direction, diag_left_direction,
diag_right_direction, team, start_position):
"""
Initializes the pawn.
Args:
piece_name (str): The name of the piece existing out of a capital letter
color (str): The color of the pawn
direction (tuple): Direction a pawn moves to consisting of an x and an y value
diag_left_direction (tuple): Direction of a pawn when capturing diagonal to the left
diag_right_direction (tuple): Direction of a pawn when capturing diagonal to the right
allied_colors (str): The allied colors of the pawn
start_position (Pos): The initial position of the pawn
Returns:
None
"""
super(Pawn, self).__init__(piece_name, color, team)
self.direction = direction
self.diag_left_direction = diag_left_direction
self.diag_right_direction = diag_right_direction
self.start_position = start_position
def get_legal_moves(self, board, moves, position, poss_positions=None, pin_info=None):
"""
Adds all the possible pawn moves following the rules of chess to the possible move
list not yet including checks.
Args:
board (arr): The chess board
moves (arr): The array of all possible moves that the player can play
position (Pos): the position of the piece
poss_positions (arr): the legal positions, or None if all positions are allowed
pin_info (dict): the dictionary with pieces that are pinned
Returns:
moves (arr): The array of all possible moves that the player can play
"""
end_position = board.getmove(position, self.direction[0], self.direction[1])
# when the pawn moves forward 1 square
end_position_two_squares = board.getmove(position, self.direction[0] * 2, self.direction[1] * 2)
# when the pawn moves forward two squares (only allowed at the start position)
end_position_diag_left = board.getmove(position, self.diag_left_direction[0], self.diag_left_direction[1])
# when the pawn can capture a piece diagonal to the left
end_position_diag_right = board.getmove(position, self.diag_right_direction[0], self.diag_right_direction[1])
pin_direction = None
if pin_info is not None:
if self in pin_info:
pin_direction = pin_info[self][1]
# when the pawn can capture a piece diagonal to the right
if board.is_on_board(end_position):
if board.check_for_piece(end_position) is None: # no piece infront of pawn
if poss_positions is None or end_position in poss_positions:
if pin_direction is None or pin_direction == (
self.direction[0], self.direction[1]) or pin_direction == (
-self.direction[0], -self.direction[1]):
moves.append(Move(position, end_position))
if poss_positions is None or end_position in poss_positions:
if (self.start_position == position) and \
board.check_for_piece(end_position_two_squares) is None:
# no piece in either of two squares in front of pawn
moves.append(Move(position, end_position_two_squares))
if board.check_for_piece(end_position_diag_left) is not None:
if board.check_for_piece(end_position_diag_left).get_color() not in self.get_team():
# enemy piece diagonal to the left
if poss_positions is None or end_position_diag_left:
if pin_direction is None or pin_direction == (
self.diag_left_direction[0], self.diag_left_direction[1]) or \
pin_direction == (-self.diag_left_direction[0], -self.diag_left_direction[1]):
moves.append(Move(position, end_position_diag_left))
if board.check_for_piece(end_position_diag_right) is not None:
if board.check_for_piece(end_position_diag_right).get_color() not in self.get_team():
# enemy piece diagonal to the right
if poss_positions is None or end_position_diag_right in poss_positions:
if pin_direction is None or pin_direction == (
self.diag_right_direction[0], self.diag_right_direction[1]) or \
pin_direction == (-self.diag_right_direction[0], -self.diag_right_direction[1]):
moves.append(Move(position, end_position_diag_right))
return moves
def get_score(self):
return 1
class Knight(Piece):
"""
The class for the Knight piece.
"""
def get_legal_moves(self, board, moves, position, poss_positions=None, pin_info=None):
"""
Adds all the possible knight moves following the rules of chess to the possible move
list not yet including checks.
Args:
board (arr): The chess board
moves (arr): The array of all possible moves that the player can play
position (Pos): the position of the piece
poss_positions (arr): the legal positions, or None if all positions are allowed
pin_info (dict): the dictionary with pieces that are pinned
Returns:
moves (arr): The array of all possible moves that the player can play
"""
directions = ((-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1))
# all possible knight moves
for direction in directions:
end_position = board.getmove(position, direction[0], direction[1])
if poss_positions is None or end_position in poss_positions:
if board.is_on_board(end_position):
if board.check_for_piece(end_position) is None: # no piece on end position
moves.append(Move(position, end_position))
else:
if board.check_for_piece(end_position).get_color() not in self.get_team():
# enemy piece on end position
moves.append(Move(position, end_position))
return moves
def get_score(self):
return 3
class Bishop(Piece):
"""
The class for the Bishop piece.
"""
def get_legal_moves(self, board, moves, position, poss_positions=None, pin_info=None):
"""
Adds all the possible bishop moves following the rules of chess to the possible move
list not yet including checks.
Args:
board (arr): The chess board
moves (arr): The array of all possible moves that the player can play
position (Pos): the position of the piece
poss_positions (arr): the legal positions, or None if all positions are allowed
pin_info (dict): the dictionary with pieces that are pinned
Returns:
moves (arr): The array of all possible moves that the player can play
"""
pin_direction = None
if pin_info is not None:
if self in pin_info:
pin_direction = pin_info[self][1]
directions = ((-1, -1), (-1, 1), (1, -1), (1, 1))
# all possible bishop directions
for direction in directions:
for tiles in range(1, 10): # maximum possible amount of diagonal squares in a row
end_position = board.getmove(position, direction[0] * tiles, direction[1] * tiles)
# position tiles amount diagonal of the bishop
if board.is_on_board(end_position):
if board.check_for_piece(end_position) is None:
if poss_positions is None or end_position in poss_positions:
if pin_direction is None or direction == pin_direction or (
-direction[0], -direction[1]) == pin_direction:
moves.append(Move(position, end_position))
else:
if board.check_for_piece(end_position).get_color() not in self.get_team():
if poss_positions is None or end_position in poss_positions:
if pin_direction is None or direction == pin_direction or (
-direction[0], -direction[1]) == pin_direction:
moves.append(Move(position, end_position))
break # can not jump over enemy piece but can capture
else:
break # cant jump over allied piece and can't capture
else:
break
return moves
def get_score(self):
return 5
class Rook(Piece):
"""
The class for the Rook piece.
"""
def get_legal_moves(self, board, moves, position, poss_positions=None, pin_info=None):
"""
Adds all the possible rook moves following the rules of chess to the possible move
list not yet including checks.
Args:
board (arr): The chess board
moves (arr): The array of all possible moves that the player can play
position (Pos): the position of the piece
poss_positions (arr): the legal positions, or None if all positions are allowed
pin_info (dict): the dictionary with pieces that are pinned
Returns:
moves (arr): The array of all possible moves that the player can play
"""
pin_direction = None
if pin_info is not None:
if self in pin_info:
pin_direction = pin_info[self][1]
directions = ((-1, 0), (0, -1), (1, 0), (0, 1))
# all possible rook directions
for direction in directions:
for tiles in range(1, 13): # maximum possible amount of straight squares in a row
end_position = board.getmove(position, direction[0] * tiles, direction[1] * tiles)
# position tiles amount straight of the rook
if board.is_on_board(end_position):
if board.check_for_piece(end_position) is None:
if poss_positions is None or end_position in poss_positions:
if pin_direction is None or direction == pin_direction or (
-direction[0], -direction[1]) == pin_direction:
moves.append(Move(position, end_position))
else:
if board.check_for_piece(end_position).get_color() not in self.get_team():
if poss_positions is None or end_position in poss_positions:
if pin_direction is None or direction == pin_direction or (
-direction[0], -direction[1]) == pin_direction:
moves.append(Move(position, end_position))
break # can't jump over enemy piece but can capture
else:
break # can't jump over allied piece and can't capture
else:
break
return moves
def get_score(self):
return 5
class Queen(Piece):
"""
The class for the Queen piece.
"""
def get_legal_moves(self, board, moves, position, poss_positions=None, pin_info=None):
"""
Adds all the possible queen moves following the rules of chess to the possible move
list not yet including checks.
Args:
board (arr): The chess board
moves (arr): The array of all possible moves that the player can play
position (Pos): the position of the piece
poss_positions (arr): the legal positions, or None if all positions are allowed
pin_info (dict): the dictionary with pieces that are pinned
Returns:
moves (arr): The array of all possible moves that the player can play
"""
Rook.get_legal_moves(self, board, moves, position, poss_positions, pin_info)
Bishop.get_legal_moves(self, board, moves, position, poss_positions, pin_info)
# because the queen can move in the same directions as the rook and bishop combined
return moves
def get_score(self):
return 9
class King(Piece):
"""
The class for the King piece.
"""
def get_legal_moves(self, board, moves, position, poss_positions=None, pin_info=None):
"""
Adds all the possible king moves following the rules of chess to the possible move
list not yet including checks.
Args:
board (arr): The chess board
moves (arr): The array of all possible moves that the player can play
position (Pos): the position of the piece
poss_positions (arr): the legal positions, or None if all positions are allowed
pin_info (dict): the dictionary with pieces that are pinned
Returns:
moves (arr): The array of all possible moves that the player can play
"""
directions = ((-1, 0), (0, -1), (1, 0), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1))
# all possible king moves
for direction in directions:
end_position = board.getmove(position, direction[0], direction[1])
# end position of a certain direction of the king
if board.is_on_board(end_position):
in_check, pin_info, check_info = board.simulate(Move(position, end_position), switch=False).check_info()
if not in_check:
if board.check_for_piece(end_position) is None:
moves.append(Move(position, end_position))
# no pieces on position thus can move king to end position
else:
if board.check_for_piece(end_position).get_color() not in self.get_team():
# can capture enemy piece thus can move king to end position
moves.append(Move(position, end_position))
return moves
def get_score(self):
return 0