-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.py
387 lines (286 loc) · 12 KB
/
piece.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
385
386
387
class Piece:
def __init__(self, type, name, color, x_position, y_position, board, moved, active):
self.type = type
self.name = name
self.color = color
self.x_position = x_position
self.y_position = y_position
board.createPiece(self)
self.initialMoveNotDone = moved
self.isActive = active
def crawlFields(self, board, direction, fieldsList):
y_index = self.y_position
x_index = self.x_position
while True:
if direction == 'North':
y_index += 1
if direction == 'South':
y_index -= 1
if direction == 'West':
x_index -= 1
if direction == 'East':
x_index += 1
if direction == 'NW':
y_index += 1
x_index -= 1
if direction == 'NE':
y_index += 1
x_index += 1
if direction == 'SE':
y_index -= 1
x_index += 1
if direction == 'SW':
y_index -= 1
x_index -= 1
p = [x_index, y_index]
if board.isFieldValid(x_index, y_index):
if board.isFieldEmpty(x_index, y_index):
fieldsList.append(p)
else:
if board.isEnemyOnTheField(x_index, y_index, self.color):
fieldsList.append(p)
break
else:
break
else:
break
def returnJSON(self):
import json
data = {"type": self.type, "name": self.name, "color": self.color, "x_position": self.x_position, "y_position": self.y_position, "isActive": self.isActive, "initialMoveNotDone": self.initialMoveNotDone}
return data
class Pawn(Piece):
value = 1
def returnValidFields(self, board):
validFields = []
if self.color == 'White':
upDown = 1
else:
upDown = -1
p = [self.x_position-1, self.y_position+1*upDown]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or p == board.returnEnPassantSquare()):
validFields.append(p)
px = [self.x_position, self.y_position+1*upDown]
if board.isFieldValid(px[0], px[1]) and board.isFieldEmpty(px[0], px[1]):
validFields.append(px)
p = [self.x_position+1, self.y_position+1*upDown]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or p == board.returnEnPassantSquare()):
validFields.append(p)
# two squares
if self.initialMoveNotDone:
p = [self.x_position, self.y_position+2*upDown]
if board.isFieldValid(p[0], p[1]) and board.isFieldEmpty(px[0], px[1]) and board.isFieldEmpty(p[0], p[1]):
validFields.append(p)
#print(validFields)
return validFields
def returnPossibleTargetFields(self, board):
targetFields = []
if self.color == 'White':
upDown = 1
else:
upDown = -1
p = [self.x_position-1, self.y_position+1*upDown]
if board.isFieldValid(p[0], p[1]):
targetFields.append(p)
p = [self.x_position+1, self.y_position+1*upDown]
if board.isFieldValid(p[0], p[1]):
targetFields.append(p)
return targetFields
def isMoveLegal(self, future_x_pos, future_y_pos, board):
futurePosition = [future_x_pos, future_y_pos]
validFields = self.returnValidFields(board)
if futurePosition in validFields:
return True
else:
return False
class Rook(Piece):
value = 4
def returnValidFields(self, board):
validFields = []
# going North
self.crawlFields(board, 'North', validFields)
# going South
self.crawlFields(board, 'South', validFields)
# going West
self.crawlFields(board, 'West', validFields)
# going East
self.crawlFields(board, 'East', validFields)
#print(validFields)
return validFields
def isMoveLegal(self, future_x_pos, future_y_pos, board):
futurePosition = [future_x_pos, future_y_pos]
validFields = self.returnValidFields(board)
if futurePosition in validFields:
return True
else:
return False
class Bishop(Piece):
value = 3
def returnValidFields(self, board):
validFields = []
# going NW
self.crawlFields(board, 'NW', validFields)
# going NE
self.crawlFields(board, 'NE', validFields)
# going SE
self.crawlFields(board, 'SE', validFields)
# going SW
self.crawlFields(board, 'SW', validFields)
#print(validFields)
return validFields
def isMoveLegal(self, future_x_pos, future_y_pos, board):
futurePosition = [future_x_pos, future_y_pos]
validFields = self.returnValidFields(board)
if futurePosition in validFields:
return True
else:
return False
class Queen(Piece):
value = 5
def returnValidFields(self, board):
validFields = []
# going North
self.crawlFields(board, 'North', validFields)
# going South
self.crawlFields(board, 'South', validFields)
# going West
self.crawlFields(board, 'West', validFields)
# going East
self.crawlFields(board, 'East', validFields)
# going NW
self.crawlFields(board, 'NW', validFields)
# going NE
self.crawlFields(board, 'NE', validFields)
# going SE
self.crawlFields(board, 'SE', validFields)
# going SW
self.crawlFields(board, 'SW', validFields)
#print(validFields)
return validFields
def isMoveLegal(self, future_x_pos, future_y_pos, board):
futurePosition = [future_x_pos, future_y_pos]
validFields = self.returnValidFields(board)
if futurePosition in validFields:
return True
else:
return False
class Knight(Piece):
value = 2
def returnValidFields(self, board):
validFields = []
p = [self.x_position+1, self.y_position+2]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position+2, self.y_position+1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position+2, self.y_position-1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position+1, self.y_position-2]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position-1, self.y_position-2]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position-2, self.y_position-1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position-2, self.y_position+1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position-1, self.y_position+2]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
#print(validFields)
return validFields
def isMoveLegal(self, future_x_pos, future_y_pos, board):
futurePosition = [future_x_pos, future_y_pos]
validFields = self.returnValidFields(board)
if futurePosition in validFields:
return True
else:
return False
class King(Piece):
value = 6
def returnValidFields(self, board):
validFields = []
p = [self.x_position, self.y_position+1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position+1, self.y_position+1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position+1, self.y_position]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position+1, self.y_position-1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position, self.y_position-1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position-1, self.y_position-1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position-1, self.y_position]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
p = [self.x_position-1, self.y_position+1]
if board.isFieldValid(p[0], p[1]) and (board.isEnemyOnTheField(p[0], p[1], self.color) or board.isFieldEmpty(p[0], p[1])):
validFields.append(p)
#castling
if self.initialMoveNotDone:
if self.color == 'White':
QR1 = board.returnPieceByName("WR1")
KR2 = board.returnPieceByName("WR2")
else:
QR1 = board.returnPieceByName("BR1")
KR2 = board.returnPieceByName("BR2")
# kingside
p = [self.x_position+2, self.y_position]
if board.isFieldValid(p[0], p[1]) and self.initialMoveNotDone and KR2.initialMoveNotDone and board.isFieldEmpty(p[0]-1,p[1]) and board.isFieldEmpty(p[0],p[1]):
if board.isFieldSafe(self.x_position, self.y_position, self.color) and board.isFieldSafe(p[0]-1, p[1], self.color) and board.isFieldSafe(p[0], p[1], self.color):
validFields.append(p)
# queenside
p = [self.x_position-2, self.y_position]
if board.isFieldValid(p[0], p[1]) and self.initialMoveNotDone and QR1.initialMoveNotDone and board.isFieldEmpty(p[0]+1,p[1]) and board.isFieldEmpty(p[0],p[1]) and board.isFieldEmpty(p[0]-1,p[1]):
if board.isFieldSafe(self.x_position, self.y_position, self.color) and board.isFieldSafe(p[0]+1, p[1], self.color) and board.isFieldSafe(p[0], p[1], self.color):
validFields.append(p)
#print(validFields)
return validFields
def isMoveLegal(self, future_x_pos, future_y_pos, board):
futurePosition = [future_x_pos, future_y_pos]
current_x = self.x_position
current_y = self.y_position
currentPieceName = ''
# prevent king to remove enemy piece and end up in check
# by moving to the field temporarly and checking safety
# and also check that way if squares in "shadow" are safe
if not board.isFieldEmpty(future_x_pos, future_y_pos):
currentPieceName = board.board[future_y_pos][future_x_pos].pieceOn
board.removePiece(self)
self.x_position = future_x_pos
self.y_position = future_y_pos
board.addPiece(self)
if not board.isFieldSafe(future_x_pos, future_y_pos, self.color):
board.removePiece(self)
self.x_position = current_x
self.y_position = current_y
board.addPiece(self)
if currentPieceName:
board.addPiece(board.returnPieceByName(currentPieceName))
if board.isEnemyOnTheField(future_x_pos, future_y_pos, self.color) or board.isFieldEmpty(future_x_pos, future_y_pos):
print("Can't move to checkmate!")
return False
else:
board.removePiece(self)
self.x_position = current_x
self.y_position = current_y
board.addPiece(self)
if currentPieceName:
board.addPiece(board.returnPieceByName(currentPieceName))
validFields = self.returnValidFields(board)
if futurePosition in validFields:
return True
else:
return False