-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.py
1210 lines (870 loc) · 41.5 KB
/
chess.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
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from tkinter import *
from PIL import ImageTk, Image
import copy
# Create Window
root = Tk()
root.title("Chess")
root.geometry("720x720")
root.configure(bg="#303030")
# Define board colours
board_colour = {'w': "", 'b': ""}
# Create board for the pieces
board = [['0' for file in range(8)] for rank in range(8)]
# Create grid for the squares
squares = [[] for rank in range(8)]
class Piece:
def __init__(self, value, colour, rank, file, square_colour):
# On the creation of the piece, assign variables
self.value = value
self.colour = colour
self.rank = rank
self.file = file
self.selected = False
self.square_colour = square_colour
# Create lists for moves and move offsets
self.moves = []
self.move_offset = []
def info(self):
# Return info of the piece (eg. 'bK' for a Black King)
return self.colour + self.value
def select(self, bg_colour):
# Select only if the board is not locked
if locked != True:
# Get is selected variable
global is_selected
# Unselect the piece
if self.selected:
# Delete image from frame
self.piece_lbl.destroy()
# Replace the image not highlighted
self.place(None)
# Update variables
self.selected = False
is_selected = False
# Remove valid moves of the piece that is selected
self.hide_moves()
# Check if this is a capture target
elif not self.selected and is_selected:
# Get the already selected piece
for rank in board:
for piece in rank:
if piece != '0' and piece.selected:
selected_piece = piece
# If this piece is in the valid moves of the already selected piece, then capture
if (self.rank, self.file) in selected_piece.moves:
selected_piece.capture(self)
# Select the piece
elif not self.selected and not is_selected and self.colour == current_turn:
# Delete image from frame
self.piece_lbl.destroy()
# Replace the image highlighted
highlighted_image = Image.open("materials/highlight.png")
self.place(highlighted_image)
# Update variables
self.selected = True
is_selected = True
# Show valid moves of the piece that is selected
self.generate_legal_moves()
self.highlight_moves()
def place(self, bg_image):
# Get required info
pieceinfo = self.info()
piece_frame = squares[self.rank][self.file]
# Get piece image from resource folder
pieceimage = Image.open("materials/{}.png".format(pieceinfo))
# Keep frame a set size (don't let the image determine the size of the frame)
piece_frame.pack_propagate(0)
# If background is pre-defined
if bg_image is not None:
# Overlay the piece image onto the background
bg_image.paste(pieceimage, None, pieceimage)
tkpieceimage = ImageTk.PhotoImage(bg_image)
# If background is not pre-defined
else:
# Convert image into TkPhoto object
tkpieceimage = ImageTk.PhotoImage(pieceimage)
# Create image with same background colour as the frame it's in
self.piece_lbl = Label(piece_frame, image=tkpieceimage, bg=piece_frame["background"])
self.piece_lbl.image = tkpieceimage
# Bind mouse click to select the piece
self.piece_lbl.bind('<Button-1>', self.select)
# Place image in frame
self.piece_lbl.pack()
def delete(self, image, piece):
# Delete image from frame
if image:
self.piece_lbl.destroy()
# Delete itself from the board ('0' defaults as an empty square)
if piece:
board[self.rank][self.file] = '0'
def highlight_moves(self):
# Highlight moves
for target_rank, target_file in self.moves:
# Get target piece and square
target_piece = board[target_rank][target_file]
target_frame = squares[target_rank][target_file]
# If there is not a piece on the target square
if target_piece == '0':
# Get circle image from resource folder
circleimage = ImageTk.PhotoImage(file="materials/circle_1.png")
# Keep frame a set size (don't let the image determine the size of the frame)
target_frame.pack_propagate(0)
# Create image with same background colour as the frame it's in
circle = Label(target_frame, image=circleimage, bg=target_frame["background"])
circle.image = circleimage
# Bind click to move the piece, and pass on the target variables
circle.bind('<Button-1>', lambda event, r=target_rank, f=target_file: self.move_piece(r, f))
# Place the image
circle.pack()
# If there is a piece in the target square
elif target_piece != '0':
# Get circle image from resource folder
circleimage = Image.open("materials/circle_2.png")
# Replace the target piece with a circle around it
target_piece.delete(True, False)
target_piece.place(circleimage)
# If the piece is a pawn, highlight any en passant moves
if self.value == 'P':
# If there is a en passant move
if self.en_passant_move != None:
# Get target square of the en passant move
target_rank, target_file = self.en_passant_move
target_frame = squares[target_rank][target_file]
# Get circle image from resource folder
circleimage = ImageTk.PhotoImage(file="materials/circle_1.png")
# Keep frame a set size (don't let the image determine the size of the frame)
target_frame.pack_propagate(0)
# Create image with same background colour as the frame it's in
circle = Label(target_frame, image=circleimage, bg=target_frame["background"])
circle.image = circleimage
# Bind click to move the piece, and pass on the target variables
circle.bind('<Button-1>', lambda event, r=target_rank, f=target_file: self.pawn_en_passant_move(r, f))
# Place the image
circle.pack()
# If the piece is a king, highlight any castling moves
if self.value == 'K':
for target_rank, target_file in self.castling_moves:
# Get target square
target_frame = squares[target_rank][target_file]
# Get circle image from resource folder
circleimage = ImageTk.PhotoImage(file="materials/circle_1.png")
# Keep frame a set size (don't let the image determine the size of the frame)
target_frame.pack_propagate(0)
# Create image with same background colour as the frame it's in
circle = Label(target_frame, image=circleimage, bg=target_frame["background"])
circle.image = circleimage
# Bind click to move the piece, and pass on the target variables
circle.bind('<Button-1>', lambda event, r=target_rank, f=target_file: self.king_castling_move(r, f))
# Place the image
circle.pack()
def generate_moves(self, board, check_castling):
# Reset any existing moves
self.moves = []
self.move_offset = []
# Pawn Moves
if self.value == 'P':
self.pawn_moves(board)
self.get_en_passant_moves(board)
# Rook Moves
if self.value == 'R':
self.sliding_moves(board, 0, 3) # 0-3 is for straight directions
# Bishop Moves
if self.value == 'B':
self.sliding_moves(board, 4, 7) # 4-7 is for diagonal directions
# Queen Moves
if self.value == 'Q':
self.sliding_moves(board, 0, 7) # 0-7 is for diagonal and straight directions
# Knight Moves
if self.value == 'N':
self.knight_moves(board)
# King Moves
if self.value == 'K':
self.king_moves(board)
# If check castling is true
if check_castling:
self.get_castling_moves(board)
def generate_legal_moves(self):
# Generate pseudo legal moves (moves that aren't necessarily legal) & check for castling
self.generate_moves(board, True)
# Create variable of removed moves
self.removed_moves = []
# Check if each pseudo legal move is legal
for pseudo_move in self.moves:
# Create a temporary copy of the current board
temp_board = []
for piece in board:
# Use copy() to create new class instances of the pieces
temp_board.append(copy.copy(piece))
# Get target piece of the move
target_rank = pseudo_move[0]
target_file = pseudo_move[1]
target_piece = temp_board[target_rank][target_file]
# Play the move on the temp board
temp_board[target_rank][target_file] = Piece(self.value, self.colour, target_rank, target_file, colour_of_square(squares[target_rank][target_file]))
temp_board[self.rank][self.file] = '0'
# Check if any opponents responses include taking the king
if self.check_for_attacked_king(temp_board, change_turn(self.colour)):
# If so, then the last move was not legal
self.removed_moves.append(pseudo_move)
# Remove the non-legal moves
for move in set(self.removed_moves):
self.moves.remove(move)
def check_for_attacked_king(self, board, colour):
# Check if the king can be captured by the inputed colour's pieces
for rank in board:
for piece in rank:
# Get the pieces of the inputed colour
if piece != '0' and piece.colour == colour:
# Generate moves for the piece (don't check for castling)
piece.generate_moves(board, False)
# Check if a move consists of taking the king
for move_rank, move_file in piece.moves:
# Get the target piece of the move
move_target = board[move_rank][move_file]
# Return true if the piece target is a king
if move_target != '0' and move_target.value == 'K':
return True
# If passed all moves, then reutrn False
return False
def pawn_moves(self, board):
# White Pawn
if self.colour == 'w':
# Starting rank and move offset
self.start_rank = 6
self.move_offset.append((-1, 0))
# Black Pawn
elif self.colour == 'b':
# Starting rank and move offset
self.start_rank = 1
self.move_offset.append((1, 0))
# Capture move offsets
self.move_offset.append((self.move_offset[0][0], 1))
self.move_offset.append((self.move_offset[0][0], -1))
# Get target rank and file
target_rank, target_file = self.rank + self.move_offset[0][0], self.file
# Check if the move is on the board
if not target_rank > 7 or target_rank < 0:
# If the square in front of the pawn is not currently occupied by another piece
if board[target_rank][target_file] == '0':
# Add the move
self.moves.append((target_rank, target_file))
# Check if the pawn hasn't moved
if self.start_rank == self.rank:
# Get 2 square forward move position
target_rank, target_file = self.rank + (self.move_offset[0][0] * 2), self.file
# If the square two squares in front of the pawn is not occupied by another piece
if board[target_rank][target_file] == '0':
# Add the move
self.moves.append((target_rank, target_file))
# Check if the pawn can capture diagonally
for rank_offset, file_offset in self.move_offset[1:]:
# Get target rank and file
target_rank = self.rank + rank_offset
target_file = self.file + file_offset
# Check if target square exists on the board
try:
target_square = board[target_rank][target_file]
if target_rank < 0 or target_file < 0:
continue
except IndexError:
continue
# Check if there is an enemy piece at the target square
if target_square != '0' and target_square.colour != self.colour:
self.moves.append((target_rank, target_file))
def get_en_passant_moves(self, board):
# Remove any existing en passant moves
self.en_passant_move = None
# Check if there is an en passant target square
if en_passant_target != None:
# Check if the pawn can capture diagonally the en passant target
for rank_offset, file_offset in self.move_offset[1:]:
# Get target rank and file
target_rank = self.rank + rank_offset
target_file = self.file + file_offset
# Check if target square exists on the board
try:
target_square = board[target_rank][target_file]
if target_rank < 0 or target_file < 0:
continue
except IndexError:
continue
# Check if the target square is the en passant target
if (target_rank, target_file) == en_passant_target:
# If there target sqaure is the en passant capture, check if move is legal
# Create a temporary copy of the current board
temp_board = []
for piece in board:
# Use copy() to create new class instances of the pieces
temp_board.append(copy.copy(piece))
# Move the pawn to the en passant capture sqaure
temp_board[target_rank][target_file] = Piece(self.value, self.colour, target_rank, target_file, colour_of_square(squares[target_rank][target_file]))
temp_board[self.rank][self.file] = '0'
# Check if any opponents responses does not include taking the king
if not self.check_for_attacked_king(temp_board, change_turn(self.colour)):
# If so, then the en passant is legal
self.en_passant_move = (target_rank, target_file)
def sliding_moves(self, board, start_dir, end_dir):
# Straight move offsets
self.move_offset.append((-1, 0)) # Up
self.move_offset.append((1, 0)) # Down
self.move_offset.append((0, -1)) # Left
self.move_offset.append((0, 1)) # Right
# Diagonal move offsets
self.move_offset.append((-1, -1)) # Up Left
self.move_offset.append((-1, 1)) # Up Right
self.move_offset.append((1, -1)) # Down Left
self.move_offset.append((1, 1)) # Down Right
# Get moves in each direction
for direction in range(start_dir, end_dir + 1):
# Reset move multiple
move_multiple = 0
# Loop until all moves in direction has been found
while True:
# Increment move multiplier
move_multiple += 1
# Get target rank and file
target_rank = self.rank + (self.move_offset[direction][0] * move_multiple)
target_file = self.file + (self.move_offset[direction][1] * move_multiple)
# Check if target square exists on the board
try:
target_square = board[target_rank][target_file]
if target_rank < 0 or target_file < 0:
break
except IndexError:
break
# Check if there is not a frienly piece at the target square
if (target_square != '0' and target_square.colour == self.colour):
break
# Add the move
self.moves.append((target_rank, target_file))
# If there is any enemy piece at the target square, then this is the last move for the direction
if target_square != '0':
break
def knight_moves(self, board):
# Knight move offsets
self.move_offset.append((-2, -1)) # Up 2 Left 1
self.move_offset.append((-2, 1)) # Up 2 Right 1
self.move_offset.append((-1, -2)) # Up 1 Left 2
self.move_offset.append((-1, 2)) # Up 1 Right 2
self.move_offset.append((1, -2)) # Down 1 Left 2
self.move_offset.append((1, 2)) # Down 1 Right 2
self.move_offset.append((2, -1)) # Down 2 Left 1
self.move_offset.append((2, 1)) # Down 2 Right 1
# Get move (or not) in each direction
for move in self.move_offset:
# Get target rank and file
target_rank = self.rank + move[0]
target_file = self.file + move[1]
# Check if target square exists on the board
try:
target_square = board[target_rank][target_file]
if target_rank < 0 or target_file < 0:
continue
except IndexError:
continue
# Check if there is not a frienly piece at the target square
if (target_square != '0' and target_square.colour == self.colour):
continue
# Add the move
self.moves.append((target_rank, target_file))
def king_moves(self, board):
# King move offsets
self.move_offset.append((-1, 0)) # Up
self.move_offset.append((1, 0)) # Down
self.move_offset.append((0, -1)) # Left
self.move_offset.append((0, 1)) # Right
self.move_offset.append((-1, -1)) # Up Left
self.move_offset.append((-1, 1)) # Up Right
self.move_offset.append((1, -1)) # Down Left
self.move_offset.append((1, 1)) # Down Right
# Get move (or not) in each direction
for move in self.move_offset:
# Get target rank and file
target_rank = self.rank + move[0]
target_file = self.file + move[1]
# Check if target square exists on the board
try:
target_square = board[target_rank][target_file]
if target_rank < 0 or target_file < 0:
continue
except IndexError:
continue
# Check if there is not a frienly piece at the target square
if (target_square != '0' and target_square.colour == self.colour):
continue
# Add the move
self.moves.append((target_rank, target_file))
def get_castling_moves(self, board):
# Reset any existing castling moves
self.castling_moves = []
# If player is still legible to castle short
if castling_short[self.colour] != False:
# Check if there are not any pieces blocking the castling move
if board[self.rank][self.file + 1] == '0' and board[self.rank][self.file + 2] == '0':
# Check if the king doesn't excape check
if self.check_for_attacked_king(board, change_turn(self.colour)) != True:
# Check if the king doesn't pass through check or go into check
if self.check_castling(board, [1, 2]):
# If all checks are passed, then king can legally castle short
self.castling_moves.append((self.rank, self.file + 2))
# If player is still legible to castle long
if castling_long[self.colour] != False:
# Check if there are not any pieces blocking the castling move
if board[self.rank][self.file - 1] == '0' and board[self.rank][self.file - 2] == '0' and board[self.rank][self.file - 3] == '0':
# Check if the king doesn't excape check
if self.check_for_attacked_king(board, change_turn(self.colour)) != True:
# Check if the king doesn't pass through check or go into check
if self.check_castling(board, [-1, -2, -3]):
# If all checks are passed, then king can legally castle long
self.castling_moves.append((self.rank, self.file - 2))
def check_castling(self, board, offsets):
# Check if the king doesn't pass through check or go into check
for offset in offsets:
# Create a temporary copy of the current board
temp_board = []
for piece in board:
# Use copy() to create new class instances of the pieces
temp_board.append(copy.copy(piece))
# Move the king over in it's rank by the offset
temp_board[self.rank][self.file + offset] = Piece(self.value, self.colour, self.rank, self.rank + offset, colour_of_square(squares[self.rank][self.file + offset]))
temp_board[self.rank][self.file] = '0'
# Check if the king is not in check
if self.check_for_attacked_king(temp_board, change_turn(self.colour)):
# If the king is in check, then castling is not a legal move
return False
# If passes all moves, then castling is a legal move
return True
def evaluate(self):
global locked
# Check if opponent has any legal moves
opponent_legal_moves = []
for rank in board:
for piece in rank:
# Get the enemy pieces
if piece != '0' and piece.colour != self.colour:
# Generate legal moves for that piece
piece.generate_legal_moves()
# If the piece has legal moves, add them to the list
if piece.moves:
opponent_legal_moves.append(piece.moves)
# Check if move puts opponent's king in check / checkmate
if self.check_for_attacked_king(board, self.colour):
# If the opponent has no legal moves and is in check, then checkmate
if not opponent_legal_moves:
# Lock the board
locked = True
# Find the winner
if self.colour == 'w':
winner_text = "WHITE WINS!"
elif self.colour == 'b':
winner_text = "BLACK WINS!"
# Create checkmate label
checkmate_lbl = Label(boardframe, text=winner_text, bg="#303030", fg="White", font="comicsans 96", width=720)
checkmate_lbl.place(x=360, y=360, anchor=CENTER)
# Update the board and after 5 seconds (5000 milliseconds) delete the label
root.update()
root.after(5000, checkmate_lbl.destroy())
# If the opponent does have legal moves and is in check, then check
else:
# Create check label
check_lbl = Label(boardframe, text="CHECK", bg="#303030", fg="White", font="comicsans 96", width=720)
check_lbl.place(x=360, y=360, anchor=CENTER)
# Update the board and after 2 seconds (2000 milliseconds) delete the label
root.update()
root.after(2000, check_lbl.destroy())
# Check for draw by stalemate
else:
# If the opponent has no legal moves and is not in check, then stalemate
if not opponent_legal_moves:
# Lock the board
locked = True
# Create stalemate label
stalemate_lbl = Label(boardframe, text="STALEMATE", bg="#303030", fg="White", font="comicsans 96", width=720)
stalemate_lbl.place(x=360, y=360, anchor=CENTER)
# Update the board and after 5 seconds (5000 milliseconds) delete the label
root.update()
root.after(5000, stalemate_lbl.destroy())
def pawn_promotion(self, rank, file):
# Get the piece on the target square
piece = board[rank][file]
# Check if the piece is a pawn
if piece != '0' and piece.value == 'P':
# White pawn promotion rank
if piece.colour == 'w':
piece.promote_rank = 0
# Black pawn promotion rank
elif piece.colour == 'b':
piece.promote_rank = 7
# Check if the pawn is on the promotion rank
if piece.rank == piece.promote_rank:
# If so, prompt the user with a window to choose promotion piece
promotion_window(piece)
def hide_moves(self):
# Add any castling moves
if self.value == 'K':
moves = self.moves + self.castling_moves
else:
moves = self.moves
# Add any en passant moves
if self.value == 'P' and self.en_passant_move != None:
self.moves.append(self.en_passant_move)
moves = self.moves
# Unhighlight moves back to original square colour
for target_rank, target_file in moves:
# Get target piece and square
target_piece = board[target_rank][target_file]
target_frame = squares[target_rank][target_file]
# If the square is empty, remove the circle
if target_piece == '0':
# Destory the image inside the frame
for image in target_frame.winfo_children():
image.destroy()
# If the square is not empty, remove the circle from the piece
else:
# Replace the image without the circle around it
target_piece.delete(True, False)
target_piece.place(None)
def move_piece(self, target_rank, target_file):
# Unhighlight existing moves
self.hide_moves()
# Update castling availability
self.castling_availability()
# If pawn just did a double move, update the en passant target square
global en_passant_target
if self.value == 'P' and self.rank + 2 * self.move_offset[0][0] == target_rank:
# Set the en passant target square one square behind the double move
en_passant_target = (self.rank + self.move_offset[0][0], self.file)
else:
# Else reset the en passant target
en_passant_target = None
# Move the piece to the target square
board[target_rank][target_file] = Piece(self.value, self.colour, target_rank, target_file, colour_of_square(squares[target_rank][target_file]))
board[target_rank][target_file].place(None)
# Once the piece is placed, delete the old piece
self.delete(True, True)
# Check for pawn promotion
self.pawn_promotion(target_rank, target_file)
# Update the move to the board
self.update_move()
def pawn_en_passant_move(self, target_rank, target_file):
# Reset the en passant target square
global en_passant_target
en_passant_target = None
# Unhighlight existing moves
self.hide_moves()
# Get the pawn target of the en passant capture
target_pawn = board[target_rank - self.move_offset[0][0]][target_file]
# Delete the captured pawn
target_pawn.delete(True, True)
# Move the pawn to the en passant capture location
board[target_rank][target_file] = Piece(self.value, self.colour, target_rank, target_file, colour_of_square(squares[target_rank][target_file]))
board[target_rank][target_file].place(None)
# Once the piece is placed, delete the old piece
self.delete(True, True)
# Update the move to the board
self.update_move()
def king_castling_move(self, target_rank, target_file):
# Unhighlight existing moves
self.hide_moves()
# Update castling availability
self.castling_availability()
# Find and move the required rook
if target_file > self.file:
# Rook on king side
rook = board[self.rank][7]
board[target_rank][target_file - 1] = Piece(rook.value, self.colour, target_rank, target_file - 1, colour_of_square(squares[target_rank][target_file - 1]))
board[target_rank][target_file - 1].place(None)
else:
# Rook on queen side
rook = board[self.rank][0]
board[target_rank][target_file + 1] = Piece(rook.value, self.colour, target_rank, target_file + 1, colour_of_square(squares[target_rank][target_file + 1]))
board[target_rank][target_file + 1].place(None)
# Move the king to the new square
board[target_rank][target_file] = Piece(self.value, self.colour, target_rank, target_file, colour_of_square(squares[target_rank][target_file]))
board[target_rank][target_file].place(None)
# Delete old pieces
rook.delete(True, True)
self.delete(True, True)
# Update the move to the board
self.update_move()
def capture(self, target_piece):
# Unhighlight existing moves
self.hide_moves()
# Update castling availability
self.castling_availability()
# Get the capture location
capture_rank, capture_file = target_piece.rank, target_piece.file
# Delete the captured pieces from the board
target_piece.delete(True, True)
# Move the capturing piece to where the capture piece is
board[capture_rank][capture_file] = Piece(self.value, self.colour, capture_rank, capture_file, colour_of_square(squares[capture_rank][capture_file]))
board[capture_rank][capture_file].place(None)
# Once the piece is placed, delete the old piece
self.delete(True, True)
# Check for pawn promotion
self.pawn_promotion(capture_rank, capture_file)
# Update the move to the board
self.update_move()
def update_move(self):
# Update selected variable
global is_selected
is_selected = False
# Check for check/checkmate
self.evaluate()
# Switch player's turn
global current_turn
current_turn = change_turn(self.colour)
def castling_availability(self):
global castling_short, castling_long
# If the moved piece was a king, revoke castling privileges for that colour
if self.value == 'K':
castling_short[self.colour] = False
castling_long[self.colour] = False
# If the moved piece was a rook, revoke castling privileges only for that side (ie. short / long)
if self.value == 'R':
# Queen side (long)
if self.file == 0:
castling_long[self.colour] = False
# King side (short)
if self.file == 7:
castling_short[self.colour] = False
def start():
''' Title Header '''
# Title frame
global title_frame
title_frame = Frame(root, bg="#303030")
title_frame.pack()
# Title content
title_1_lbl = Label(title_frame, text="CHESS", font="comicsans 92", bg="#303030", fg="White")
title_1_lbl.pack(pady=(80, 0))
title_2_lbl = Label(title_frame, text="Created By: Samuel Johnston", font="comicsans 14", bg="#303030", fg="White")
title_2_lbl.pack(pady=(0, 60))
''' Custom Board Selection '''
# Fen for board editor
global fen
fen = StringVar()
fen.set("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
# Create custom board position frame
global board_position_editor
board_position_editor = Frame(root, bg="#303030")
board_position_editor.pack()
# Show editor title
custom_board_lbl = Label(board_position_editor, text="CUSTOMISE BOARD POSITION", font="comicsans 14", bg="#303030", fg="White")
custom_board_lbl.pack(pady=(0, 10))
# Show the fen editor
fen_frame = Frame(board_position_editor, bg="#303030")
fen_lbl = Label(fen_frame, text="Board Fen:", font="comicsans 12", bg="#303030", fg="White")
fen_entry = Entry(fen_frame, textvariable=fen, width="50", highlightbackground="#303030")
fen_lbl.pack(side=LEFT, padx=(0, 10))
fen_entry.pack(side=RIGHT)
fen_frame.pack(pady=(0, 20))
''' Custom Colour Selection '''
# Colours for board
global colour_b, colour_w
colour_b, colour_w = StringVar(), StringVar()
colour_b.set("#f8dcb4")
colour_b.trace("w", lambda name, index, mode: update_colours())
colour_w.set("#b88c64")
colour_w.trace("w", lambda name, index, mode: update_colours())
# Create colour editor frame
global colour_editor
colour_editor = Frame(root, bg="#303030")
colour_editor.pack()
# Show editor title
custom_colour_lbl = Label(colour_editor, text="CUSTOMISE BOARD COLOUR", font="comicsans 14", bg="#303030", fg="White")
custom_colour_lbl.pack(pady=(0, 10))
# Show the colour editor
colour_frame = Frame(colour_editor, bg="#303030")
# Split colour frame into 3 sections horizontally
colour_lbl = Frame(colour_frame, bg="#303030")
colour_lbl.pack(side=LEFT)
colour_entry = Frame(colour_frame, bg="#303030")
colour_entry.pack(side=LEFT)
colour_display = Frame(colour_frame, bg="#303030")
colour_display.pack(side=RIGHT)
# Section 1: Labels
colour_1_lbl = Label(colour_lbl, text="White Colour:", font="comicsans 12", bg="#303030", fg="White")
colour_1_lbl.pack(padx=(0, 10), pady=(0, 2))
colour_2_lbl = Label(colour_lbl, text="Black Colour:", font="comicsans 12", bg="#303030", fg="White")
colour_2_lbl.pack(padx=(0, 10), pady=(2, 0))
# Section 2: Entry Boxes
colour_1_entry = Entry(colour_entry, textvariable=colour_b, width="15", highlightbackground="#303030")
colour_1_entry.pack()
colour_2_entry = Entry(colour_entry, textvariable=colour_w, width="15", highlightbackground="#303030")
colour_2_entry.pack()
# Section 3: Updating colour frames
global colour_1_display, colour_2_display
colour_1_display = Frame(colour_display, bg=colour_b.get(), height="22", width="50")
colour_1_display.pack(padx="3", pady="3")
colour_2_display = Frame(colour_display, bg=colour_w.get(), height="22", width="50")
colour_2_display.pack(padx="3", pady="3")
colour_frame.pack(pady=(0, 20))
''' Start Button '''
global start_button
start_button = Label(root, text="START", font="comicsans 32", bg="#303030", fg="White")
start_button.pack(pady=(50, 0))
start_button.bind("<Button-1>", start_game)
def update_colours():
# When entry box is edited, update the colour of the frames
# If the colour doesn't exist, update the colour to white
try:
colour_1_display.config(bg=colour_b.get())
except:
colour_1_display.config(bg="white")
try:
colour_2_display.config(bg=colour_w.get())
except:
colour_2_display.config(bg="white")
def start_game(event):
# Get the start fen from the entry box
start_fen = fen.get()