-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback_chess.py
More file actions
595 lines (529 loc) · 22 KB
/
back_chess.py
File metadata and controls
595 lines (529 loc) · 22 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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/env python3
"""
Chess game engine for PyGame. Provides game engine for chess game operation. Intended for
2 person multiplayer. Does not include AI for singleplayer. For details, refer to the
README.md file.
Last updated: 10.Jun.2020, Python 3.8.3
By Joseph Libasora
"""
import pygame
class Game(object):
"""Game class - Holds/controls game running status"""
def __init__(self):
self.board = [
["b_R", "b_N", "b_B", "b_Q", "b_K", "b_B", "b_N", "b_R"],
["b_P", "b_P", "b_P", "b_P", "b_P", "b_P", "b_P", "b_P"],
["-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-"],
["w_P", "w_P", "w_P", "w_P", "w_P", "w_P", "w_P", "w_P"],
["w_R", "w_N", "w_B", "w_Q", "w_K", "w_B", "w_N", "w_R"],
]
self.white = True
self.moves = []
self.dct = {
"P": self.pawn, "R": self.rook, "N": self.knight,
"B": self.bishop, "K": self.king, "Q": self.queen
}
self.w_K, self.b_K = (7, 4), (0, 4)
self.cm, self.stale = False, False
self.enPpos = ()
self.castleR = Castle(True, True, True, True)
self.castleRLog = [Castle(self.castleR.wR, self.castleR.bR, self.castleR.wL, self.castleR.bL)]
def mkMove(self, other):
self.board[other.start[0]][other.start[1]], self.board[other.end[0]][other.end[1]] = "-", other.p_moved
self.moves.append(other)
self.white = not self.white
# Logging king positions
if other.p_moved == "w_K":
self.w_K = (other.end[0], other.end[1])
elif other.p_moved == "b_K":
self.b_K = (other.end[0], other.end[1])
# Promotions
if other.promo:
self.board[other.end[0]][other.end[1]] = f"{other.p_moved[0]}_{other.pChoice}"
# En passent
if other.enPmv:
self.board[other.start[0]][other.end[1]] = "-"
if other.p_moved[-1] == "P" and abs(other.start[0] - other.end[0]) == 2:
self.enPpos = ((other.start[0] + other.end[0]) // 2, other.end[1])
else:
self.enPpos = ()
# Castling
if other.castle:
if other.end[1] - other.start[1] == 2:
self.board[other.end[0]][other.end[1] - 1], self.board[other.end[0]][other.end[1] + 1] = self.board[other.end[0]][other.end[1] + 1], "-"
else:
self.board[other.end[0]][other.end[1] + 1], self.board[other.end[0]][other.end[1] - 2] = self.board[other.end[0]][other.end[1] - 2], "-"
self.updateCastleR(other)
self.castleRLog.append(Castle(self.castleR.wR, self.castleR.bR, self.castleR.wL, self.castleR.bL))
def undo(self):
if len(self.moves) != 0:
last = self.moves.pop()
self.board[last.start[0]][last.start[1]], self.board[last.end[0]][last.end[1]] = last.p_moved, last.p_captured
self.white = not self.white
# King position logging
if last.p_moved == "w_K":
self.w_K = (last.start[0], last.start[1])
elif last.p_moved == "b_K":
self.b_K = (last.start[0], last.start[1])
# En passent
if last.enPmv:
self.board[last.end[0]][last.end[1]], self.board[last.start[0]][last.end[1]] = "-", last.p_captured
self.enPpos = (last.end[0], last.end[1])
if last.p_moved[-1] == "P" and abs(last.start[0] - last.end[0]) == 2:
self.enPpos = ()
# Castling
self.castleRLog.pop()
newR = self.castleRLog[-1]
self.castleR = Castle(newR.wR, newR.bR, newR.wL, newR.bL)
if last.castle:
if last.end[1] - last.start[1] == 2:
self.board[last.end[0]][last.end[1] + 1], self.board[last.end[0]][last.end[1] - 1] = self.board[last.end[0]][last.end[1] - 1], "-"
else:
self.board[last.end[0]][last.end[1] - 2], self.board[last.end[0]][last.end[1] + 1] = self.board[last.end[0]][last.end[1] + 1], "-"
return f"Undid {last}"
else:
return "Max undo"
def updateCastleR(self, other):
# Update castling rights
if other.p_moved == "w_K":
self.castleR.wR, self.castleR.wL = False, False
elif other.p_moved == "b_K":
self.castleR.bR, self.castleR.bL = False, False
elif other.p_moved == "w_R":
if other.start[0] == 7:
if other.start[1] == 0:
self.castleR.wL = False
elif other.start[1] == 7:
self.castleR.wR = False
elif other.p_moved == "b_R":
if other.start[0] == 0:
if other.start[1] == 0:
self.castleR.wL = False
elif other.start[1] == 7:
self.castleR.wR = False
def turnCheck(self, other):
if self.white:
return self.board[other[0][0]][other[0][1]][0] == "w"
else:
return self.board[other[0][0]][other[0][1]][0] == "b"
def clickCheck(self, lst):
l, r = self.board[lst[0][0]][lst[0][1]][0], self.board[lst[1][0]][lst[1][1]][0]
return l == r
def inCheck(self):
if self.white:
return self.sqAttack(self.w_K[0], self.w_K[1])
else:
return self.sqAttack(self.b_K[0], self.b_K[1])
def gtChoice(self, other):
tmp = {2: "Q", 3: "R", 4: "N", 5: "B"}
return tmp[other[1]]
def sqAttack(self, r, c):
self.white = not self.white
oMoves = self.getAllPossible()
self.white = not self.white
for move in oMoves:
if move.end[0] == r and move.end[1] == c:
return True
return False
"""getValid(), getAllPossible() calculate all rule valid moves"""
def getValid(self):
"""Work around: tmp vars used to solve issues with move calculation. Values replaced again at function end."""
tmp_enP, tmp_castleR = self.enPpos, Castle(self.castleR.wR, self.castleR.bR, self.castleR.wL, self.castleR.bL)
moves = self.getAllPossible()
if self.white:
self.castleMV(self.w_K[0], self.w_K[1], moves)
else:
self.castleMV(self.b_K[0], self.b_K[1], moves)
for i in range(len(moves) -1, -1, -1):
self.mkMove(moves[i])
self.white = not self.white
if self.inCheck():
moves.pop(i)
self.white = not self.white
self.undo()
if len(moves) == 0:
if self.inCheck():
self.cm = True
else:
self.stale = True
else:
self.cm, self.stale = False, False
self.enPpos, self.castleR = tmp_enP, tmp_castleR
return moves
def getAllPossible(self):
p_moves = []
for r in range(8):
for c in range(8):
plyr, piece = self.board[r][c][0], self.board[r][c][-1]
if (plyr == "w" and self.white) or (plyr == "b" and not self.white):
self.dct[piece](r, c, p_moves)
return p_moves
"""Returning all valid moves for each respective piece. Includes castling."""
def pawn(self, r, c, p_moves):
if self.white:
if self.board[r - 1][c] == "-":
p_moves.append(Move(((r, c), (r - 1, c)), self.board))
if r == 6 and self.board[r - 2][c] == "-":
p_moves.append(Move(((r, c), (r - 2, c)), self.board))
if 0 <= c - 1:
if self.board[r - 1][c - 1][0] == "b":
p_moves.append(Move(((r, c), (r - 1, c - 1)), self.board))
elif (r - 1, c - 1) == self.enPpos:
p_moves.append(Move(((r, c), (r - 1, c - 1)), self.board, enP=True))
if c + 1 < 8:
if self.board[r - 1][c + 1][0] == "b":
p_moves.append(Move(((r, c), (r - 1, c + 1)), self.board))
elif (r - 1, c + 1) == self.enPpos:
p_moves.append(Move(((r, c), (r - 1, c + 1)), self.board, enP=True))
else:
if self.board[r + 1][c] == "-":
p_moves.append(Move(((r, c), (r + 1, c)), self.board))
if r == 1 and self.board[r + 2][c] == "-":
p_moves.append(Move(((r, c), (r + 2, c)), self.board))
if 0 <= c - 1:
if self.board[r + 1][c - 1][0] == "w":
p_moves.append(Move(((r, c), (r + 1, c - 1)), self.board))
elif (r + 1, c - 1) == self.enPpos:
p_moves.append(Move(((r, c), (r + 1, c - 1)), self.board, enP=True))
if c + 1 < 8:
if self.board[r + 1][c + 1][0] == "w":
p_moves.append(Move(((r, c), (r + 1, c + 1)), self.board))
elif (r + 1, c + 1) == self.enPpos:
p_moves.append(Move(((r, c), (r + 1, c + 1)), self.board, enP=True))
def rook(self ,r, c, p_moves):
i = 1
nrun = srun = erun = wrun = True
if self.white:
while nrun or srun or erun or wrun:
if r - i < 0:
nrun = False
if nrun:
if self.board[r - i][c] == "-":
p_moves.append(Move(((r, c), (r - i, c)), self.board))
elif self.board[r - i][c][0] == "b":
p_moves.append(Move(((r, c), (r - i, c)), self.board))
nrun = False
else:
nrun = False
if 7 < r + i:
srun = False
if srun:
if self.board[r + i][c] == "-":
p_moves.append(Move(((r, c), (r + i, c)), self.board))
elif self.board[r + i][c][0] == "b":
p_moves.append(Move(((r, c), (r + i, c)), self.board))
srun = False
else:
srun = False
if c - i < 0:
wrun = False
if wrun:
if self.board[r][c - i] == "-":
p_moves.append(Move(((r, c), (r, c - i)), self.board))
elif self.board[r][c - i][0] == "b":
p_moves.append(Move(((r, c), (r, c - i)), self.board))
wrun = False
else:
wrun = False
if 7 < c + i:
erun = False
if erun:
if self.board[r][c + i] == "-":
p_moves.append(Move(((r, c), (r, c + i)), self.board))
elif self.board[r][c + i][0] == "b":
p_moves.append(Move(((r, c), (r, c + i)), self.board))
erun = False
else:
erun = False
i += 1
else:
while nrun or srun or erun or wrun:
if 7 < r + i:
srun = False
if srun:
if self.board[r + i][c] == "-":
p_moves.append(Move(((r, c), (r + i, c)), self.board))
elif self.board[r + i][c][0] == "w":
p_moves.append(Move(((r, c), (r + i, c)), self.board))
srun = False
else:
srun = False
if r - i < 0:
nrun = False
if nrun:
if self.board[r - i][c] == "-":
p_moves.append(Move(((r, c), (r - i, c)), self.board))
elif self.board[r - i][c][0] == "w":
p_moves.append(Move(((r, c), (r - i, c)), self.board))
nrun = False
else:
nrun = False
if 7 < c + i:
erun = False
if erun:
if self.board[r][c + i] == "-":
p_moves.append(Move(((r, c), (r, c + i)), self.board))
elif self.board[r][c + i][0] == "w":
p_moves.append(Move(((r, c), (r, c + i)), self.board))
erun = False
else:
erun = False
if c - i < 0:
wrun = False
if wrun:
if self.board[r][c - i] == "-":
p_moves.append(Move(((r, c), (r, c - i)), self.board))
elif self.board[r][c - i][0] == "w":
p_moves.append(Move(((r, c), (r, c - i)), self.board))
wrun = False
else:
wrun = False
i += 1
def knight(self, r, c, p_moves):
tmp = [(-2, 1), (-2, -1), (2, 1), (2, -1), (-1, 2), (1, 2), (-1, -2), (1, -2)]
if self.white:
for t in tmp:
if 0 <= r + t[0] < 8 and 0 <= c + t[1] < 8:
if self.board[r + t[0]][c + t[1]][0] != "w":
p_moves.append(Move(((r, c), (r + t[0], c + t[1])), self.board))
else:
for t in tmp:
if 0 <= r + t[0] < 8 and 0 <= c + t[1] < 8:
if self.board[r + t[0]][c + t[1]][0] != "b":
p_moves.append(Move(((r, c), (r + t[0], c + t[1])), self.board))
def bishop(self, r, c, p_moves):
i = 1
uL = uR = lL = lR = True
if self.white:
while uL or uR or lL or lR:
if r - i < 0 or c - i < 0:
uL = False
if uL:
if self.board[r - i][c - i] == "-":
p_moves.append(Move(((r, c), (r - i, c - i)), self.board))
elif self.board[r - i][c - i][0] == "b":
p_moves.append(Move(((r, c), (r - i, c - i)), self.board))
uL = False
else:
uL = False
if r - i < 0 or 7 < c + i:
uR = False
if uR:
if self.board[r - i][c + i] == "-":
p_moves.append(Move(((r, c), (r - i, c + i)), self.board))
elif self.board[r - i][c + i][0] == "b":
p_moves.append(Move(((r, c), (r - i, c + i)), self.board))
uR = False
else:
uR = False
if 7 < r + i or c - i < 0:
lL = False
if lL:
if self.board[r + i][c - i] == "-":
p_moves.append(Move(((r, c), (r + i, c - i)), self.board))
elif self.board[r + i][c - i][0] == "b":
p_moves.append(Move(((r, c), (r + i, c - i)), self.board))
lL = False
else:
lL = False
if 7 < r + i or 7 < c + i:
lR = False
if lR:
if self.board[r + i][c + i] == "-":
p_moves.append(Move(((r, c), (r + i, c + i)), self.board))
elif self.board[r + i][c + i][0] == "b":
p_moves.append(Move(((r, c), (r + i, c + i)), self.board))
lR = False
else:
lR = False
i += 1
else:
while uL or uR or lL or lR:
if 7 < r + i or 7 < c + i:
lR = False
if lR:
if self.board[r + i][c + i] == "-":
p_moves.append(Move(((r, c), (r + i, c + i)), self.board))
elif self.board[r + i][c + i][0] == "w":
p_moves.append(Move(((r, c), (r + i, c + i)), self.board))
lR = False
else:
lR = False
if 7 < r + i or c - i < 0:
lL = False
if lL:
if self.board[r + i][c - i] == "-":
p_moves.append(Move(((r, c), (r + i, c - i)), self.board))
elif self.board[r + i][c - i][0] == "w":
p_moves.append(Move(((r, c), (r + i, c - i)), self.board))
lL = False
else:
lL = False
if r - i < 0 or 7 < c + i:
uR = False
if uR:
if self.board[r - i][c + i] == "-":
p_moves.append(Move(((r, c), (r - i, c + i)), self.board))
elif self.board[r - i][c + i][0] == "w":
p_moves.append(Move(((r, c), (r - i, c + i)), self.board))
uR = False
else:
uR = False
if r - i < 0 or c - i < 0:
uL = False
if uL:
if self.board[r - i][c - i] == "-":
p_moves.append(Move(((r, c), (r - i, c - i)), self.board))
elif self.board[r - i][c - i][0] == "w":
p_moves.append(Move(((r, c), (r - i, c - i)), self.board))
uL = False
else:
uL = False
i += 1
def queen(self, r, c, p_moves):
self.rook(r, c, p_moves)
self.bishop(r, c, p_moves)
def king(self, r, c, p_moves):
tmp = [
(-1, -1), (-1, 0), (-1, 1), (0, -1),
(0, 1), (1, -1), (1, 0), (1, 1)
]
if self.white:
for t in tmp:
if 0 <= r + t[0] < 8 and 0 <= c + t[1] < 8:
if self.board[r + t[0]][c + t[1]][0] != "w":
p_moves.append(Move(((r, c), (r + t[0], c + t[1])), self.board))
else:
for t in tmp:
if 0 <= r + t[0] < 8 and 0 <= c + t[1] < 8:
if self.board[r + t[0]][c + t[1]][0] != "b":
p_moves.append(Move(((r, c), (r + t[0], c + t[1])), self.board))
def castleMV(self, r, c, p_moves):
if self.sqAttack(r, c):
return
if (self.white and self.castleR.wR) or (not self.white and self.castleR.bR):
self.castle_R(r, c, p_moves)
if (self.white and self.castleR.wL) or (not self.white and self.castleR.bL):
self.castle_L(r, c, p_moves)
def castle_L(self, r, c, p_moves):
if self.board[r][c - 1] == "-" and self.board[r][c - 2] == "-" and self.board[r][c - 3] == "-":
if not self.sqAttack(r, c - 1) and not self.sqAttack(r, c - 2):
p_moves.append(Move(((r, c), (r, c - 2)), self.board, castle=True))
def castle_R(self, r, c, p_moves):
if self.board[r][c + 1] == "-" and self.board[r][c + 2] == "-":
if not self.sqAttack(r, c + 1) and not self.sqAttack(r, c + 2):
p_moves.append(Move(((r, c), (r, c + 2)), self.board, castle=True))
# Helper methods for game operation
@staticmethod
def getIndex(inp):
(y, x) = inp[1] // 75, inp[0] // 75
if y < 0 or 7 < y or x < 0 or 7 < x:
return None
return (y, x)
@staticmethod
def choiceIndex(inp):
(y, x) = inp[1] // 75, inp[0] // 75
if y == 9 and 1 < x < 6:
return (y, x)
return None
@staticmethod
def endIndex(inp):
(y, x) = inp[1] // 75, inp[0] // 75
if y == 9:
if x == 0 or x == 1:
return True
elif x == 6 or x == 7:
return False
return None
class Move(object):
"""Move class manages piece movements. Also holds attributes for special game rule functions"""
RnkToRow = {
"1": 7, "2":6, "3": 5, "4": 4,
"5": 3, "6": 2, "7": 1, "8": 0
}
RowToRnk = {v: k for k, v in RnkToRow.items()}
FileToCol = {
"a": 0, "b": 1, "c": 2, "d": 3,
"e": 4, "f": 5, "g": 6, "h": 7
}
ColToFile = {v: k for k, v in FileToCol.items()}
def __init__(self, clickLog, board, pChoice="", enP=False, castle=False):
self.start = (clickLog[0][0], clickLog[0][1])
self.end = (clickLog[1][0], clickLog[1][1])
self.p_moved = board[self.start[0]][self.start[1]]
self.p_captured = board[self.end[0]][self.end[1]]
self.moveID = (self.start[0] * 1000) + (self.start[1] * 100) + (self.end[0] * 10) + (self.end[1])
self.pChoice = pChoice
self.promo = (self.p_moved == "w_P" and self.end[0] == 0) or (self.p_moved == "b_P" and self.end[0] == 7)
self.enPmv = enP
if self.enPmv:
self.p_captured = "w_P" if self.p_moved == "b_P" else "b_P"
self.castle = castle
# Helper methods for game operation/game ui
def getNotation(self):
return f"{self.getRnkFile(self.start[0], self.start[1])}, {self.getRnkFile(self.end[0], self.end[1])}"
def getRnkFile(self, r, c):
return self.ColToFile[c] + self.RowToRnk[r]
def __eq__(self, other):
if isinstance(other, Move):
return self.moveID == other.moveID
return False
def __str__(self):
return self.getNotation()
class Castle(object):
"""Castle class manages game castling status"""
def __init__(self, wR, bR, wL, bL):
self.wR, self.bR, self.wL, self.bL = wR, bR, wL, bL
class Time(object):
"""Time class, manages game timer"""
def __init__(self, h=0, m=0, s=0):
self.h, self.m, self.s = h, m, s
def add(self, other):
secs = self.t2s()
self.h, self.m, self.s = self.s2t(secs + other)
def t2s(self):
h, m, s = self.h, self.m, self.s
return (h * 3600) + (m * 60) + s
def getTime(self):
return "{:02d}:{:02d}:{:02d}".format(self.h, self.m, self.s)
@classmethod
def s2newT(cls, s):
m, s = divmod(s, 60)
h, m = divmod(m, 60)
over, h = divmod(h, 24)
return cls(h, m, s)
@staticmethod
def s2t(s):
m, s = divmod(s, 60)
h, m = divmod(m, 60)
over, h = divmod(h, 24)
return (h, m, s)
def __add__(self, other):
tmp = self.t2s() + other.t2s()
return self.s2newT(tmp)
def __str__(self):
return "{:02d}:{:02d}:{:02d}".format(self.h, self.m, self.s)
def main():
print(" ------ Chess engine backend tests ------ ")
game = Game()
game.board = [
["-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "w_P", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-", "-", "-", "-"],
]
for x in game.board:
print(x)
print("--------------------------")
valids = game.getValid()
for x in valids:
print(x)
if __name__ == "__main__":
main()