-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessRules.py
344 lines (286 loc) · 10.4 KB
/
ChessRules.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
from itertools import product
import numpy
from ChessState import ChessState
from Color import Color
from Piece import Piece
from Pieces import Pieces
class ChessRules():
def __init__(self, chessBoard):
self._boardWidth = 8
self._boardHeight = 8
self._state = ChessState(chessBoard)
def turn(self):
return self._state.turn
def _getPiece(self, pos):
return self._state.chessBoard[pos[0]][pos[1]]
def _setPiece(self, pos, piece):
self._state.chessBoard[pos[0]][pos[1]] = piece
def _switchTurn(self):
if(self._state.turn == Color.WHITE):
self._state.turn = Color.BLACK
else:
self._state.turn = Color.WHITE
def _setPiecesMoved(self, piece, startPos):
if(piece.pieceType == Piece.KING):
self._state.kingMoved[piece.color] = True
elif(piece.pieceType == Piece.CASTLE):
if(startPos == (7, 0)):
self._state.castle1Moved[piece.color] = True
elif(startPos == (7, 7)):
self._state.castle2Moved[piece.color] = True
def _kingPos(self, color):
for row in range(self._boardHeight):
for col in range(self._boardWidth):
piece = self._getPiece((row, col))
if(piece != None and
piece.pieceType == Piece.KING and
piece.color == color):
return (row, col)
def _promotePawnColored(self, color, row):
for col in range(self._boardWidth):
piece = self._getPiece((row, col))
if(piece != None and
piece.color == color and
piece.pieceType == Piece.PAWN):
if(color == Color.WHITE):
self._state.chessBoard[row][col] = Pieces.WHITE_QUEEN
else:
self._state.chessBoard[row][col] = Pieces.BLACK_QUEEN
def _promotePawnIfEighthRank(self):
whiteUpgradeRow = 0
blackUpgradeRow = self._boardHeight - 1
self._promotePawnColored(Color.WHITE, whiteUpgradeRow)
self._promotePawnColored(Color.BLACK, blackUpgradeRow)
def _moveCastledPieces(self, row, direction):
if(direction < 0):
startCol, endCol = 0, 3
else:
startCol, endCol = 7, 5
piece = self._getPiece((row, startCol))
self._setPiece((row, startCol), None)
self._setPiece((row, endCol), piece)
def movePiece(self, startPos, endPos):
isCastling = self._validKingCastleMove(startPos, endPos)
piece = self._state.chessBoard[startPos[0]][startPos[1]]
if(self._isCheckAfterMove(piece.color, startPos, endPos)):
return
self._state.chessBoard[startPos[0]][startPos[1]] = None
self._state.chessBoard[endPos[0]][endPos[1]] = piece
self._setPiecesMoved(piece, startPos)
self._promotePawnIfEighthRank()
if(isCastling):
direction = numpy.sign(endPos[1] - startPos[1])
row = startPos[0]
self._moveCastledPieces(row, direction)
self._switchTurn()
def _basicValidMove(self, startPos, endPos):
if(startPos[0] >= self._boardHeight or startPos[0] < 0 or
startPos[1] >= self._boardWidth or startPos[1] < 0 or
endPos[0] >= self._boardHeight or endPos[0] < 0 or
endPos[1] >= self._boardWidth or endPos[1] < 0):
return False
startPiece = self._state.chessBoard[startPos[0]][startPos[1]]
endPiece = self._state.chessBoard[endPos[0]][endPos[1]]
if(startPos == endPos or
startPiece == None or
startPiece.color != self._state.turn or
(endPiece != None and startPiece.color == endPiece.color)):
return False
return True
def validMove(self, startPos, endPos):
return (
(self._validMovement(startPos, endPos) or
self._validKingCastleMove(startPos, endPos)) and not
self._isCheckAfterMove(self._state.turn, startPos, endPos)
)
def _validMovement(self, startPos, endPos):
if(not self._basicValidMove(startPos, endPos)):
return False
startPiece = self._state.chessBoard[startPos[0]][startPos[1]]
endPiece = self._state.chessBoard[endPos[0]][endPos[1]]
if(startPiece.pieceType == Piece.PAWN):
return self._validPawnMove(startPos, endPos)
elif(startPiece.pieceType == Piece.KNIGHT):
return self._validKnightMove(startPos, endPos)
elif(startPiece.pieceType == Piece.BISHOP):
return self._validBishopMove(startPos, endPos)
elif(startPiece.pieceType == Piece.CASTLE):
return self._validCastleMove(startPos, endPos)
elif(startPiece.pieceType == Piece.QUEEN):
return self._validQueenMove(startPos, endPos)
elif(startPiece.pieceType == Piece.KING):
return self._validKingMove(startPos, endPos)
else:
return False
def _isCheckAfterMove(self, color, startPos, endPos):
# Try moving the pieces to see if will be in check.
tempBoard = self._boardCopy()
endPiece = self._getPiece(endPos)
startPiece = self._getPiece(startPos)
self._setPiece(endPos, startPiece)
self._setPiece(startPos, None)
isCheckAfterMove = self.isCheck()
# Restore the original state.
self._loadBoard(tempBoard)
return isCheckAfterMove
def _validPawnMove(self, startPos, endPos):
dx = endPos[0] - startPos[0]
dy = endPos[1] - startPos[1]
endPiece = self._getPiece(endPos)
startPiece = self._getPiece(startPos)
# Enforce the directionality of piece colors.
if((startPiece.color == Color.BLACK and dx < 1) or
(startPiece.color == Color.WHITE and dx > -1)):
return False
dx = abs(dx)
# Normal one space move.
if(dx == 1 and dy == 0 and endPiece == None):
return True
# Attack move.
elif(dx == 1 and
abs(dy) == 1 and
endPiece != None and
endPiece.color != startPiece.color):
return True
elif(dx == 2 and dy == 0 and endPiece == None and
((startPiece.color == Color.WHITE and startPos[0] == 6) or
(startPiece.color == Color.BLACK and startPos[0] == 1))):
return True
return False
def _validKnightMove(self, startPos, endPos):
dx = endPos[0] - startPos[0]
dy = endPos[1] - startPos[1]
return ((abs(dx) == 1 and abs(dy) == 2) or
(abs(dy) == 1 and abs(dx) == 2))
def _validBishopMove(self, startPos, endPos):
drow = endPos[0] - startPos[0]
dcol = endPos[1] - startPos[1]
if(not self._validBishopMovement(drow, dcol)):
return False
rowstep = numpy.sign(drow)
colstep = numpy.sign(dcol)
rowstart = startPos[0] + rowstep
colstart = startPos[1] + colstep
rowend = endPos[0]
colend = endPos[1]
row, col = rowstart, colstart
while(row != rowend or col != colend):
if(self._getPiece((row, col)) != None):
return False
row += rowstep
col += colstep
# No rule violations found. Move is valid.
return True
def _validCastleMove(self, startPos, endPos):
drow = endPos[0] - startPos[0]
dcol = endPos[1] - startPos[1]
if(not self._validCastleMovement(drow, dcol)):
return False
rowstep = numpy.sign(drow)
colstep = numpy.sign(dcol)
rowstart = startPos[0] + rowstep
colstart = startPos[1] + colstep
rowend = endPos[0]
colend = endPos[1]
row, col = rowstart, colstart
while(row != rowend or col != colend):
if(self._getPiece((row, col)) != None):
return False
row += rowstep
col += colstep
# No rule violations found. Move is valid.
return True
def _validQueenMove(self, startPos, endPos):
drow = endPos[0] - startPos[0]
dcol = endPos[1] - startPos[1]
if(not self._validQueenMovement(drow, dcol)):
return False
rowstep = numpy.sign(drow)
colstep = numpy.sign(dcol)
rowstart = startPos[0] + rowstep
colstart = startPos[1] + colstep
rowend = endPos[0]
colend = endPos[1]
row, col = rowstart, colstart
while(row != rowend or col != colend):
if(self._getPiece((row, col)) != None):
return False
row += rowstep
col += colstep
# No rule violations found. Move is valid.
return True
def _validKingMove(self, startPos, endPos):
startPiece = self._getPiece(startPos)
dx = endPos[0] - startPos[0]
dy = endPos[1] - startPos[1]
if(abs(dx) > 1 or abs(dy) > 1):
return False
return True
def _validKingCastleMove(self, startPos, endPos):
if(not self._basicValidMove(startPos, endPos)):
return False
startPiece = self._getPiece(startPos)
color = startPiece.color
dx = endPos[0] - startPos[0]
dy = endPos[1] - startPos[1]
kingMoved = self._state.kingMoved[color]
castleMoved = (self._state.castle1Moved[color] if dy < 0
else self._state.castle2Moved[color])
if(startPiece.pieceType != Piece.KING or
kingMoved or castleMoved or
abs(dy) != 2 or dx != 0):
return False
# Check mid pieces. Any piece between castle and king is invalid.
row = startPos[0]
midColIndexes = range(5, 6) if dy > 0 else range(1, 3)
for col in midColIndexes:
if(self._state.chessBoard[row][col] != None):
return False
# Determine if the king will be in check at any point through the move.
direction = numpy.sign(dy)
midPos = (startPos[0], startPos[1] + direction)
if(self.isCheck() or
self._isCheckAfterMove(color, startPos, midPos) or
self._isCheckAfterMove(color, startPos, endPos)):
return False
return True
def _validCastleMovement(self, dx, dy):
return dx == 0 or dy == 0
def _validBishopMovement(self, dx, dy):
return abs(dx) == abs(dy)
def _validQueenMovement(self, dx, dy):
return (self._validBishopMovement(dx, dy) or
self._validCastleMovement(dx, dy))
def isCheck(self):
color = self._state.turn
kingPos = self._kingPos(color)
isCheck = False
tempTurn = self._state.turn
if(self._state.turn == color):
self._switchTurn()
for row in range(self._boardHeight):
for col in range(self._boardWidth):
if(self.validMove((row, col), kingPos)):
isCheck = True
self._state.turn = tempTurn
return isCheck
def isCheckmate(self):
allPositions = list(product(range(0, self._boardWidth), range(0, self._boardHeight)))
moves = list(product(allPositions, allPositions))
for startPos, endPos in moves:
if self.validMove(startPos, endPos):
return False
# No valid moves found, return checkmate status as true.
return True
def _boardCopy(self):
newBoard = []
for row in self._state.chessBoard:
newRow = []
for col in row:
newRow.append(col)
newBoard.append(newRow)
return newBoard
def _loadBoard(self, board):
for row in range(self._boardHeight):
for col in range(self._boardWidth):
self._setPiece((row, col), board[row][col])