-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackEndChess.py
354 lines (323 loc) · 14.5 KB
/
BackEndChess.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
# This file specifies what the traits are for each chess piece object.
# It also explains when there is a check/checkmate on the board.
from PIL import Image, ImageTk
import copy
# Finds the players king on the board.
def findKing(board, turn):
for row in range(len(board)):
for col in range(len(board[0])):
if isinstance(board[row][col], King) and board[row][col].color == turn:
kingRow = row
kingCol = col
return [kingRow, kingCol]
# Identifies if a player is in check.
def isCheck(board, turn):
kingPlace = findKing(board, turn)
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] != None and board[row][col].color != turn:
board[row][col].getMoves(board)
if [kingPlace[0], kingPlace[1]] in board[row][col].moves:
return True
return False
# Run this function if the player is in check to see if it is a checkmate.
def isCheckMate(board, turn):
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] != None and board[row][col].color == turn:
board[row][col].getMoves(board)
for move in board[row][col].moves:
tempB = copy.deepcopy(board)
tempB[move[0]][move[1]] = board[row][col]
tempB[move[0]][move[1]].posRow = move[0]
tempB[move[0]][move[1]].posCol = move[1]
tempB[row][col] = None
if not isCheck(tempB, turn):
return False
return True
class Pawn(object):
def __init__(self, color, posRow, posCol):
self.color = color
self.posRow = posRow
self.posCol = posCol
if self.color == "Black":
self.image = "./PieceImages/BlackPawn.png"
else:
self.image = "./PieceImages/WhitePawn.png"
self.moves = []
self.takeMoves = []
def __repr__(self):
return self.color+" Pawn"
# Finds possible pawn moves.
def getMoves(self, board):
self.moves = []
# Black pieces start at the top of the board.
if self.color == "Black":
if self.posRow == 1:
self.moves = [[self.posRow+1,self.posCol],[self.posRow+2,self.posCol]]
else:
self.moves = [[self.posRow+1,self.posCol]]
# White pieces start at the bottom of the board.
else:
if self.posRow == 6:
self.moves = [[self.posRow-1,self.posCol],[self.posRow-2,self.posCol]]
else:
self.moves = [[self.posRow-1,self.posCol]]
# Gets rid of moves where other pieces are blocking pawn.
for move in range(len(self.moves)):
if move == 0:
if board[self.moves[move][0]][self.moves[move][1]] != None:
self.moves = []
break
else:
if board[self.moves[move][0]][self.moves[move][1]] != None:
self.moves.pop()
self.findTakeMoves(board)
# Finds possible diagonal moves for taking pieces with a pawn.
def findTakeMoves(self, board):
self.takeMoves = []
if self.color == "Black":
self.takeMoves.append([self.posRow+1, self.posCol-1])
self.takeMoves.append([self.posRow+1, self.posCol+1])
if self.posCol == 0:
self.takeMoves.remove([self.posRow+1, self.posCol-1])
if self.posCol == len(board)-1:
self.takeMoves.remove([self.posRow+1, self.posCol+1])
else:
self.takeMoves.append([self.posRow-1, self.posCol-1])
self.takeMoves.append([self.posRow-1, self.posCol+1])
if self.posCol == 0:
self.takeMoves.remove([self.posRow-1, self.posCol-1])
if self.posCol == len(board)-1:
self.takeMoves.remove([self.posRow-1, self.posCol+1])
for i in range(len(self.takeMoves)):
if (board[self.takeMoves[i][0]][self.takeMoves[i][1]] != None
and self.color != board[self.takeMoves[i][0]][self.takeMoves[i][1]].color):
self.moves.append(self.takeMoves[i])
class Rook(object):
def __init__(self, color, posRow, posCol):
self.color = color
self.posRow = posRow
self.posCol = posCol
if self.color == "Black":
self.image = "./PieceImages/BlackRook.png"
else:
self.image = "./PieceImages/WhiteRook.png"
self.moves = []
def __repr__(self):
return self.color+" Rook"
# Finds possible Rook moves.
def getMoves(self, board):
self.moves = []
for i in range(self.posRow-1, -1, -1):
if board[i][self.posCol] != None:
if board[i][self.posCol].color != self.color:
self.moves.append([i, self.posCol])
break
self.moves.append([i, self.posCol])
for j in range(self.posCol-1, -1, -1):
if board[self.posRow][j] != None:
if board[self.posRow][j].color != self.color:
self.moves.append([self.posRow, j])
break
self.moves.append([self.posRow,j])
for k in range(self.posRow+1, 8, 1):
if board[k][self.posCol] != None:
if board[k][self.posCol].color != self.color:
self.moves.append([k, self.posCol])
break
self.moves.append([k,self.posCol])
for m in range(self.posCol+1, 8, 1):
if board[self.posRow][m] != None:
if board[self.posRow][m].color != self.color:
self.moves.append([self.posRow, m])
break
self.moves.append([self.posRow,m])
class Knight(object):
def __init__(self, color, posRow, posCol):
self.color = color
self.posRow = posRow
self.posCol = posCol
if self.color == "Black":
self.image = "./PieceImages/BlackKnight.png"
else:
self.image = "./PieceImages/WhiteKnight.png"
self.moves = []
def __repr__(self):
return self.color+" Knight"
# Finds possible Knight moves.
def getMoves(self, board, counter = 0):
self.moves = []
for move in [[-1,-2],[-1,2],[-2,-1],[-2,1],[1,-2],[1,2],[2,-1],[2,1]]:
if 0 <= self.posRow+move[0] <= 7 and 0 <= self.posCol+move[1] <= 7:
if board[self.posRow+move[0]][self.posCol+move[1]] == None:
self.moves.append([self.posRow+move[0],self.posCol+move[1]])
else:
if board[self.posRow+move[0]][self.posCol+move[1]].color != self.color:
self.moves.append([self.posRow+move[0],self.posCol+move[1]])
class Bishop(object):
def __init__(self, color, posRow, posCol):
self.color = color
self.posRow = posRow
self.posCol = posCol
if self.color == "Black":
self.image = "./PieceImages/BlackBishop.png"
else:
self.image = "./PieceImages/WhiteBishop.png"
self.moves = []
def __repr__(self):
return self.color+" Bishop"
# Finds Possible diagonal moves.
def getMoves(self, board):
self.moves = []
adder = 0
# Going diagonally down and to the right.
while(self.posRow+adder <= 6 and self.posCol+adder <= 6):
adder += 1
if board[self.posRow+adder][self.posCol+adder] != None:
if board[self.posRow+adder][self.posCol+adder].color != self.color:
self.moves.append([self.posRow+adder, self.posCol+adder])
break
self.moves.append([self.posRow+adder, self.posCol+adder])
adder = 0
# Up and to the left.
while(self.posRow+adder >= 1 and self.posCol+adder >= 1):
adder -= 1
if board[self.posRow+adder][self.posCol+adder] != None:
if board[self.posRow+adder][self.posCol+adder].color != self.color:
self.moves.append([self.posRow+adder, self.posCol+adder])
break
self.moves.append([self.posRow+adder, self.posCol+adder])
adder = 0
# Up and to the right.
while(self.posRow-adder >= 1 and self.posCol+adder <= 6):
adder += 1
if board[self.posRow-adder][self.posCol+adder] != None:
if board[self.posRow-adder][self.posCol+adder].color != self.color:
self.moves.append([self.posRow-adder, self.posCol+adder])
break
self.moves.append([self.posRow-adder, self.posCol+adder])
adder = 0
# Down and to the left.
while(self.posRow+adder <= 6 and self.posCol-adder >= 1):
adder += 1
if board[self.posRow+adder][self.posCol-adder] != None:
if board[self.posRow+adder][self.posCol-adder].color != self.color:
self.moves.append([self.posRow+adder, self.posCol-adder])
break
self.moves.append([self.posRow+adder, self.posCol-adder])
class Queen(object):
def __init__(self, color, posRow, posCol):
self.color = color
self.posRow = posRow
self.posCol = posCol
if self.color == "Black":
self.image = "./PieceImages/BlackQueen.png"
else:
self.image = "./PieceImages/WhiteQueen.png"
self.moves = []
def __repr__(self):
return self.color+" Queen"
# Finds possible rook-like straight moves.
def getStraightMoves(self, board):
for i in range(self.posRow-1, -1, -1):
if board[i][self.posCol] != None:
if board[i][self.posCol].color != self.color:
self.moves.append([i, self.posCol])
break
self.moves.append([i,self.posCol])
for j in range(self.posCol-1, -1, -1):
if board[self.posRow][j] != None:
if board[self.posRow][j].color != self.color:
self.moves.append([self.posRow, j])
break
self.moves.append([self.posRow,j])
for k in range(self.posRow+1, 8):
if board[k][self.posCol] != None:
if board[k][self.posCol].color != self.color:
self.moves.append([k, self.posCol])
break
self.moves.append([k,self.posCol])
for m in range(self.posCol+1, 8):
if board[self.posRow][m] != None:
if board[self.posRow][m].color != self.color:
self.moves.append([self.posRow, m])
break
self.moves.append([self.posRow,m])
# Finds possible bishop-like diagonal moves.
def getDiagonalMoves(self, board):
adder = 0
# Going diagonally down and to the right.
while(self.posRow+adder <= 6 and self.posCol+adder <= 6):
adder += 1
if board[self.posRow+adder][self.posCol+adder] != None:
if board[self.posRow+adder][self.posCol+adder].color != self.color:
self.moves.append([self.posRow+adder, self.posCol+adder])
break
self.moves.append([self.posRow+adder, self.posCol+adder])
adder = 0
# Up and to the left.
while(self.posRow+adder >= 1 and self.posCol+adder >= 1):
adder -= 1
if board[self.posRow+adder][self.posCol+adder] != None:
if board[self.posRow+adder][self.posCol+adder].color != self.color:
self.moves.append([self.posRow+adder, self.posCol+adder])
break
self.moves.append([self.posRow+adder, self.posCol+adder])
adder = 0
# Up and to the right.
while(self.posRow-adder >= 1 and self.posCol+adder <= 6):
adder += 1
if board[self.posRow-adder][self.posCol+adder] != None:
if board[self.posRow-adder][self.posCol+adder].color != self.color:
self.moves.append([self.posRow-adder, self.posCol+adder])
break
self.moves.append([self.posRow-adder, self.posCol+adder])
adder = 0
# Down and to the left.
while(self.posRow+adder <= 6 and self.posCol-adder >= 1):
adder += 1
if board[self.posRow+adder][self.posCol-adder] != None:
if board[self.posRow+adder][self.posCol-adder].color != self.color:
self.moves.append([self.posRow+adder, self.posCol-adder])
break
self.moves.append([self.posRow+adder, self.posCol-adder])
def getMoves(self, board):
self.moves = []
self.getDiagonalMoves(board)
self.getStraightMoves(board)
class King(object):
def __init__(self, color, posRow, posCol):
self.color = color
self.posRow = posRow
self.posCol = posCol
if self.color == "Black":
self.image = "./PieceImages/BlackKing.png"
else:
self.image = "./PieceImages/WhiteKing.png"
self.moves = []
def __repr__(self):
return self.color+" King"
# Finds possible king moves.
def getMoves(self, board):
self.moves = []
for i in range(-1, 2):
if 0 <= self.posRow-1 <= 7 and 0 <= self.posCol+i <= 7:
self.moves.append([self.posRow-1, self.posCol+i])
if board[self.posRow-1][self.posCol+i] != None:
if board[self.posRow-1][self.posCol+i].color == self.color:
self.moves.remove([self.posRow-1, self.posCol+i])
for j in range(-1, 2):
if 0 <= self.posRow+1 <= 7 and 0 <= self.posCol+j <= 7:
self.moves.append([self.posRow+1, self.posCol+j])
if board[self.posRow+1][self.posCol+j] != None:
if board[self.posRow+1][self.posCol+j].color == self.color:
self.moves.remove([self.posRow+1, self.posCol+j])
if 0 <= self.posRow <= 7 and 0 <= self.posCol-1 <= 7:
if (board[self.posRow][self.posCol-1] == None or
board[self.posRow][self.posCol-1].color != self.color):
self.moves.append([self.posRow, self.posCol-1])
if 0 <= self.posRow <= 7 and 0 <= self.posCol+1 <= 7:
if (board[self.posRow][self.posCol+1] == None or
board[self.posRow][self.posCol+1].color != self.color):
self.moves.append([self.posRow, self.posCol+1])