-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessPieces.py
More file actions
443 lines (333 loc) · 15.4 KB
/
chessPieces.py
File metadata and controls
443 lines (333 loc) · 15.4 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
'''
Classes that enable chess within python including piece scoring (Hopefully better than attempt 1!)
'''
from string import ascii_lowercase
from itertools import product
class chessPiece:
'''
What do we want :
1. Piece colour, scoring and type
2. Each piece should know which pieces are in the way and which squares it can move to
3. King should know if it's in check
4. Pawn needs to be able to do its unique moves (or brick on Pipi)
5. Castling
'''
def __init__(self, colour: int, startSquare: str, value: int) -> None:
'''
Sets up chess piece
colour = +-1 (-1=Black, +1 = White)
value = piece value
'''
self._colour=colour
self._currentSquare=startSquare
self._value=value
self._pieceType='NaN'
self._boardcols={L:iL for iL, L in enumerate(ascii_lowercase[:8])} #Useful since it's annoying to deal with letters
self._friendlyPieces=[]
self._enemypieces=[]
self._blockingSquares=[]
self._isTaken=False
self._positiveline = lambda x : x
self._negativeline = lambda x : -x
self._totalmoves=0
self._friendlyPiecesOnRow=[]
self._friendlyPiecesOnCol =[]
self._takeablePiecesOnRow=[]
self._takeablePiecesOnCol=[]
self._friendlyPiecesOnPosDiag=[]
self._friendlyPiecesOnNegDiag=[]
self._takeablePiecesOnPosDiag=[]
self._takeablePiecesOnNegDiag=[]
self._lastMoved=False
@property
def currentSquare(self):
return self._currentSquare
@currentSquare.setter
def currentSquare(self, newSquare):
self._currentSquare=newSquare
@property
def colour(self):
return self._colour
@property
def pieceType(self):
return self._pieceType
@property
def value(self):
return self._value
@property
def totalMoves(self):
return self._totalmoves
@property
def lastMoved(self):
return self._lastMoved
@lastMoved.setter
def lastMoved(self, val: bool):
self._lastMoved=val
def checkBoardState(self, friendlyPieces: list, enemyPieces: list)->None:
#Let's get a quick scan of everything done here
self._friendlyPieces=friendlyPieces
self._enemypieces=enemyPieces
self._friendlyPiecesOnRow=[sq for sq in friendlyPieces if sq.currentSquare[0]==self._currentSquare[0]]
self._friendlyPiecesOnCol=[sq for sq in friendlyPieces if sq.currentSquare[1]==self._currentSquare[1]]
self._takeablePiecesOnRow=[sq for sq in enemyPieces if sq.currentSquare[0]==self._currentSquare[0]]
self._takeablePiecesOnCol=[sq for sq in enemyPieces if sq[1]==self._currentSquare[1]]
pdiagsq, ndiagsq=self.getDiagMoves()
self._friendlyPiecesOnPosDiag=[sq for sq in friendlyPieces if sq.currentSquare in pdiagsq]
self._friendlyPiecesOnNegDiag=[sq for sq in friendlyPieces if sq.currentSquare in ndiagsq]
self._takeablePiecesOnPosDiag=[sq for sq in enemyPieces if sq.currentSquare in pdiagsq]
self._takeablePiecesOnNegDiag=[sq for sq in enemyPieces if sq.currentSquare in ndiagsq]
def getDiagMoves(self):
xindex=self._boardcols[self._currentSquare[0]]
yindex=int(self._currentSquare[1])
self._positiveline = lambda x : x+(yindex-xindex)
self._negativeline = lambda x : -x+(yindex-xindex)
posdiagMoves=[f"{list(self._boardcols.values())[lX]}{self._positiveline(lX)}" for lX in range(1,9) if 0<self._positiveline(lX)<9]
negativediagMoves=[f"{list(self._boardcols.values())[lX]}{self._positiveline(lX)}" for lX in range(1,9) if 0<self._negativeline(lX)<9]
return posdiagMoves, negativediagMoves
def getLinearMoves(self):
verticleMoves=[f"{self._currentSquare[0]}{lY}" for lY in range(1,9)]
horizontalMoves=[f"{list(self._boardcols.values())[lX]}{self._currentSquare[1]}" for lX in range(1,9)]
return verticleMoves, horizontalMoves
def takePiece(self):
self._isTaken=True
def getLegalLinearMoves(self):
vertarray=self._friendlyPiecesOnRow+self._takeablePiecesOnRow
horarray=self._friendlyPiecesOnCol+self._takeablePiecesOnCol
uparray=[piece for piece in vertarray if int(piece.currentSquare[1])>int(self.currentSquare[1])]
uppiece=self.getClosestPiece(uparray)
downarray=[piece for piece in vertarray if int(piece.currentSquare[1])<int(self.currentSquare[1])]
downpiece=self.getClosestPiece(downarray)
leftarray=[piece for piece in horarray if self._boardcols[piece.currentSquare[0]]<self._boardcols[self.currentSquare[0]]]
leftpiece=self.getClosestPiece(leftarray)
rightarray=[piece for piece in horarray if self._boardcols[piece.currentSquare[0]]>self._boardcols[self.currentSquare[0]]]
rightpiece=self.getClosestPiece(rightarray)
self._blockingSquares.append([uppiece,downpiece,leftpiece,rightpiece])
def getLegalDiagonalMoves(self):
posdiagarray=self._friendlyPiecesOnPosDiag+self._takeablePiecesOnPosDiag
lhposarr=[piece for piece in posdiagarray if int(piece.currentSquare[1])<int(self.currentSquare[1])]
lhpospiece=self.getClosestPiece(lhposarr)
rhposarr=[piece for piece in posdiagarray if int(piece.currentSquare[1])>int(self.currentSquare[1])]
rhpospiece=self.getClosestPiece(rhposarr)
negdiagarray=self._friendlyPiecesOnNegDiag+self._takeablePiecesOnNegDiag
lhnegarr=[piece for piece in negdiagarray if int(piece.currentSquare[1])>int(self.currentSquare[1])]
lhnegpiece=self.getClosestPiece(lhnegarr)
rhnegarr=[piece for piece in negdiagarray if int(piece.currentSquare[1])<int(self.currentSquare[1])]
rhnegpiece=self.getClosestPiece(rhnegarr)
self._blockingSquares.extend([lhpospiece, rhpospiece, lhnegpiece, rhnegpiece])
def getClosestPiece(self, inarr):
closestpiece=None
dist=999
for piece in inarr:
iDist=abs(int(piece.currentSquare[1])-int(self._currentSquare[1]))
if iDist<dist:
closestpiece=piece
dist=iDist
return closestpiece
def moveHorizontal(self, proposedSquare)->bool:
if proposedSquare[0]!=self._currentSquare[0] or proposedSquare[1]!=self._currentSquare[1]:
return False
if proposedSquare==self._currentSquare:
return False
self._blockingSquares=[]
self.getLegalLinearMoves()
xcoord=self._boardcols[proposedSquare[0]]
ycoord=int(proposedSquare[1])
uplimit=int(self._blockingSquares[0].currentSquare[1])
downlimit=int(self._blockingSquares[1].currentSquare[1])
leftlimit=self._boardcols[self._blockingSquares[2].currentSquare[0]]
rightlimit=self._boardcols[self._blockingSquares[3].currentSquare[0]]
havemoved=False
if ycoord<=uplimit:
if ycoord==uplimit and self._blockingSquares[0].colour!=self._colour:
self._currentSquare=self._blockingSquares[0].currentSquare
havemoved=True
elif ycoord >= downlimit:
if ycoord==downlimit and self._blockingSquares[1].colour!=self._colour:
havemoved=True
elif xcoord>=leftlimit:
if xcoord==leftlimit and self._blockingSquares[2].colour!=self._colour:
havemoved=True
elif xcoord>=rightlimit:
if xcoord==rightlimit and self._blockingSquares[3].colour!=self._colour:
havemoved=True
else:
havemoved=True
return havemoved
def moveDiagonal(self, proposedSquare: str)->bool:
positivearr,negativerr = self.getDiagMoves()
allMoves=positivearr+negativerr
if proposedSquare not in allMoves or proposedSquare==self._currentSquare:
return False
self._blockingSquares=[]
self.getLegalDiagonalMoves()
ycoord=int(proposedSquare[1])
lhposlimit=int(self._blockingSquares[0].currentSquare[1])
rhposlimit=int(self._blockingSquares[1].currentSquare[1])
lhneglimit=int(self._blockingSquares[2].currentSquare[1])
rhneglimit=int(self._blockingSquares[3].currentSquare[1])
havemoved=False
if ycoord>=lhposlimit:
if ycoord==lhposlimit and self._blockingSquares[0].colour!=self._colour:
havemoved=True
elif ycoord<=rhposlimit:
if ycoord==rhposlimit and self._blockingSquares[1].colour!=self._colour:
havemoved=True
elif ycoord<=lhneglimit:
if ycoord==lhneglimit and self._blockingSquares[2].colour!=self._colour:
havemoved=True
elif ycoord>=rhneglimit:
if ycoord==rhneglimit and self._blockingSquares[3].colour!=self._colour:
havemoved=True
else:
havemoved=True
return havemoved
#####################
class rook(chessPiece):
def __init__(self, colour: int, startSquare: str) -> None:
super().__init__(colour, startSquare, value=5)
self._pieceType='R'
def checkMoveLegal(self, proposedStep):
return self.moveHorizontal(proposedStep)
def movePiece(self, proposedStep):
move = self.checkMoveLegal(proposedStep)
if move:
self._totalmoves+=1
self._currentSquare=proposedStep
return move
class bishop(chessPiece):
def __init__(self, colour: int, startSquare: str) -> None:
super().__init__(colour, startSquare, 3)
self._pieceType='B'
def checkMoveLegal(self, proposedStep):
return self.moveDiagonal(proposedStep)
def movePiece(self, proposedStep):
move = self.checkMoveLegal(proposedStep)
if move:
self._totalmoves+=1
self._currentSquare=proposedStep
return move
class queen(chessPiece):
def __init__(self, colour: int, startSquare: str) -> None:
super().__init__(colour, startSquare, 9)
self._pieceType='Q'
def checkMoveLegal(self, proposedStep):
move=False
move=self.moveDiagonal(proposedStep)
if not move:
self.moveHorizontal(proposedStep)
return move
def movePiece(self, proposedStep):
move = self.checkMoveLegal(proposedStep)
if move:
self._totalmoves+=1
self._currentSquare=proposedStep
return move
class king(chessPiece):
def __init__(self, colour: int, startSquare: str) -> None:
super().__init__(colour, startSquare, 1000)
self._isInCheck=False
self.hasCastled=False
self._pieceType='K'
def checkMoveLegal(self, proposedStep):
proposedX=self._boardcols[proposedStep[0]]
currX=self._boardcols[self._currentSquare[0]]
proposedY=int(proposedStep[1])
currY=int(self._currentSquare[1])
if abs(proposedX-currX)>1 or abs(proposedY-currY)>1:
if self._totalmoves==0 and abs(proposedY-currY)==0 and abs(proposedX-currX)==2:
self.getLegalLinearMoves()
crossingpoint=list(self._boardcols.values())[int((proposedX+currX)/2)]
for enemy in self._enemypieces:
if crossingpoint in enemy.checkMoveLegal(f"{crossingpoint}{proposedY}"):
return False
for i in [2,3]:
if self._blockingSquares[i].pieceType=='R' and self._blockingSquares[i].totalMoves==0:
self._totalmoves+=1
self._hasCastled=True
return True
move=False
move=self.moveDiagonal(proposedStep)
if not move:
self.moveHorizontal(proposedStep)
return move
def movePiece(self, proposedStep):
move = self.checkMoveLegal(proposedStep)
if move:
self._totalmoves+=1
self._currentSquare=proposedStep
return move
class knight(chessPiece):
def __init__(self, colour: int, startSquare: str) -> None:
super().__init__(colour, startSquare, 2)
self._pieceType='N'
def movePiece(self, proposedStep):
'''
How does the horsey move
'''
move = self.checkMoveLegal(proposedStep)
if move:
self._totalmoves+=1
self._currentSquare=proposedStep
return move
def checkMoveLegal(self, proposedStep)->bool:
xMoves = [self._boardcols[self._currentSquare[0]]+dx for dx in [-1,1, -2, 2]]
yMoves = [int(self._currentSquare[1])+dy for dy in [-1, 1, -2, 2]]
vertmoves=[m for m in product(xMoves[0:2],yMoves[2:]) if 0<m[0]<9 and 0<m[1]<9] #Up +-2 along +-1
hormoves=[m for m in product(xMoves[2:], yMoves[:2]) if 0<m[0]<9 and 0<m[1]<9]
stepcoord=(self._boardcols[proposedStep[0]], int(proposedStep[1]))
if stepcoord not in vertmoves+hormoves:
return False
for friendly in self._friendlyPieces:
if friendly.currentSquare==proposedStep:
return False
return True
class pawn(chessPiece):
def __init__(self, colour: int, startSquare: str) -> None:
super().__init__(colour, startSquare, 1)
self._pieceType='p'
def checkMoveLegal(self, proposedStep):
currentXcoord=self._boardcols[self._currentSquare[0]]
proposedXcoord=self._boardcols[proposedStep[0]]
currentYcoord=int(self._currentSquare[1])
proposedYcoord=int(proposedStep[1])
if proposedXcoord==currentXcoord and proposedYcoord==proposedYcoord+1*self._colour:
return self.moveHorizontal(proposedStep)
elif proposedXcoord==currentXcoord and proposedYcoord==currentYcoord+2*self._colour and self._totalmoves==0:
return self.moveHorizontal(proposedStep)
elif abs(proposedXcoord-currentXcoord)==1 and proposedYcoord==currentYcoord+2*self._colour:
return True
elif self.canDoEnPassant(proposedStep):
return True
return False
def promotePiece(self, promoteType):
if int(self._currentSquare[1]) in [1,8]:
if promoteType=='Q':
return queen(self._colour, self._currentSquare)
if promoteType=='N':
return knight(self._colour, self._currentSquare)
if promoteType=='B':
return bishop(self._colour, self._currentSquare)
if promoteType=='R':
return rook(self._colour, self._currentSquare)
else:
return self
def canDoEnPassant(self, proposedStep):
if abs(self._boardcols[proposedStep[0]]-self._boardcols[self._currentSquare])!=1 or int(proposedStep[1])!=int(self._currentSquare)+1*self._colour:
return 1
for piece in self._enemypieces:
if piece.pieceType=='p' and piece.totalMoves==1 and piece.lastMoved==True:
pieceY=int(piece.currentSquare[1])
pieceX=self._boardcols[piece.currentSquare[0]]
if pieceY==int(proposedStep[1]) and abs(pieceX-self._boardcols[proposedStep[0]])==0 and pieceY==5 or pieceY==4:
# print("GOOGLE EN PASSANT")
return True
return False
def movePiece(self, proposedStep):
'''
How does the horsey move
'''
move = self.checkMoveLegal(proposedStep)
if move:
self._totalmoves+=1
self._currentSquare=proposedStep
return move