-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPieceMovement.py
More file actions
569 lines (485 loc) · 15.2 KB
/
PieceMovement.py
File metadata and controls
569 lines (485 loc) · 15.2 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import sys
import copy
# Global Constants
WHITE, BLACK = 'w', 'b'
PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING = 'p', 'B', 'N', 'R', 'Q', 'K'
class Piece:
def __init__(self, p, name):
self.picture = p
self.name = name
self.piecelist = []
self.value = 0
class boardState:
changed = None
prevBoard = None
prevState = None
enPassant = None
# Castling
ws, wl, bs, bl = True, True, True, True
lastCapture, lastPawnMove = 0, 0
numPieces = 32
curState = boardState()
# White pieces:
wk = Piece("Pieces/WhiteKing.png", KING)
wq = Piece("Pieces/WhiteQueen.png", QUEEN)
wb = Piece("Pieces/WhiteBishop.png", BISHOP)
wn = Piece("Pieces/WhiteKnight.png", KNIGHT)
wr = Piece("Pieces/WhiteRook.png", ROOK)
wp = Piece("Pieces/WhitePawn.png", PAWN)
whitepieces = [wk, wq, wb, wn, wr, wp]
for y in whitepieces: y.colour = WHITE
# Black pieces:
bk = Piece("Pieces/BlackKing.png", KING)
bq = Piece("Pieces/BlackQueen.png", QUEEN)
bb = Piece("Pieces/BlackBishop.png", BISHOP)
bn = Piece("Pieces/BlackKnight.png", KNIGHT)
br = Piece("Pieces/BlackRook.png", ROOK)
bp = Piece("Pieces/BlackPawn.png", PAWN)
blackpieces = [bk, bq, bb, bn, br, bp]
for y in blackpieces: y.colour = BLACK
# Misc.
allpieces = whitepieces + blackpieces
boardlist = [0] * 70 # Just to be safe
pieceIds = {0: None}
# Set the piece Ids right away
for i in allpieces: pieceIds[id(i)]=i
# Clears the board of all pieces
def emptyboard():
for i in xrange(64): boardlist[i] = 0
# Looks up the piece in the pieceIds dictionary
def getPiece(pId):
return pieceIds[pId]
# Adds the positions of all pieces on the board to their respective objects
def updatepieces():
for y in allpieces:
y.piecelist = []
for num, pieceId in enumerate(boardlist):
if not(pieceId): continue
getPiece(pieceId).piecelist.append(num)
def updatepiecesfast(changed):
for i in changed:
p = getPiece(i)
toAdd = [] if changed[i][1]==-1 else [changed[i][1]]
if changed[i][0]!=-1: p.piecelist.remove(changed[i][0])
newpiecelist = p.piecelist + toAdd
p.piecelist = newpiecelist
# Reverts the board to the starting position
def resetboard():
emptyboard()
boardlist[4] = id(wk)
boardlist[3] = id(wq)
boardlist[2], boardlist[5] = id(wb), id(wb)
boardlist[1], boardlist[6] = id(wn), id(wn)
boardlist[0], boardlist[7] = id(wr), id(wr)
for i in xrange(8, 16): boardlist[i] = id(wp)
boardlist[60] = id(bk)
boardlist[59] = id(bq)
boardlist[61], boardlist[58] = id(bb), id(bb)
boardlist[62], boardlist[57] = id(bn), id(bn)
boardlist[63], boardlist[56] = id(br), id(br)
for i in xrange(48, 56): boardlist[i] = id(bp)
updatepieces()
resetboard()
# Resets the board and initializes some values
def resetgame():
global curState
resetboard()
curState = boardState()
# Converts sqr to a number for ease of calculation (sqr is a string coord)
def coordtonum(sqr):
return 8 * (int(sqr[1]) - 1) + ord(sqr[0]) - 97
# Converts num back to a coordinate
def numtocoord(num):
return chr(num%8 + 97) + str((num/8)+1)
# Returns the piece on square num
def pieceatsqr(num):
if num > 63 or num < 0:
return None
s = boardlist[num]
return getPiece(s)
# Changes the value of the piece on start to end
def ChangeVar(start, end):
j = pieceatsqr(start)
boardlist[start] = 0
boardlist[end] = id(j)
# Checks if a pawn has made it to the eighth/first rank
def pawnPromoted(end):
s = numtocoord(end)
pce = pieceatsqr(end)
if (s[1] == '8' and pce == wp) or (s[1] == '1' and pce == bp):
return True
return False
# Moves the piece on start to end
def MovePiece(start, end, update=True):
j = pieceatsqr(start)
m = pieceatsqr(end)
curState.prevState = copy.copy(curState)
curState.prevBoard = copy.copy(boardlist)
ChangeVar(start, end)
toUpdate = {id(j):[start,end]}
# Move the rook if the king castled
if j.name == KING and (end - start) == 2:
ChangeVar(end + 1, start + 1)
toUpdate[id(pieceatsqr(start+1))]=[end+1,start+1]
elif j.name == KING and (start - end) == 2:
ChangeVar(end - 2, start - 1)
toUpdate[id(pieceatsqr(start-1))]=[end-2,start-1]
# Turn a promoted pawn into a queen
if pawnPromoted(end):
if j.colour == WHITE:
boardlist[end] = id(wq)
toUpdate[id(wp)]=[start,-1];toUpdate[id(wq)]=[-1,end]
if j.colour == BLACK:
boardlist[end] = id(bq)
toUpdate[id(bp)]=[start,-1];toUpdate[id(bq)]=[-1,end]
# Updates fields needed for 50-move-rule
curState.lastCapture += 1
curState.lastPawnMove += 1
if m:
curState.lastCapture = 0
curState.numPieces -= 1
toUpdate[id(m)]=[end,-1]
curState.enPassant = None
# Checks if En Passant is valid
if j.name == PAWN:
curState.lastPawnMove = 0
s, e = start, end
if abs(s-e) == 16:
if j.colour == BLACK:
curState.enPassant = e+8
if j.colour == WHITE:
curState.enPassant = e-8
if abs(s-e) == 7 or abs(s-e) == 9:
if not m:
if j.colour == WHITE:
boardlist[e-8] = 0
toUpdate[id(bp)]=[e-8,-1]
elif j.colour == BLACK:
boardlist[e+8] = 0
toUpdate[id(wp)]=[e+8,-1]
if update:
updatepiecesfast(toUpdate)
curState.changed = copy.copy(toUpdate)
updateCastlingRights()
return j # return piece on start square (mostly for coords)
# Checks if each side can still castle
def updateCastlingRights():
if boardlist[4] != id(wk):
curState.ws, curState.wl = False, False
if boardlist[60] != id(bk):
curState.bs, curState.bl = False, False
if boardlist[0] != id(wr):
curState.wl = False
if boardlist[56] != id(br):
curState.bl = False
if boardlist[7] != id(wr):
curState.ws = False
if boardlist[63] != id(br):
curState.bs = False
# Undoes the previous move made
def UndoMove(update=True):
global curState
global boardlist
if curState.prevState == None:
return
boardlist = curState.prevBoard
if update:
x = curState.changed
for i in x: x[i] = [x[i][1],x[i][0]]
updatepiecesfast(x)
curState = curState.prevState
#helper
def rookMovement(i):
p = []
for x in range(i-8,-1,-8):
p.append(x)
if boardlist[x] != 0:
break
for x in range(i+8,64,8):
p.append(x)
if boardlist[x] != 0:
break
for x in range(i-1,i-8,-1):
if x%8 == 7: break
p.append(x)
if boardlist[x] != 0:
break
for x in range(i+1,i+8):
if x%8 == 0: break
p.append(x)
if boardlist[x] != 0:
break
return p
#helper
def bishopMovement(i):
p =[]
for x in range(i-9,-1,-9):
if x%8 == 7: break
p.append(x)
if boardlist[x] != 0:
break
for x in range(i+9,64,9):
if x%8 == 0: break
p.append(x)
if boardlist[x] != 0:
break
for x in range(i-7,0,-7):
if x%8 == 0: break
p.append(x)
if boardlist[x] != 0:
break
for x in range(i+7,64,7):
if x%8 == 7: break
p.append(x)
if boardlist[x] != 0:
break
return p
#helper
def kingMovement(sqr, i):
m, n = sqr[0], sqr[1]
p = []
if m != 'a':
p.append(i - 1)
if n != '1':
p.append(i - 9)
if n != '8':
p.append(i + 7)
if m != 'h':
p.append(i + 1)
if n != '1':
p.append(i - 7)
if n != '8':
p.append(i + 9)
if n != '1':
p.append(i - 8)
if n != '8':
p.append(i + 8)
return p
#helper
def knightMovement(sqr, i):
m, n = sqr[0], sqr[1]
p = []
if m != 'a' and m != 'b':
if n != '1':
p.append(i - 10)
if n != '8':
p.append(i + 6)
if n != '1' and n != '2':
if m != 'a':
p.append(i - 17)
if m != 'h':
p.append(i - 15)
if n != '7' and n != '8':
if m != 'a':
p.append(i + 15)
if m != 'h':
p.append(i + 17)
if m != 'g' and m != 'h':
if n != '1':
p.append(i - 6)
if n != '8':
p.append(i + 10)
return p
# Returns a list of squares that are valid moves for the piece on square i
def PieceMovement(i):
j = pieceatsqr(i)
sqr = numtocoord(i)
m, n = sqr[0], sqr[1]
p = []
if not j: return p
# King Movement
if j.name == KING:
p = kingMovement(sqr, i)
# Castling
if (j.colour == WHITE and curState.ws) or (j.colour == BLACK and curState.bs):
a, b = pieceatsqr(i + 1), pieceatsqr(i + 2)
if not a and not b:
# Cannot castle through check
if isSafe(i+1, j.colour) and isSafe(i+2, j.colour):
p.append(i + 2)
if (j.colour == WHITE and curState.wl) or (j.colour == BLACK and curState.bl):
a, b, c = pieceatsqr(i - 1), pieceatsqr(i - 2), pieceatsqr(i - 3)
if not(a) and not(b) and not(c):
if isSafe(i-1, j.colour) and isSafe(i-2, j.colour):
p.append(i - 2)
# Pawn Movement
elif j.name == PAWN:
ep = curState.enPassant
if j.colour == WHITE:
up1, up2 = i+8, i+16
xleft, xright = i+7, i+9
startrank = '2'
elif j.colour == BLACK:
up1, up2 = i-8, i-16
xleft, xright = i-9, i-7
startrank = '7'
s = pieceatsqr(up1)
if not(s):
p.append(up1)
if m != 'a':
z = pieceatsqr(xleft)
if (z and z.colour != j.colour) or ep == xleft:
p.append(xleft)
if m != 'h':
y = pieceatsqr(xright)
if (y and y.colour != j.colour) or ep == xright:
p.append(xright)
if n == startrank:
t = pieceatsqr(up2)
if not(s) and not(t):
p.append(up2)
# Knight Movement
elif j.name == KNIGHT:
p = knightMovement(sqr, i)
# Rook Movement
elif j.name == ROOK:
p = rookMovement(i)
# Bishop Movement
elif j.name == BISHOP:
p = bishopMovement(i)
# Queen Movement
elif j.name == QUEEN:
p = rookMovement(i) + bishopMovement(i)
def MoveFilterer(x):
# Make sure we don't have friendly fire
if boardlist[x] and (pieceatsqr(x)).colour == j.colour:
return False
# Cannot castle out of check
if j.name == KING and abs(x-i) == 2 and isInCheck(j.colour):
return False
legalMove = True
MovePiece(i, x, update=False)
# Cannot move into check
if isInCheckMod(j.colour):
legalMove = False
UndoMove(update=False)
return legalMove
return filter(MoveFilterer, p)
# Danger Functions
def PawnDanger(i, colour):
if colour == WHITE:
if i <= 47 and i % 8 != 7 and boardlist[i+9] == id(bp):
return True
if i <= 47 and i % 8 != 0 and boardlist[i+7] == id(bp):
return True
elif colour == BLACK:
if i >= 16 and i % 8 != 0 and boardlist[i-9] == id(wp):
return True
if i >= 16 and i % 8 != 7 and boardlist[i-7] == id(wp):
return True
def KnightDanger(sqr, colour):
if colour == WHITE:
knight = id(bn)
else:
knight = id(wn)
for y in knightMovement(numtocoord(sqr), sqr):
if boardlist[y] == knight:
return True
def KingDanger(sqr, colour):
if colour == WHITE:
king = id(bk)
else:
king = id(wk)
for y in kingMovement(numtocoord(sqr), sqr):
if boardlist[y] == king:
return True
def BigPieceDanger(sqr, colour):
if colour == WHITE:
rook, bishop, queen = id(br), id(bb), id(bq)
else:
rook, bishop, queen = id(wr), id(wb), id(wq)
for y in rookMovement(sqr):
if boardlist[y] == rook or boardlist[y] == queen:
return True
for y in bishopMovement(sqr):
if boardlist[y] == bishop or boardlist[y] == queen:
return True
# Determines whether a move can get you killed
def isSafe(sqr, colour, *exclude):
if not('P' in exclude) and PawnDanger(sqr, colour):
return False
if not('N' in exclude) and KnightDanger(sqr, colour):
return False
if not('K' in exclude) and KingDanger(sqr, colour):
return False
if not('BIG' in exclude) and BigPieceDanger(sqr, colour):
return False
return True
# determines whether the 'colour' king is in check
def isInCheck(colour):
if colour == WHITE:
kingsqr = wk.piecelist[0]
elif colour == BLACK:
kingsqr = bk.piecelist[0]
return not(isSafe(kingsqr, colour))
# same as isInCheck except it manually finds kingsqr
def isInCheckMod(colour):
if colour == WHITE:
king = id(wk)
elif colour == BLACK:
king = id(bk)
for s in xrange(64):
if boardlist[s] == king:
kingsqr = s
break
return not(isSafe(kingsqr, colour))
# determines whether colour is in checkmate, stalemate or neither
def isMated(colour, threshold=32):
inCheck = isInCheck(colour)
if not(inCheck) and curState.numPieces > threshold:
return False
if colour == WHITE:
pieces = whitepieces
elif colour == BLACK:
pieces = blackpieces
for y in pieces:
for i in y.piecelist:
if len(PieceMovement(i)) > 0:
return False
if inCheck:
return 'CHECKMATE'
return 'STALEMATE'
# Returns the boardState i states ago
def prev(i, state):
if i == 0 or not(state):
return None
if i <= 1:
return state.prevState
return prev(i-1, state.prevState)
# Determines whether the current position has been consecutively repeated three times
def isRepitition():
tempState = prev(3, curState)
tempState2 = prev(4, tempState)
if tempState and tempState2:
if boardlist == tempState.prevBoard and boardlist == tempState2.prevBoard:
return True
return False
# Determines whether the game is a draw by insufficient material
def isInsufficient():
if len(wp.piecelist) > 0 or len(bp.piecelist) > 0:
return False
elif len(wq.piecelist) > 0 or len(bq.piecelist) > 0:
return False
elif len(wr.piecelist) > 0 or len(br.piecelist) > 0:
return False
elif len(wb.piecelist) > 1 or len(bb.piecelist) > 1:
return False
elif len(wb.piecelist) > 0 and len(wn.piecelist) > 0:
return False
elif len(bb.piecelist) > 0 and len(bn.piecelist) > 0:
return False
return True
# Returns true if no capture or pawn move has been made in the last 50 moves
def isDrawByFifty():
if curState.lastCapture >= 100 and curState.lastPawnMove >= 100:
return True
return False
# Checks whether the game is a draw
def isDraw():
if isInsufficient():
return "INSUFFICIENT MATERIAL"
if isRepitition():
return "DRAW BY REPITITION"
if isDrawByFifty():
return "DRAW BY 50 MOVE RULE"
return False