-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboardController.py
More file actions
462 lines (388 loc) · 14.7 KB
/
boardController.py
File metadata and controls
462 lines (388 loc) · 14.7 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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
from copy import deepcopy #to copy a 2d array
WHITE_PLAYER = 2
BLACK_PLAYER = 1
class Board:
def __init__(self):
self.status = "gaming"
self.wKingMoved = False
self.bKingMoved = False
self.wRooksMoved = [False, False]
self.bRooksMoved = [False, False]
self.pieces = {
11:"assets/images/white_pawn.png",
12:"assets/images/white_king.png",
13:"assets/images/white_knight.png",
14:"assets/images/white_bishop.png",
15:"assets/images/white_rook.png",
19:"assets/images/white_queen.png",
21:"assets/images/black_pawn.png",
22:"assets/images/black_king.png",
23:"assets/images/black_knight.png",
24:"assets/images/black_bishop.png",
25:"assets/images/black_rook.png",
29:"assets/images/black_queen.png",
}
self.board = [
[25,23,24,29,22,24,23,25],
[21,21,21,21,21,21,21,21],
[00,00,00,00,00,00,00,00],
[00,00,00,00,00,00,00,00],
[00,00,00,00,00,00,00,00],
[00,00,00,00,00,00,00,00],
[11,11,11,11,11,11,11,11],
[15,13,14,19,12,14,13,15],
]
"""
self.board = [
[25,00,00,00,22,00,00,00],
[00,00,00,00,00,00,00,00],
[00,00,00,00,00,00,00,00],
[00,00,00,00,00,00,00,00],
[00,00,00,00,00,00,00,00],
[00,19,00,00,00,19,00,00],
[00,00,00,00,00,00,00,00],
[00,00,00,00,12,00,00,00],
]
"""
def getStatus(self):
return self.status
def getBoard(self):
return self.board
def getPieces(self):
return self.pieces
def swap(self, x, y, x2, y2, arr="default"):
if arr == "default":
arr = self.board
arr[x2][y2] = arr[x][y]
arr[x][y] = 0
return arr
def whereIsKing(self, white, arr="default"):
if arr == "default":
arr = self.board
for i in range(8):
for j in range(8):
if arr[i][j] == 12 and white:
return i,j
elif arr[i][j] == 22 and (not white):
return i,j
def isInBoard(self, x, y):
return x >= 0 and x <= 7 and y >= 0 and y <= 7
def isWhitePiece(self, x, y, arr="default"):
if arr == "default":
arr = self.board
return arr[x][y] < 20 and arr[x][y] != 0
def getColor(self, piece): #true: white / false: black
if piece > 20:
return False
elif piece < 20 and piece != 0:
return True
return None
def isBlackPiece(self, x, y, arr="default"):
if arr == "default":
arr = self.board
return arr[x][y] > 20
def coordToStandard(self, x, y): #ex: 6 0 -> a2
return str(chr(97+y)+""+str(8-x))
def standardToCoord(self, expr): #ex: a2 -> 6 0
return (8-int(expr[1]), ord(expr[0])-97)
def move(self, x, y, x2, y2, white, arr="default"): #working
if arr == "default":
arr = self.board
if self.isWhitePiece(x2, y2, arr) and self.isWhitePiece(x, y, arr):
return False
if self.isBlackPiece(x2, y2, arr) and self.isBlackPiece(x, y, arr):
return False
key = self.coordToStandard(x, y)
legalMoves = self.getLegalMoves(white, arr)
if key in legalMoves:
if self.coordToStandard(x2, y2) in legalMoves[key]:
if arr[x][y] == 12:
self.wKingMoved = True
if y == y2+2 and arr[x][y+1] == 0 and arr[x][y+2] == 0:
arr[x2][y2+1] = arr[x][y-4]
arr[x][y-4] = 0
elif y == y2-2 and arr[x][y-1] == 0 and arr[x][y-2] == 0:
arr[x2][y2-1] = arr[x][y+3]
arr[x][y+3] = 0
elif arr[x][y] == 22:
self.bKingMoved = True
if y == y2+2 and arr[x][y+1] == 0 and arr[x][y+2] == 0:
arr[x2][y2+1] = arr[x][y-4]
arr[x][y-4] = 0
elif y == y2-2 and arr[x][y-1] == 0 and arr[x][y-2] == 0:
arr[x2][y2-1] = arr[x][y+3]
arr[x][y+3] = 0
elif arr[x][y] == 15:
if x == 7 and y == 0:
self.wRooksMoved[0] = True
elif x == 7 and y == 7:
self.wRooksMoved[1] = True
elif arr[x][y] == 25:
if x == 0 and y == 0:
self.bRooksMoved[0] = True
elif x == 0 and y == 7:
self.bRooksMoved[1] = True
arr[x2][y2] = arr[x][y]
arr[x][y] = 0
if arr[x2][y2] == 11 and x2 == 0:
arr[x2][y2] = 19
elif arr[x2][y2] == 21 and x2 == 7:
arr[x2][y2] = 29
return True
return False
def isPlaceable(self, piece, x, y, arr="default"): #working
if self.isInBoard(x, y):
if arr == "default":
arr = self.board
if piece > 20 and arr[x][y] < 20:
return True
elif (piece < 20 and piece > 0 and arr[x][y] > 20 or arr[x][y] == 0):
return True
return False
def isAttacked(self, x, y, white, moves, arr="default"): #working
if arr == "default":
arr = self.board
target = self.coordToStandard(x, y)
for key in moves:
for m in moves[key]:
if m == target:
return True
return False
def getAllPawnMoves(self, x, y, white, arr="default"): #working
if arr == "default":
arr = self.board
direction = 1
if white:
direction = -1
moves = []
#standard
if self.isInBoard(x+direction, y) and arr[x+direction][y] == 0:
moves.append(self.coordToStandard(x+direction, y))
if self.isInBoard(x+2*direction, y) and arr[x+2*direction][y] == 0 and (x == 1 and (not white) or x == 6 and white):
moves.append(self.coordToStandard(x+2*direction, y))
#take
if self.isInBoard(x+direction, y+1):
if white and self.isBlackPiece(x+direction,y+1,arr) or (not white) and self.isWhitePiece(x+direction,y+1,arr):
moves.append(self.coordToStandard(x+direction, y+1))
if self.isInBoard(x+direction, y-1):
if white and self.isBlackPiece(x+direction,y-1,arr) or (not white) and self.isWhitePiece(x+direction,y-1,arr):
moves.append(self.coordToStandard(x+direction, y-1))
return moves
def getAllBishopMoves(self, x, y, white, arr="default"): #working
if arr == "default":
arr = self.board
moves = []
tl, tr, bl, br = True, True, True, True
count = 1
while tl or tr or bl or br:
if tl:
if x-count >= 0 and y-count >= 0 and self.isPlaceable(arr[x][y], x-count, y-count, arr):
moves.append(self.coordToStandard(x-count, y-count))
if self.getColor(arr[x-count][y-count]) == (not white):
tl = False
else:
tl = False
if tr:
if x-count >= 0 and y+count <= 7 and self.isPlaceable(arr[x][y], x-count, y+count, arr):
moves.append(self.coordToStandard(x-count, y+count))
if self.getColor(arr[x-count][y+count]) == (not white):
tr = False
else:
tr = False
if bl:
if x+count <= 7 and y-count >= 0 and self.isPlaceable(arr[x][y], x+count, y-count, arr):
moves.append(self.coordToStandard(x+count, y-count))
if self.getColor(arr[x+count][y-count]) == (not white):
bl = False
else:
bl = False
if br:
if x+count <= 7 and y+count <= 7 and self.isPlaceable(arr[x][y], x+count, y+count, arr):
moves.append(self.coordToStandard(x+count, y+count))
if self.getColor(arr[x+count][y+count]) == (not white):
br = False
else:
br = False
count += 1
return moves
def getAllRookMoves(self, x, y, white, arr="default"): #working
if arr == "default":
arr = self.board
moves = []
top, bot, left, right = True, True, True, True
count = 1
while top or bot or left or right:
if left:
if y-count >= 0 and self.isPlaceable(arr[x][y], x, y-count, arr):
moves.append(self.coordToStandard(x, y-count))
if self.getColor(arr[x][y-count]) == (not white):
left = False
else:
left = False
if right:
if y+count <= 7 and self.isPlaceable(arr[x][y], x, y+count, arr):
moves.append(self.coordToStandard(x, y+count))
if self.getColor(arr[x][y+count]) == (not white):
right = False
else:
right = False
if top:
if x-count >= 0 and self.isPlaceable(arr[x][y], x-count, y, arr):
moves.append(self.coordToStandard(x-count, y))
if self.getColor(arr[x-count][y]) == (not white):
top = False
else:
top = False
if bot:
if x+count <= 7 and self.isPlaceable(arr[x][y], x+count, y, arr):
moves.append(self.coordToStandard(x+count, y))
if self.getColor(arr[x+count][y]) == (not white):
bot = False
else:
bot = False
count += 1
return moves
def getAllQueenMoves(self, x, y, white, arr="default"): #working
return self.getAllRookMoves(x, y, white, arr) + self.getAllBishopMoves(x, y, white, arr)
def getAllKnightMoves(self, x, y, white, arr="default"): #working
if arr == "default":
arr = self.board
moves = []
if self.isInBoard(x+2, y+1) and self.isPlaceable(arr[x][y], x+2, y+1, arr):
moves.append(self.coordToStandard(x+2, y+1))
if self.isInBoard(x+2, y-1) and self.isPlaceable(arr[x][y], x+2, y-1, arr):
moves.append(self.coordToStandard(x+2, y-1))
if self.isInBoard(x-2, y+1) and self.isPlaceable(arr[x][y], x-2, y+1, arr):
moves.append(self.coordToStandard(x-2, y+1))
if self.isInBoard(x-2, y-1) and self.isPlaceable(arr[x][y], x-2, y-1, arr):
moves.append(self.coordToStandard(x-2, y-1))
if self.isInBoard(x+1, y+2) and self.isPlaceable(arr[x][y], x+1, y+2, arr):
moves.append(self.coordToStandard(x+1, y+2))
if self.isInBoard(x+1, y-2) and self.isPlaceable(arr[x][y], x+1, y-2, arr):
moves.append(self.coordToStandard(x+1, y-2))
if self.isInBoard(x-1, y+2) and self.isPlaceable(arr[x][y], x-1, y+2, arr):
moves.append(self.coordToStandard(x-1, y+2))
if self.isInBoard(x-1, y-2) and self.isPlaceable(arr[x][y], x-1, y-2, arr):
moves.append(self.coordToStandard(x-1, y-2))
return moves
def getAllKingMoves(self, x, y, white, arr="default"): #working (not rock)
if arr == "default":
arr = self.board
moves = []
if self.isInBoard(x+1, y) and self.isPlaceable(arr[x][y], x+1, y, arr):
moves.append(self.coordToStandard(x+1, y))
if self.isInBoard(x+1, y+1) and self.isPlaceable(arr[x][y], x+1, y+1, arr):
moves.append(self.coordToStandard(x+1, y+1))
if self.isInBoard(x+1, y-1) and self.isPlaceable(arr[x][y], x+1, y-1, arr):
moves.append(self.coordToStandard(x+1, y-1))
if self.isInBoard(x-1, y) and self.isPlaceable(arr[x][y], x-1, y, arr):
moves.append(self.coordToStandard(x-1, y))
if self.isInBoard(x-1, y+1) and self.isPlaceable(arr[x][y], x-1, y+1, arr):
moves.append(self.coordToStandard(x-1, y+1))
if self.isInBoard(x-1, y-1) and self.isPlaceable(arr[x][y], x-1, y-1, arr):
moves.append(self.coordToStandard(x-1, y-1))
if self.isInBoard(x, y+1) and self.isPlaceable(arr[x][y], x, y+1, arr):
moves.append(self.coordToStandard(x, y+1))
if self.isInBoard(x, y-1) and self.isPlaceable(arr[x][y], x, y-1, arr):
moves.append(self.coordToStandard(x, y-1))
if white:
if not self.wKingMoved:
if self.isInBoard(x, y-4) and self.isInBoard(x, y-1) and self.isInBoard(x, y-2):
if not self.wRooksMoved[0] and arr[x][y-4] == 15 and arr[x][y-1] == 0 and arr[x][y-2] == 0:
moves.append(self.coordToStandard(x, y-2))
if self.isInBoard(x, y+3) and self.isInBoard(x, y+1) and self.isInBoard(x, y+2):
if not self.wRooksMoved[1] and arr[x][y+3] == 15 and arr[x][y+1] == 0 and arr[x][y+2] == 0:
moves.append(self.coordToStandard(x, y+2))
else:
if not self.bKingMoved:
if self.isInBoard(x, y-4) and self.isInBoard(x, y-1) and self.isInBoard(x, y-2):
if not self.bRooksMoved[0] and arr[x][y-4] == 25 and arr[x][y-1] == 0 and arr[x][y-2] == 0:
moves.append(self.coordToStandard(x, y-2))
if self.isInBoard(x, y+3) and self.isInBoard(x, y+1) and self.isInBoard(x, y+2):
if not self.bRooksMoved[1] and arr[x][y+3] == 25 and arr[x][y+1] == 0 and arr[x][y+2] == 0:
moves.append(self.coordToStandard(x, y+2))
return moves
def getPotentialMoves(self, white, arr="default"): #working
potentialMoves = {}
if arr == "default":
arr = self.board
adjuster = 0
if not white:
adjuster = -10
for x in range(len(arr)):
for y in range(len(arr[x])):
p = arr[x][y] + adjuster
key = self.coordToStandard(x, y)
if p == 11: #Pawn
potentialMoves[key] = self.getAllPawnMoves(x, y, white, arr)
elif p == 14: #Bishop
potentialMoves[key] = self.getAllBishopMoves(x, y, white, arr)
elif p == 13: #Knight
potentialMoves[key] = self.getAllKnightMoves(x, y, white, arr)
elif p == 15: #Rook
potentialMoves[key] = self.getAllRookMoves(x, y, white, arr)
elif p == 19: #Queen
potentialMoves[key] = self.getAllQueenMoves(x, y, white, arr)
elif p == 12: #King
potentialMoves[key] = self.getAllKingMoves(x, y, white, arr)
return potentialMoves
#return list of all the possible moves for a position
def getLegalMoves(self, white, arr="default"):
legalMoves = {}
if self.status == "gaming":
if arr == "default":
arr = self.board
cArr = deepcopy(arr)
moves = self.getPotentialMoves(white, arr)
a = 0
#print(moves)
for key in moves:
legalMoves[key] = []
for m in moves[key]:
cArr = deepcopy(arr)
a += 1
c = self.standardToCoord(key)
c2 = self.standardToCoord(m)
val = cArr[c2[0]][c2[1]]
cArr = self.swap(c[0], c[1], c2[0], c2[1], cArr) #play the 'm' move
king = self.whereIsKing(white, cArr)
moves2 = self.getPotentialMoves(not white, cArr)
if val != 0 and (cArr[c2[0]][c2[1]] == 22 or cArr[c2[0]][c2[1]] == 12):
kStd = self.coordToStandard(c2[0], c2[1])
moves3 = self.getPotentialMoves(white, cArr)
if not self.isAttacked(c2[0], c2[1], white, moves3, cArr):
legalMoves[key].append(m)
for kk in moves2:
for mkk in moves2[kk]:
if kStd == mkk:
legalMoves[key].pop()
else:
if not self.isAttacked(king[0], king[1], not white, moves2, cArr):
legalMoves[key].append(m)
if cArr[c2[0]][c2[1]] == 12:
if c[1]-c2[1] == 2:
if self.isAttacked(c[0], c[1]-1, not white, moves2, cArr) or self.isAttacked(c[0], c[1]-2, not white, moves2, cArr) or self.isAttacked(c[0], c[1], not white, moves2, cArr):
legalMoves[key].pop()
elif c[1]-c2[1] == -2:
if self.isAttacked(c[0], c[1]+1, not white, moves2, cArr) or self.isAttacked(c[0], c[1]+2, not white, moves2, cArr) or self.isAttacked(c[0], c[1], not white, moves2, cArr):
legalMoves[key].pop()
elif cArr[c2[0]][c2[1]] == 22:
if c[1]-c2[1] == 2:
if self.isAttacked(c[0], c[1]-1, not white, moves2, cArr) or self.isAttacked(c[0], c[1]-2, not white, moves2, cArr) or self.isAttacked(c[0], c[1], not white, moves2, cArr):
legalMoves[key].pop()
elif c[1]-c2[1] == -2:
if self.isAttacked(c[0], c[1]+1, not white, moves2, cArr) or self.isAttacked(c[0], c[1]+2, not white, moves2, cArr) or self.isAttacked(c[0], c[1], not white, moves2, cArr):
legalMoves[key].pop()
#check mate / pat ... verifs (if no legal moves)
if not bool([a for a in legalMoves.values() if a != []]):
king = self.whereIsKing(white, arr)
moves4 = self.getPotentialMoves(not white, arr)
if self.isAttacked(king[0], king[1], white, moves4, arr):
if white:
self.status = "Checkmate (white lost)"
else:
self.status = "Checkmate (black lost)"
else:
self.status = "Draw"
print(self.status)
return legalMoves