-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessInPython.py
More file actions
739 lines (615 loc) · 35.5 KB
/
chessInPython.py
File metadata and controls
739 lines (615 loc) · 35.5 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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# Chess Game by Ryan
import pygame
import chess
from chess.constants import POSITION_FONT, MENU_FONT, C1, C2
import copy
import time
import datetime
WIDTH, HEIGHT = 800, 600
SQUARE_SIZE = 60
class View:
def __init__(self, win, model):
self.win = win
self.model = model
def draw(self):
if self.model.game_type is not None:
self.win.fill((90, 90, 90))
pygame.draw.rect(self.win, (118, 150, 86), (20, 20, 480, 480))
for num in range(8):
for col in range(num % 2, 8, 2):
pygame.draw.rect(self.win, (238, 238, 210), (num * SQUARE_SIZE + 20, col * SQUARE_SIZE + 20, SQUARE_SIZE, SQUARE_SIZE))
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
for num in range(8):
if num % 2 == 0:
row_widget = POSITION_FONT.render(letters[num], True, (238, 238, 210))
column_widget = POSITION_FONT.render(str(8 - num), True, (118, 150, 86))
else:
row_widget = POSITION_FONT.render(letters[num], True, (118, 150, 86))
column_widget = POSITION_FONT.render(str(8 - num), True, (238, 238, 210))
row_rect = row_widget.get_rect()
row_rect.center = (num * SQUARE_SIZE + 73, 491)
self.win.blit(row_widget, row_rect)
col_rect = row_widget.get_rect()
col_rect.center = (26, num * SQUARE_SIZE + 27)
self.win.blit(column_widget, col_rect)
for i in range(8):
for j in range(8):
if self.model.board[j][i] != '0':
if int(self.model.game_type[1]) == 7:
if '1' in self.model.board[j][i]:
self.win.blit(C1, (i*SQUARE_SIZE+25, j*SQUARE_SIZE+25))
else:
self.win.blit(C2, (i*SQUARE_SIZE+25, j*SQUARE_SIZE+25))
else:
self.win.blit(getattr(chess.constants, self.model.board[j][i].split('*')[0].split('D')[0].split('X')[0]), (i*SQUARE_SIZE+25, j*SQUARE_SIZE+25))
surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
for i in range(len(self.model.moves)):
if self.model.if_enemy_on_tile(self.model.moves[i][0], self.model.moves[i][1]):
pygame.draw.circle(surface, (0, 0, 0, 95), (self.model.moves[i][0]*SQUARE_SIZE+50, self.model.moves[i][1]*SQUARE_SIZE+50), 20, width=5)
else:
pygame.draw.circle(surface, (0, 0, 0, 95), (self.model.moves[i][0]*SQUARE_SIZE+50, self.model.moves[i][1]*SQUARE_SIZE+50), 10)
self.win.blit(surface, (0, 0))
pygame.draw.rect(self.win, (238, 238, 210), (540, 20, 240, 480))
pygame.draw.rect(self.win, (0, 0, 0), (560, 40, 100, 50))
self.get_text_widget_and_center((255, 255, 255), 610, 65, MENU_FONT, str(datetime.timedelta(seconds=int(self.model.game[1]))))
pygame.draw.rect(self.win, (255, 255, 255), (560, 110, 100, 50))
self.get_text_widget_and_center((0, 0, 0), 610, 135, MENU_FONT, str(datetime.timedelta(seconds=int(self.model.game[0]))))
if self.model.check3mode is not None:
self.get_text_widget_and_center((118, 150, 86), 720, 65, MENU_FONT, 'Checks: ' + str(self.model.check3mode[1]))
self.get_text_widget_and_center((118, 150, 86), 720, 135, MENU_FONT, 'Checks: ' + str(self.model.check3mode[0]))
if self.model.mate or self.model.stale:
x, y = pygame.mouse.get_pos()
pygame.draw.rect(self.win, (90, 90, 90), (250, 195, 300, 190), border_radius=4)
pygame.draw.rect(self.win, (238, 238, 210), (255, 200, 290, 180), border_radius=4)
self.get_text_widget_and_center((118, 150, 86), 400, 215, MENU_FONT, 'Game Over!')
pygame.draw.rect(self.win, (100, 100, 100), (265, 265, 130, 60), border_radius=4)
if 265 < x < 395 and 270 < y < 330:
pygame.draw.rect(self.win, (170, 170, 170), (270, 270, 120, 50), border_radius=4)
pygame.draw.rect(self.win, (100, 100, 100), (405, 265, 130, 60), border_radius=4)
if 405 < x < 535 and 270 < y < 330:
pygame.draw.rect(self.win, (170, 170, 170), (410, 270, 120, 50), border_radius=4)
pygame.draw.rect(self.win, (100, 100, 100), (345, 335, 110, 40), border_radius=4)
if 345 < x < 455 and 335 < y < 375:
pygame.draw.rect(self.win, (170, 170, 170), (350, 340, 100, 30), border_radius=4)
self.get_text_widget_and_center((238, 238, 210), 330, 295, MENU_FONT, 'Play Again')
self.get_text_widget_and_center((238, 238, 210), 470, 295, MENU_FONT, 'Main Menu')
self.get_text_widget_and_center((238, 238, 210), 400, 355, POSITION_FONT, 'Quit Game')
if self.model.mate:
x, y = self.model.check_whether_king_check(self.model.move, king_pos_checker=True)
if self.model.check3mode is not None and (self.model.check3mode[0] == 3 or self.model.check3mode[1] == 3):
if self.model.check3mode[0] == 3:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'BLACK WON BY 3 CHECK')
else:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'WHITE WON BY 3 CHECK')
elif int(self.model.game_type[1]) == 2 and (2 < int(x) < 5 and 2 < int(y) < 5):
if self.model.move == '1':
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'WHITE WON BY KotH')
else:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'BLACK WON BY KotH')
elif int(self.model.game_type[1]) == 3 and int(y) == 0:
if self.model.move == '1':
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'WHITE WON BY 8th RANK')
else:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'BLACK WON BY 8th RANK')
elif len(self.model.game) == 5:
if self.model.game[4] == 1:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'BLACK WON BY TIMEOUT')
else:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'WHITE WON BY TIMEOUT')
elif self.model.move == '1':
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'BLACK WON BY CHECKMATE')
else:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'WHITE WON BY CHECKMATE')
else:
if self.model.move50stalemate == 50:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'DRAW BY 50 MOVE RULE')
elif not self.model.repetition3_boards:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'DRAW BY REPETITION')
else:
self.get_text_widget_and_center((118, 150, 86), 400, 245, POSITION_FONT, 'DRAW BY STALEMATE')
if self.model.promotion is not None:
promotion_pieces = ['Q', 'N', 'R', 'B']
pygame.draw.rect(self.win, (0, 0, 0), (self.model.promotion[1]*SQUARE_SIZE + 70, self.model.promotion[2]*SQUARE_SIZE + 40, 60, 225))
for i in range(4):
pygame.draw.rect(self.win, (238, 238, 210), (self.model.promotion[1]*SQUARE_SIZE + 75, self.model.promotion[2]*SQUARE_SIZE + 45 + i*55, 50, 50))
self.win.blit(getattr(chess.constants, promotion_pieces[i]+self.model.promotion[0]), (self.model.promotion[1]*SQUARE_SIZE + 75, self.model.promotion[2]*SQUARE_SIZE + 45 + i*55))
else:
if self.model.game_type is None:
self.win.fill((118, 150, 86))
x, y = pygame.mouse.get_pos()
pygame.draw.rect(self.win, (100, 100, 100), (280, 175, 240, 250), border_radius=4)
pygame.draw.rect(self.win, (238, 238, 210), (285, 180, 230, 240), border_radius=4)
if self.model.game is None:
self.get_text_widget_and_center((118, 150, 86), 400, 200, MENU_FONT, 'Play a game?')
else:
self.get_text_widget_and_center((118, 150, 86), 400, 200, MENU_FONT, 'Choose your gamemode.')
pygame.draw.rect(self.win, (100, 100, 100), (690, 540, 100, 50), border_radius=4)
if 690 < x < 790 and 540 < y < 590:
pygame.draw.rect(self.win, (170, 170, 170), (695, 545, 90, 40), border_radius=4)
self.get_text_widget_and_center((238, 238, 210), 740, 565, MENU_FONT, 'Back')
if self.model.game is None:
for i in range(3):
pygame.draw.rect(self.win, (100, 100, 100), (295, 220+i*60, 210, 50), border_radius=4)
if 295 < x < 505 and 220+i*60 < y < 270+i*60:
pygame.draw.rect(self.win, (170, 170, 170), (300, 225+i*60, 200, 40), border_radius=4)
self.get_text_widget_and_center((238, 238, 210), 400, 245+i*60, MENU_FONT, str((i+1)*5) + ' mins')
else:
for i in range(3):
if i + (3*self.model.page) < len(self.model.game_selections):
pygame.draw.rect(self.win, (100, 100, 100), (295, 220+i*60, 210, 50), border_radius=4)
if 295 < x < 505 and 220+i*60 < y < 270+i*60:
pygame.draw.rect(self.win, (170, 170, 170), (300, 225+i*60, 200, 40), border_radius=4)
self.get_text_widget_and_center((238, 238, 210), 400, 245 + i * 60, MENU_FONT, self.model.game_selections[i+(3*self.model.page)].split('@')[0])
if self.model.page < (len(self.model.game_selections) // 3):
if 460 < x < 490 and 395 < y < 415:
pygame.draw.rect(self.win, (170, 170, 170), (460, 395, 30, 20), border_radius=4)
else:
pygame.draw.rect(self.win, (100, 100, 100), (460, 395, 30, 20), border_radius=4)
self.get_text_widget_and_center((238, 238, 210), 475, 405, MENU_FONT, '>')
if self.model.page > 0:
if 310 < x < 340 and 395 < y < 415:
pygame.draw.rect(self.win, (170, 170, 170), (310, 395, 30, 20), border_radius=4)
else:
pygame.draw.rect(self.win, (100, 100, 100), (310, 395, 30, 20), border_radius=4)
self.get_text_widget_and_center((238, 238, 210), 325, 405, MENU_FONT, '<')
self.get_text_widget_and_center((100, 100, 100), 400, 405, POSITION_FONT, str(self.model.page + 1) + '/' + str(len(self.model.game_selections) // 3+1))
def get_text_widget_and_center(self, rgb, c_x, c_y, font, text):
widget = font.render(text, True, rgb)
rect = widget.get_rect()
rect.center = (c_x, c_y)
self.win.blit(widget, rect)
class Model:
def __init__(self):
self.board = []
self.checking_board = []
self.game_boards = [[['R2X', 'N2', 'B2', 'Q2', 'K2X', 'B2', 'N2', 'R2X'],
['P2', 'P2', 'P2', 'P2', 'P2', 'P2', 'P2', 'P2'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P1', 'P1'],
['R1X', 'N1', 'B1', 'Q1', 'K1X', 'B1', 'N1', 'R1X']],
[['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['K2', 'R2', 'B2', 'N2', 'N1', 'B1', 'R1', 'K1'],
['Q2', 'R2', 'B2', 'N2', 'N1', 'B1', 'R1', 'Q1']]]
self.game_selections = ['Classic@0,0', '3 Check@0,1', 'King of the Hill@0,2', 'Racing Kings@1,3', 'Sideways Pawn@0,4', 'No Castling@0,5', 'Torpedo@0,6', 'Chess With Checkers@0,7', 'Opposite Castling@0,8']
self.page = 0
self.game_type = None
self.game = None
self.run = True
self.moves = []
self.piece_selected = ''
self.piece_pos = []
self.move = '1'
self.checked = False
self.mate = False
self.stale = False
self.promotion = None
self.move50stalemate = False
self.repetition3_boards = []
self.check3mode = None
self.castle_direction = '10'
self.knight_moves = [[1, 2], [-1, 2], [1, -2], [-1, -2], [2, 1], [2, -1], [-2, 1], [-2, -1]]
self.bishop_moves = [[1, 1], [1, -1], [-1, -1], [-1, 1]]
self.rook_moves = [[1, 0], [-1, 0], [0, 1], [0, -1]]
self.king_moves = [[1, 1], [1, -1], [-1, -1], [-1, 1], [1, 0], [-1, 0], [0, 1], [0, -1]]
def check_where_mouse_clicked(self, x, y):
if self.game_type is not None:
if 20 < x < 500 and 20 < y < 500 and not self.mate and not self.stale and self.promotion is None:
piece = self.board[(y - 20) // 60][(x - 20) // 60]
color = ''
if '1' in self.piece_selected:
color = '1'
elif '2' in self.piece_selected:
color = '2'
elif '1' in piece:
color = '1'
elif '2' in piece:
color = '2'
if ([(x - 20) // 60, (y - 20) // 60] in self.moves or [(x - 20) // 60, (y - 20) // 60, 2] in self.moves or [(x - 20) // 60, (y - 20) // 60, -1] in self.moves or [(x - 20) // 60, (y - 20) // 60, 1] in self.moves or [(x - 20) // 60, (y - 20) // 60, '3-1'] in self.moves or [(x - 20) // 60, (y - 20) // 60, '3-2'] in self.moves) and color == self.move:
if [(x - 20) // 60, (y - 20) // 60, 2] in self.moves:
self.move_piece((y - 20) // 60, (x - 20) // 60, double_move=True)
elif [(x - 20) // 60, (y - 20) // 60, 1] in self.moves:
self.move_piece((y - 20) // 60, (x - 20) // 60, en_passant_capture=1)
elif [(x - 20) // 60, (y - 20) // 60, -1] in self.moves:
self.move_piece((y - 20) // 60, (x - 20) // 60, en_passant_capture=-1)
elif [(x - 20) // 60, (y - 20) // 60, '3-1'] in self.moves:
self.move_piece((y - 20) // 60, (x - 20) // 60, castling_side='1')
elif [(x - 20) // 60, (y - 20) // 60, '3-2'] in self.moves:
self.move_piece((y - 20) // 60, (x - 20) // 60, castling_side='2')
else:
self.move_piece((y - 20) // 60, (x - 20) // 60)
elif piece != '0' and color == self.move and color in piece:
self.moves.clear()
self.calc_piece_moves(piece, (x - 20) // 60, (y - 20) // 60)
else:
self.moves.clear()
self.piece_selected = ''
elif self.promotion is not None:
if self.promotion[1]*SQUARE_SIZE + 75 < x < self.promotion[1]*SQUARE_SIZE + 125 and self.promotion[2]*SQUARE_SIZE + 45 < y < self.promotion[2]*SQUARE_SIZE + 100:
self.board[self.promotion[2]][self.promotion[1]] = 'Q' + self.promotion[0]
self.promotion = None
elif self.promotion[1]*SQUARE_SIZE + 75 < x < self.promotion[1]*SQUARE_SIZE + 125 and self.promotion[2]*SQUARE_SIZE + 100 < y < self.promotion[2]*SQUARE_SIZE + 155:
self.board[self.promotion[2]][self.promotion[1]] = 'N' + self.promotion[0]
self.promotion = None
elif self.promotion[1]*SQUARE_SIZE + 75 < x < self.promotion[1]*SQUARE_SIZE + 125 and self.promotion[2]*SQUARE_SIZE + 155 < y < self.promotion[2]*SQUARE_SIZE + 210:
self.board[self.promotion[2]][self.promotion[1]] = 'R' + self.promotion[0]
self.promotion = None
elif self.promotion[1]*SQUARE_SIZE + 75 < x < self.promotion[1]*SQUARE_SIZE + 125 and self.promotion[2]*SQUARE_SIZE + 210 < y < self.promotion[2]*SQUARE_SIZE + 260:
self.board[self.promotion[2]][self.promotion[1]] = 'B' + self.promotion[0]
self.promotion = None
elif self.mate or self.stale:
if 265 < x < 395 and 270 < y < 330:
self.game = [self.game[3], self.game[3], time.time(), self.game[3]]
self.reset_game()
elif 405 < x < 535 and 270 < y < 330:
self.game = None
self.game_type = None
elif 345 < x < 455 and 335 < y < 375:
self.run = False
elif self.game_type is None:
if self.game is None:
if 295 < x < 505:
if 220 < y < 270:
self.game = [301, 300, time.time(), 300]
elif 280 < y < 330:
self.game = [601, 600, time.time(), 600]
elif 340 < y < 390:
self.game = [901, 900, time.time(), 900]
else:
if 220 < y < 270 and 295 < x < 505 and 3*self.page < len(self.game_selections):
self.game_type = self.game_selections[0+(self.page*3)].split('@')[1].split(',')
self.reset_game()
elif 280 < y < 330 and 295 < x < 505 and 1 + 3*self.page < len(self.game_selections):
self.game_type = self.game_selections[1+(self.page*3)].split('@')[1].split(',')
self.reset_game()
elif 340 < y < 390 and 295 < x < 505 and 2 + 3*self.page < len(self.game_selections):
self.game_type = self.game_selections[2+(self.page*3)].split('@')[1].split(',')
self.reset_game()
elif 460 < x < 490 and 395 < y < 415 and self.page < (len(self.game_selections) // 3):
self.page += 1
elif 310 < x < 340 and 395 < y < 415 and self.page > 0:
self.page -= 1
elif 690 < x < 790 and 540 < y < 590:
self.game = None
def reset_game(self):
self.board = copy.deepcopy(self.game_boards[int(self.game_type[0])])
self.checking_board = copy.deepcopy(self.game_boards[int(self.game_type[0])])
self.game[2] = time.time()
self.moves = []
self.piece_selected = ''
self.piece_pos = []
self.move = '1'
self.checked = False
self.mate = False
self.stale = False
self.promotion = None
self.move50stalemate = False
self.repetition3_boards = []
self.castle_direction = '10'
if int(self.game_type[1]) == 1:
self.check3mode = [0, 0]
else:
self.check3mode = None
def check_whether_king_check(self, king, move_piece_checker=False, x=100, y=100, king_pos_checker=False):
king_pos_x = x
king_pos_y = y
checked = False
if king_pos_x == 100 and king_pos_y == 100:
for i in range(8):
for j in range(8):
if self.checking_board[j][i].split('X')[0] == 'K' + king:
king_pos_x = i
king_pos_y = j
break
if king_pos_checker:
return king_pos_x, king_pos_y
else:
if king == '1':
opposite = '2'
else:
opposite = '1'
for j in range(8):
for i in range(8):
if not checked and opposite in self.checking_board[j][i]:
available_moves = self.calc_piece_moves(self.checking_board[j][i], i, j, True)
for move in available_moves:
if move[0] == king_pos_x and move[1] == king_pos_y:
checked = True
break
if checked and not move_piece_checker:
if self.check_for_mate(king):
self.piece_selected = ''
self.piece_pos = []
self.moves = []
self.mate = True
if move_piece_checker:
return checked
else:
self.checked = checked
def check_for_stale(self, me):
stale = True
repetition_stale = False
self.repetition3_boards.append(copy.deepcopy(self.board))
for board in self.repetition3_boards:
if self.repetition3_boards.count(board) == 3:
self.repetition3_boards.clear()
repetition_stale = True
break
for j in range(8):
for i in range(8):
if me in self.checking_board[j][i]:
moves = self.calc_piece_moves(self.checking_board[j][i], i, j, True)
if moves is not None:
stale = False
break
if self.move50stalemate == 50:
stale = True
if repetition_stale:
stale = True
return stale
def check_for_mate(self, king):
old_piece_selected = self.piece_selected
old_piece_pos = self.piece_pos
old_moves = self.moves
mate = True
for j in range(8):
for i in range(8):
if king in self.checking_board[j][i] and mate:
moves = self.calc_piece_moves(self.checking_board[j][i], i, j, mate_checker=True)
if moves is not None:
for k in range(len(moves)):
self.checking_board = copy.deepcopy(self.board)
self.checking_board[self.piece_pos[1]][self.piece_pos[0]] = '0'
self.checking_board[moves[k][1]][moves[k][0]] = self.piece_selected
if len(moves[k]) == 3:
if moves[k][2] == -1 or moves[k][2] == 1:
self.checking_board[moves[k][1] + moves[k][2]][moves[k][0]] = '0'
if not self.check_whether_king_check(king, True):
self.checking_board = copy.deepcopy(self.board)
mate = False
break
self.checking_board = copy.deepcopy(self.board)
if self.check3mode is not None and (self.check3mode[0] == 3 or self.check3mode[1] == 3):
mate = True
if not mate:
self.piece_pos = old_piece_pos
self.piece_selected = old_piece_selected
self.moves = old_moves
return mate
def move_piece(self, y, x, double_move=False, en_passant_capture=0, castling_side='0'):
oldx = self.piece_pos[0]
oldy = self.piece_pos[1]
self.move50stalemate += 0.5
self.checked = False
self.board[oldy][oldx] = '0'
done = False
if '1' in self.piece_selected:
me = '1'
opposite = '2'
else:
me = '2'
opposite = '1'
self.update_time(True)
for i in range(8):
for j in range(8):
if 'D' in self.board[j][i]:
self.board[j][i] = self.board[j][i].split('D')[0]
if opposite in self.board[y][x] and int(self.game_type[1]) == 9 and 'P' not in self.piece_selected:
self.piece_selected = self.piece_selected + 'S'
if double_move:
self.board[y][x] = self.piece_selected + 'D'
done = True
elif 'D' in self.piece_selected:
self.board[y][x] = self.piece_selected.split('D')[0]
done = True
if 'P' in self.piece_selected:
self.move50stalemate = 0
if me == '1':
if y == 0:
self.promotion = [me, x, y]
else:
if y == 7:
self.promotion = [me, x, y]
if en_passant_capture != 0:
self.board[y][x] = self.piece_selected
self.board[y+en_passant_capture][x] = '0'
done = True
if 'X' in self.piece_selected:
self.board[y][x] = self.piece_selected.split('X')[0]
done = True
if castling_side != '0':
if castling_side == '1':
self.board[y][x] = self.piece_selected.split('X')[0]
self.board[y][x+1] = '0'
self.board[y][x-1] = 'R' + me
elif castling_side == '2':
self.board[y][x] = self.piece_selected.split('X')[0]
self.board[y][x-2] = '0'
self.board[y][x+1] = 'R' + me
if self.castle_direction == '10' and int(self.game_type[1]) == 8:
self.castle_direction = castling_side
done = True
if 'K' in self.piece_selected and (int(self.game_type[1]) == 2 or int(self.game_type[1]) == 3):
if 5 > x > 2 == int(self.game_type[1]) and 2 < y < 5:
self.mate = True
elif y == 0 and int(self.game_type[1]) == 3:
self.mate = True
if not done:
self.board[y][x] = self.piece_selected
self.moves.clear()
self.piece_pos = []
self.piece_selected = ''
self.checking_board = copy.deepcopy(self.board)
if not self.mate:
if self.move == '1':
self.move = '2'
else:
self.move = '1'
if int(self.game_type[1]) == 1 and self.check_whether_king_check(self.move, True):
self.check3mode[int(self.move)-1] += 1
if self.check_for_stale(self.move):
self.stale = True
self.check_whether_king_check(self.move)
def calc_piece_moves(self, piece, x, y, king_check=False, mate_checker=False):
if not self.mate or not self.stale:
if '1' in piece:
me = '1'
opposite = '2'
else:
me = '2'
opposite = '1'
moves = []
if 'P' in piece:
if opposite == '1':
if opposite not in self.checking_board[y+1][x] and me not in self.checking_board[y+1][x]:
moves.append([x, y+1])
if x+1 < 8 and (opposite in self.checking_board[y+1][x+1] or (x+1 < 8 and opposite in self.checking_board[y][x+1] and 'D' in self.checking_board[y][x+1])):
if 'D' in self.checking_board[y][x + 1]:
moves.append([x + 1, y + 1, -1])
else:
moves.append([x + 1, y + 1])
if x-1 > -1 and (opposite in self.checking_board[y+1][x-1] or (x-1 > -1 and opposite in self.checking_board[y][x-1] and 'D' in self.checking_board[y][x-1])):
if 'D' in self.checking_board[y][x - 1]:
moves.append([x - 1, y + 1, -1])
else:
moves.append([x - 1, y + 1])
if y == 1 and opposite not in self.checking_board[y+1][x] and opposite not in self.checking_board[y+2][x] and me not in self.checking_board[y+1][x] and me not in self.checking_board[y+2][x]:
moves.append([x, y+2, 2])
elif opposite == '2':
if opposite not in self.checking_board[y-1][x] and me not in self.checking_board[y-1][x]:
moves.append([x, y-1])
if x-1 > -1 and (opposite in self.checking_board[y-1][x-1] or (x-1 > -1 and opposite in self.checking_board[y][x-1] and 'D' in self.checking_board[y][x-1])):
if 'D' in self.checking_board[y][x - 1]:
moves.append([x - 1, y - 1, 1])
else:
moves.append([x - 1, y - 1])
if x+1 < 8 and (opposite in self.checking_board[y-1][x+1] or (x+1 < 8 and opposite in self.checking_board[y][x+1] and 'D' in self.checking_board[y][x+1])):
if 'D' in self.checking_board[y][x+1]:
moves.append([x+1, y-1, 1])
else:
moves.append([x+1, y-1])
if y == 6 and opposite not in self.checking_board[y-1][x] and opposite not in self.checking_board[y-2][x] and me not in self.checking_board[y-1][x] and me not in self.checking_board[y-2][x]:
moves.append([x, y-2, 2])
if 'N' in piece:
for i in self.knight_moves:
if -1 < (x + i[0]) < 8 and -1 < (y + i[1]) < 8 and me not in self.checking_board[y+i[1]][x+i[0]]:
moves.append([x + i[0], y + i[1]])
if 'B' in piece or 'Q' in piece:
for _ in range(4):
end = False
i = 1
while not end:
imod_x = i * self.bishop_moves[_][0]
imod_y = i * self.bishop_moves[_][1]
if -1 < (x + imod_x) < 8 and -1 < (y + imod_y) < 8:
if me in self.checking_board[y + imod_y][x + imod_x]:
end = True
elif opposite in self.checking_board[y + imod_y][x + imod_x]:
moves.append([x + imod_x, y + imod_y])
end = True
else:
moves.append([x + imod_x, y + imod_y])
else:
end = True
i += 1
if 'R' in piece or 'Q' in piece:
for _ in range(4):
end = False
i = 1
while not end:
imod_x = i * self.rook_moves[_][0]
imod_y = i * self.rook_moves[_][1]
if -1 < (x + imod_x) < 8 and -1 < (y + imod_y) < 8:
if me in self.checking_board[y + imod_y][x + imod_x]:
end = True
elif opposite in self.checking_board[y + imod_y][x + imod_x]:
moves.append([x + imod_x, y + imod_y])
end = True
else:
moves.append([x + imod_x, y + imod_y])
else:
end = True
i += 1
if 'K' in piece:
for _ in range(8):
if -1 < (x + self.king_moves[_][0]) < 8 and -1 < (y + self.king_moves[_][1]) < 8:
if me not in self.checking_board[y + self.king_moves[_][1]][x + self.king_moves[_][0]]:
moves.append([x + self.king_moves[_][0], y + self.king_moves[_][1]])
if not king_check and int(self.game_type[1]) != 5:
if self.castle_direction != '1' and 'X' in piece and not self.checked and self.checking_board[y][x + 1] == '0' and self.checking_board[y][x + 2] == '0' and (self.checking_board[y][x + 3] == 'R1X' or self.checking_board[y][x + 3] == 'R2X'):
if not self.check_whether_king_check(me, True, x+1, y):
moves.append([x+2, y, '3-1'])
if self.castle_direction != '2' and 'X' in piece and not self.checked and self.checking_board[y][x - 1] == '0' and self.checking_board[y][x - 2] == '0' and self.checking_board[y][x - 3] == '0' and (self.checking_board[y][x - 4] == 'R1X' or self.checking_board[y][x - 4] == 'R2X'):
if not self.check_whether_king_check(me, True, x-1, y) and not self.check_whether_king_check(me, True, x-2, y):
moves.append([x-2, y, '3-2'])
if not king_check:
self.piece_selected = piece
self.piece_pos = [x, y]
if not mate_checker:
delete = []
for i in range(len(moves)):
self.checking_board = copy.deepcopy(self.board)
self.checking_board[self.piece_pos[1]][self.piece_pos[0]] = '0'
self.checking_board[moves[i][1]][moves[i][0]] = self.piece_selected
if len(moves[i]) == 3 and (moves[i][2] == -1 or moves[i][2] == 1):
self.checking_board[moves[i][1]+moves[i][2]][moves[i][0]] = '0'
if self.check_whether_king_check(me, True):
delete.append(moves[i])
for d in delete:
moves.remove(d)
self.checking_board = copy.deepcopy(self.board)
if king_check or mate_checker:
return moves
else:
self.moves = moves
def update_time(self, time_to_switch=False):
if self.game_type is not None and not self.mate and not self.stale:
if self.game[0] < 1 or self.game[1] < 1:
if self.game[0] < 1:
self.game.append(1)
elif self.game[1] < 1:
self.game.append(2)
self.mate = True
if not self.mate and (time.time() - self.game[2] > 1 or time_to_switch):
self.game[int(self.move) - 1] -= (time.time() - self.game[2])
self.game[2] = time.time()
def if_enemy_on_tile(self, x, y):
if self.move == '1':
enemy = '2'
else:
enemy = '1'
if enemy in self.checking_board[y][x]:
return True
else:
return False
class Controller:
def __init__(self):
self.win = pygame.display.set_mode((WIDTH, HEIGHT))
self.model = Model()
self.view = View(self.win, self.model)
pygame.display.set_caption('Chess in Python')
def run(self):
clock = pygame.time.Clock()
while self.model.run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.model.run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
x, y = pygame.mouse.get_pos()
self.model.check_where_mouse_clicked(x, y)
self.view.draw()
self.model.update_time()
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
c = Controller()
c.run()