-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupUI.py
More file actions
2782 lines (2404 loc) · 137 KB
/
setupUI.py
File metadata and controls
2782 lines (2404 loc) · 137 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
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
# Sorry for messy code design and one file consist of 2.000 lines code stacking. I learned a lot since
# i finish this project.
# NOTE : Due to bad code design, it's too hard to implement board flipping feature (forgive me xD)
# Built-in Modules.
from io import UnsupportedOperation
from multiprocessing.dummy.connection import families
from os import devnull, putenv, write
from plistlib import UID
from re import A
from xml.etree.ElementTree import PI
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QFocusEvent, QIcon, QPainter, QPalette, QPen, QBrush, QColor, QPixmap
from PyQt5.QtCore import PYQT_VERSION, QRect, QSize, Qt, forcepoint
from PyQt5.QtWidgets import QApplication, QFrame, QGridLayout, QLabel, QMainWindow, QPushButton, QWidget
from PyQt5.sip import delete
from pynput import mouse
import os
import string
import itertools
import functools
import time
import chess
import sys
import subprocess
import random
# Importing non built-in modules.
import control
import playStockfish
# Creating base variables to support the program.
global enpassant_square
global checkmate_verificator
global prev_movement
global after_movement
file = list(string.ascii_lowercase)[0:8]
rank = list(range(1,9))
rank_y = rank
enpassant_square = [] # to save pawn's En Passant square.
prev_movement = None # to mark previous movement sequent.
after_movement = None # to mark previous movement sequent.
# Board coordinate representation
# Ex : b1 -> ['b', 1, 2] (the last item represent 'b' as the second character alphabetically.)
board_coordinates = []
for i in rank:
board_2 = list(map(lambda x, y :[y, i, x], rank_y, file))
board_coordinates += board_2
class Ui_MainWindow(QMainWindow):
# Setting Up Dashboard UI
# This part is imported from UI file which is created by PyQt5 Designer (You can see it personally on "dashboard_uifile.ui").
boardSquare_list = []
def __init__(self):
super(QMainWindow, self).__init__()
# Creating universal widget.
global main_centralWidget
self._centralwidget = QWidget()
main_centralWidget = self._centralwidget
self.setCentralWidget(self._centralwidget)
self.setGeometry(0,0, 1920, 1080)
# Creating label object to set UI background.
self.label = QtWidgets.QLabel(self._centralwidget)
self.label.setGeometry(QtCore.QRect(0, -40, 1931, 1061))
font = QtGui.QFont()
font.setItalic(False)
self.label.setFont(font)
self.label.setAutoFillBackground(False)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("bckgroundchess.png"))
# Giving a little bit credit for creator XD.
self.creatorName = QtWidgets.QLabel(self._centralwidget)
self.creatorName.setFont(QtGui.QFont("Niagara Engraved", 10))
self.creatorName.setText("Created by : Farrel Dinarta")
self.creatorName.setGeometry(QtCore.QRect(910, 865, 200, 200))
# Play button set up
self.pushButton = QtWidgets.QPushButton(self._centralwidget)
self.pushButton.setGeometry(QtCore.QRect(720, 790, 481, 141))
self.pushButton.setStyleSheet("""
QPushButton { background-color: grey; }
QPushButton:focusressed { background-color: black; }
QPushButton:focus { background-color: rgb(168, 168, 168); }
""")
font = QtGui.QFont()
font.setFamily("Niagara Engraved")
font.setPointSize(40)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setIconSize(QtCore.QSize(60, 60))
self.pushButton.clicked.connect(lambda : self.gameSettingsRoom())
self.retranslateUi_dashboard()
# Additional Dashboard UI Settings
def retranslateUi_dashboard(self):
# Assigning the play button.
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("MainWindow", "MyChess"))
self.pushButton.setText(_translate("MainWindow", "PLAY"))
# Signal declaration for mouse activity.
released = QtCore.pyqtSignal(int, int)
# Connecting mouse to the program.
def window_mouseclick_detection(window):
window._listener = mouse.Listener(on_click=window._handle_click)
window._listener.start()
# Giving mouse-click a command to do something.
def _handle_click(x, y, button, pressed, win):
for players in Player.player_list:
if players.isMyTurn:
for pc in Pieces.pieces:
if pc.strcolor == players.color:
pc.isMoving = True
for sqr2 in Ui_MainWindow.boardSquare_list:
if [sqr2.coordinate[2], sqr2.coordinate[1]] == prev_movement:
continue
if sqr2.isOccupied and sqr2.piece_color == pc.strcolor:
sqr2.squares.setStyleSheet(sqr2.info_padding)
else:
pc.isMoving = False
break
for sqr in Ui_MainWindow.boardSquare_list:
if [sqr.coordinate[2], sqr.coordinate[1]] == prev_movement:
continue
if not sqr.isOccupied:
sqr.squares.setStyleSheet(sqr.info_padding)
sqr.squares.setIcon(QIcon(""))
for plyr in Player.player_list:
if plyr.isMyTurn:
if sqr.isOccupied and plyr.color != sqr.piece_color and sqr.isClicked:
sqr.squares.setEnabled(False)
# Board color.
color_dark = "(81, 150, 62)"
color_light = "(200, 224, 206)"
# Function to create a game set-up room GUI.
def gameSettingsRoom(self):
global playComputer
global start_button
self.label.deleteLater()
self.creatorName.deleteLater()
self.pushButton.deleteLater()
self.setting_background = QtWidgets.QLabel(self._centralwidget)
self.setting_background.setPixmap(QtGui.QPixmap('Background_GameSettings.png'))
self.startGame_button = QPushButton(self._centralwidget)
self.startGame_button.clicked.connect(lambda : self.startGame())
self.startGame_button.setGeometry(QtCore.QRect(1320, 845, 531, 141))
self.startGame_button.setFont(QtGui.QFont("League Gothic", 30))
self.startGame_button.setText("START GAME")
self.startGame_button.setEnabled(False)
start_button = self.startGame_button
self.startGame_button.setStyleSheet(""" background-color: rgb(131,145,145);
border-style: outset;
border-radius: 15px;
border-color: black;
color: rgb(252, 252, 252);
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
padding: 4px;""")
self.startGame_button.show()
# Creating time control buttons (imported from control.py)
rapid_10 = control.GameMode("Rapid", 600, 80, 240, 250, 100)
rapid_10.createButton()
rapid_15 = control.GameMode("Rapid", 900, 350, 240, 250, 100)
rapid_15.createButton()
rapid_30 = control.GameMode("Rapid", 1800, 620, 240, 250, 100)
rapid_30.createButton()
blitz_3 = control.GameMode("Blitz", 180, 80, 435, 250, 100)
blitz_3.createButton()
blitz_5 = control.GameMode("Blitz", 300, 350, 435, 250, 100)
blitz_5.createButton()
blitz_3_2 = control.GameMode("Blitz", 180, 620, 435, 250, 100, 2)
blitz_3_2.createButton()
blitz_5_5 = control.GameMode("Blitz", 300, 890, 435, 250, 100, 5)
blitz_5_5.createButton()
bullet_1 = control.GameMode("Bullet", 60, 80, 630, 250, 100)
bullet_1.createButton()
bullet_2 = control.GameMode("Bullet", 120, 350, 630, 250, 100)
bullet_2.createButton()
bullet_1_1 = control.GameMode("Bullet", 60, 620, 630, 250, 100, 1)
bullet_1_1.createButton()
bullet_2_1 = control.GameMode("Bullet", 120, 890, 630, 250, 100, 1)
bullet_2_1.createButton()
hyper_bullet_15 = control.GameMode("Hyper Bullet", 15, 80, 825, 250, 100)
hyper_bullet_15.createButton()
hyper_bullet_30 = control.GameMode("Hyper Bullet", 30, 350, 825, 250, 100)
hyper_bullet_30.createButton()
self.setting_background.show()
# Creating bot setup room (imported from control.py)
playComputer = control.stockfish_controlRoom(1199,240, 101, 101)
playComputer.create_choosePlayBot()
# Starting Game (draw boards, assign pieces, etc.)
def startGame(self):
global a_file
global boardBorder
global BoardInfo
global player1_frame, player2_frame, player1_name,player2_name,color_isMyTurn, color_isNotMyTurn, name_color_isMyTurn, name_color_isNotMyTurn
self.setting_background.deleteLater()
self.startGame_button.deleteLater()
# Game Background setup.
self.gameBackground = QtWidgets.QLabel(self._centralwidget)
self.gameBackground.setText("")
self.gameBackground.setPixmap(QtGui.QPixmap('WoodenBackground.png'))
self.gameBackground.show()
boardBorder = QFrame(self._centralwidget)
boardBorder.setFrameShape(QFrame.StyledPanel)
boardBorder.setLineWidth(100)
boardBorder.resize(850,850)
boardBorder.move(525, 75)
boardBorder.show()
self.border = QtWidgets.QLabel(boardBorder)
self.border.setText("")
self.border.setPixmap(QPixmap('Board Border.png'))
self.border.resize(850, 850)
self.border.show()
# Game profile setup
boardFrame = QFrame(self._centralwidget)
boardFrame.setFrameShape(QFrame.StyledPanel)
boardFrame.setLineWidth(80)
boardFrame.resize(800, 800)
boardFrame.move(550, 100)
boardFrame.show()
color_isMyTurn = '#876316'
color_isNotMyTurn = '#45381b'
name_color_isMyTurn = 'white'
name_color_isNotMyTurn = 'grey'
player2_frame = QFrame(self._centralwidget)
player2_frame.setGeometry(QRect(405, 75, 120, 120))
player2_frame.show()
player2_name = QtWidgets.QLabel(self._centralwidget)
player2_name.setGeometry(QRect(425, 200, 140, 40))
player2_name.setFont(QtGui.QFont("Niagara Engraved", 25))
player2_name.show()
player1_frame = QFrame(self._centralwidget)
player1_frame.setGeometry(QRect(405, 805, 120, 120))
player1_frame.show()
player1_name = QtWidgets.QLabel(self._centralwidget)
player1_name.setGeometry(QRect(425, 755, 140, 40))
player1_name.setFont(QtGui.QFont("Niagara Engraved", 25))
player1_name.show()
# Creating board squares
v = 700
count_startDark = 0
count_startLight = 0
sqr_coordinate_counter = 0
rank_ctr = 1
for verti in range(8):
h = 0
if verti % 2 == 0:
for hori in range(8):
if count_startDark % 2 == 0:
if hori == 0:
board = Board(Ui_MainWindow.color_dark, h, v, 100, 100, boardFrame, board_coordinates[sqr_coordinate_counter],f"background-color: rgb{Ui_MainWindow.color_dark};border: none;color: rgb{Ui_MainWindow.color_light};" ,f"{str(rank_ctr)}")
board.piece_color = None
rank_ctr += 1
Ui_MainWindow.boardSquare_list.append(board)
else:
board = Board(Ui_MainWindow.color_dark, h, v, 100, 100, boardFrame, board_coordinates[sqr_coordinate_counter],f"background-color: rgb{Ui_MainWindow.color_dark};border: none;color: rgb{Ui_MainWindow.color_light};")
board.piece_color = None
Ui_MainWindow.boardSquare_list.append(board)
else:
board = Board(Ui_MainWindow.color_light, h, v, 100, 100, boardFrame, board_coordinates[sqr_coordinate_counter],f"background-color: rgb{Ui_MainWindow.color_light};border: none;color: rgb{Ui_MainWindow.color_dark};")
board.piece_color = None
Ui_MainWindow.boardSquare_list.append(board)
sqr_coordinate_counter += 1
count_startDark += 1
h += 100
count_startDark = 0
else:
for hori in range(8):
if count_startLight % 2 != 0:
board = Board(Ui_MainWindow.color_dark, h, v, 100, 100, boardFrame, board_coordinates[sqr_coordinate_counter],f"background-color: rgb{Ui_MainWindow.color_dark};border: none;color: rgb{Ui_MainWindow.color_light};")
board.piece_color = None
Ui_MainWindow.boardSquare_list.append(board)
else:
if hori == 0:
board = Board(Ui_MainWindow.color_light, h, v, 100, 100, boardFrame, board_coordinates[sqr_coordinate_counter] ,f"background-color: rgb{Ui_MainWindow.color_light};border: none;color: rgb{Ui_MainWindow.color_dark};",str(rank_ctr))
board.piece_color = None
rank_ctr += 1
Ui_MainWindow.boardSquare_list.append(board)
else:
board = Board(Ui_MainWindow.color_light, h, v, 100, 100, boardFrame, board_coordinates[sqr_coordinate_counter] ,f"background-color: rgb{Ui_MainWindow.color_light};border: none;color: rgb{Ui_MainWindow.color_dark};")
board.piece_color = None
Ui_MainWindow.boardSquare_list.append(board)
sqr_coordinate_counter += 1
count_startLight += 1
h += 100
count_startLight = 0
v -= 100
# Square indexing class
class BoardInfo:
posx = 84
posy = 781
board_info_list = []
def __init__(self, text, coloring):
self.text = text
self.coloring = coloring
self.labeling = QtWidgets.QLabel(boardFrame)
self.labeling.move(BoardInfo.posx, BoardInfo.posy)
self.labeling.setText(self.text)
self.labeling.setStyleSheet(self.coloring)
self.labeling.show()
# Create square indexing
for sqr in range(0, 8):
if sqr % 2 == 0:
board_info = BoardInfo(list(string.ascii_lowercase)[sqr], f"color:rgb{Ui_MainWindow.color_light};")
else:
board_info = BoardInfo(list(string.ascii_lowercase)[sqr], f"color:rgb{Ui_MainWindow.color_dark};")
BoardInfo.board_info_list.append(board_info)
BoardInfo.posx += 100
BoardInfo.posx = 5
BoardInfo.posy = 705
for sqr in range(1, 9):
if sqr % 2 == 0:
board_info = BoardInfo(str(sqr), f"color:rgb{Ui_MainWindow.color_dark};")
else:
board_info = BoardInfo(str(sqr), f"color:rgb{Ui_MainWindow.color_light};")
BoardInfo.board_info_list.append(board_info)
BoardInfo.posy -= 100
# Game Mode
GamePack.classic_game()
# Create Players
global White_Player
global Black_Player
# Applying game settings.
if playComputer.isClicked:
try:
if playComputer.playingWhite:
White_Player = Player('White')
Black_Player = Player('Black', True)
else:
White_Player = Player('White', True)
Black_Player = Player('Black')
first_move_list = [
[['e', 2], ['e',4]],
[['d', 2], ['d',4]],
[['c', 2], ['c',4]],
[['f', 2], ['f',4]],
[['e', 2], ['e',3]],
[['d', 2], ['d',3]],
[['c', 2], ['c',3]],
[['f', 2], ['f',3]],
[['b', 1], ['c', 3]],
[['g', 1], ['f', 3]]
]
bot_chooseFirstMove = random.choice(first_move_list)
for sqr in Ui_MainWindow.boardSquare_list:
if [sqr.coordinate[0], sqr.coordinate[1]] == bot_chooseFirstMove[0]:
for pc in Pieces.pieces:
if pc.strcolor == sqr.piece_color:
pc.isMoving = True
else:
pc.isMoving = False
sqr.squareClicked()
for sqr in Ui_MainWindow.boardSquare_list:
if [sqr.coordinate[0], sqr.coordinate[1]] == bot_chooseFirstMove[1]:
for pc in Pieces.pieces:
if pc.strcolor == sqr.piece_color:
pc.isMoving = True
else:
pc.isMoving = False
sqr.squareClicked()
except:
White_Player = Player('White')
Black_Player = Player('Black')
else:
White_Player = Player('White')
Black_Player = Player('Black')
for plyr in Player.player_list:
if plyr.isMyTurn:
player1_name.setText(plyr.name)
else:
player2_name.setText(plyr.name)
Player.launch_turn()
Ui_MainWindow.window_mouseclick_detection(self)
# Game-set class (Setting up pieces)
class GamePack:
bot_allowMovement = False
def classic_game():
# WHITE POINT OF VIEW
temp_pieceholder = []
pw1 = Pawn(Qt.white, "White", "Pawn", 2, 'a')
pw2 = Pawn(Qt.white, "White", "Pawn", 2, 'b')
pw3 = Pawn(Qt.white, "White", "Pawn", 2, 'c')
pw4 = Pawn(Qt.white, "White", "Pawn", 2, 'd')
pw5 = Pawn(Qt.white, "White", "Pawn", 2, 'e')
pw6 = Pawn(Qt.white, "White", "Pawn", 2, 'f')
pw7 = Pawn(Qt.white, "White", "Pawn", 2, 'g')
pw8 = Pawn(Qt.white, "White", "Pawn", 2, 'h')
w_rook_1 = Rook(Qt.white, "White", "Rook", 1, 'a')
w_rook_2 = Rook(Qt.white, "White", "Rook", 1, 'h')
w_bishop_1 = Bishop(Qt.white, "White", "Bishop", 1, 'c')
w_bishop_2 = Bishop(Qt.white, "White", "Bishop", 1, 'f')
w_knight_1 = Knight(Qt.white, "White", "Knight", 1, 'b')
w_knight_2 = Knight(Qt.white, "White", "Knight", 1, 'g')
w_queen = Queen(Qt.white, "White", "Queen", 1, "d")
w_king = King(Qt.white, "White", "King", 1, 'e')
temp_pieceholder.append(pw1)
temp_pieceholder.append(pw2)
temp_pieceholder.append(pw3)
temp_pieceholder.append(pw4)
temp_pieceholder.append(pw5)
temp_pieceholder.append(pw6)
temp_pieceholder.append(pw7)
temp_pieceholder.append(pw8)
temp_pieceholder.append(w_rook_1)
temp_pieceholder.append(w_rook_2)
temp_pieceholder.append(w_bishop_1)
temp_pieceholder.append(w_bishop_2)
temp_pieceholder.append(w_knight_1)
temp_pieceholder.append(w_knight_2)
temp_pieceholder.append(w_queen)
temp_pieceholder.append(w_king)
pb1 = Pawn(Qt.black, "Black", "Pawn", 7, 'a')
pb2 = Pawn(Qt.black, "Black", "Pawn", 7, 'b')
pb3 = Pawn(Qt.black, "Black", "Pawn", 7, 'c')
pb4 = Pawn(Qt.black, "Black", "Pawn", 7, 'd')
pb5 = Pawn(Qt.black, "Black", "Pawn", 7, 'e')
pb6 = Pawn(Qt.black, "Black", "Pawn", 7, 'f')
pb7 = Pawn(Qt.black, "Black", "Pawn", 7, 'g')
pb8 = Pawn(Qt.black, "Black", "Pawn", 7, 'h')
b_rook_1 = Rook(Qt.black, "Black", "Rook", 8, 'a')
b_rook_2 = Rook(Qt.black, "Black", "Rook", 8, 'h')
b_bishop_1 = Bishop(Qt.black, "Black", "Bishop", 8, 'c')
b_bishop_2 = Bishop(Qt.black, "Black", "Bishop", 8, 'f')
b_knight_1 = Knight(Qt.black, "Black", "Knight", 8, 'b')
b_knight_2 = Knight(Qt.black, "Black", "Knight", 8, 'g')
b_queen = Queen(Qt.black, "Black", "Queen", 8, "d")
b_king = King(Qt.black, "Black", "King", 8, 'e')
temp_pieceholder.append(pb1)
temp_pieceholder.append(pb2)
temp_pieceholder.append(pb3)
temp_pieceholder.append(pb4)
temp_pieceholder.append(pb5)
temp_pieceholder.append(pb6)
temp_pieceholder.append(pb7)
temp_pieceholder.append(pb8)
temp_pieceholder.append(b_rook_1)
temp_pieceholder.append(b_rook_2)
temp_pieceholder.append(b_bishop_1)
temp_pieceholder.append(b_bishop_2)
temp_pieceholder.append(b_knight_1)
temp_pieceholder.append(b_knight_2)
temp_pieceholder.append(b_queen)
temp_pieceholder.append(b_king)
for pc in temp_pieceholder:
for sqr in Ui_MainWindow.boardSquare_list:
if sqr.coordinate[0] == pc.file and sqr.coordinate[1] == pc.rank:
sqr.squares.setIcon(pc.image)
sqr.squares.setIconSize(QSize(80,80))
sqr.isOccupied = True
# Board Class
class Board(Ui_MainWindow):
pgn_string = ''
move_number = 0
move_num_changer = 0
enable_pgn_writeCheck = 0
enable_findBotMovement = True
push_botMovement = False
attacker = None
prev = None
first_move = True
first_move_bugKiller = 0
def __init__(self, color, posx, posy, size_x, size_y, frame, coordinate, info_padding, info=''):
self.color = color
self.posx = posx
self.posy = posy
self.size_x = size_x
self.size_y = size_y
self.coordinate = coordinate
self.info_padding = info_padding
self.info = info
self.isOccupied = False
self.isClicked = False
self.squares = QtWidgets.QPushButton(frame)
self.squares.setText("")
self.squares.setStyleSheet(info_padding)
self.squares.setGeometry(QRect(self.posx, self.posy, self.size_x, self.size_y))
self.squares.clicked.connect(lambda : self.squareClicked(False))
self.squares.show()
# Square click command.
def squareClicked(self, isBot=False):
global prev_movement
global after_movement
global switch_clock
global enpassant_square
global king_temp
isClicked_color = "(188, 191, 107)"
next_sequence = True
switch_clock = False
# Board.attacker is a variable which save the previous piece which had contact with the recent movement.
# The if statement defines whether player make a movement or just clicking a square.
if Board.attacker is not None:
# If the clicked square is in the piece's area_covered, then these codes will be executed.
if [self.coordinate[2], self.coordinate[1]] in Board.attacker.area_covered:
# Part to save previous movement track (red squares).
prev_movement = Board.attacker.position_numeric
after_movement = [self.coordinate[2], self.coordinate[1]]
next_sequence = True
# Part of Stockfish adaptation
if isBot:
playStockfish.BOT_Stockfish.disable_clockSwitch += 1
try:
if not Board.push_botMovement:
Board.push_botMovement = True
except:
pass
# PGN Writer
Board.enable_pgn_writeCheck += 1
Board.move_num_changer += 1
if Board.move_num_changer % 2 != 0:
Board.move_number += 1
Board.pgn_string += f"{str(Board.move_number)}. "
if not self.isOccupied:
if Board.attacker.ptype == "Pawn":
if Board.attacker.position_numeric[0] != self.coordinate[2]:
Board.pgn_string += f"{list(string.ascii_lowercase)[Board.attacker.position_numeric[0]-1]}x{self.coordinate[0]}{self.coordinate[1]} e.p. "
else:
Board.pgn_string += f"{self.coordinate[0]}{self.coordinate[1]} "
elif Board.attacker.ptype == "Knight":
Board.pgn_string += f"N{self.coordinate[0]}{self.coordinate[1]} "
else:
if Board.attacker.ptype == "King":
if self.coordinate[2]-Board.attacker.position_numeric[0] == 2:
Board.pgn_string += "O-O "
elif self.coordinate[2]-Board.attacker.position_numeric[0] == -2:
Board.pgn_string += "O-O-O "
else:
Board.pgn_string += f"{Board.attacker.ptype[0]}{self.coordinate[0]}{self.coordinate[1]} "
else:
Board.pgn_string += f"{Board.attacker.ptype[0]}{self.coordinate[0]}{self.coordinate[1]} "
else:
if Board.attacker.ptype == "Pawn":
Board.pgn_string += f"{list(string.ascii_lowercase)[Board.attacker.position_numeric[0]-1]}x{self.coordinate[0]}{self.coordinate[1]} "
elif Board.attacker.ptype == "Knight":
Board.pgn_string += f"Nx{self.coordinate[0]}{self.coordinate[1]} "
else:
Board.pgn_string += f"{Board.attacker.ptype[0]}x{self.coordinate[0]}{self.coordinate[1]} "
for plyr in Player.player_list:
if plyr.isMyTurn:
for kg in Pieces.pieces:
if kg.strcolor == plyr.color and kg.ptype == "King":
king_temp = kg
switch_clock = True
# Codes to execute piece killing
try:
if self.piece_color != Board.attacker.strcolor and self.isOccupied:
for victim in Pieces.pieces:
if victim.position_numeric == [self.coordinate[2], self.coordinate[1]]:
if victim.strcolor == 'Black':
for victim_black in Pieces.black_pieces:
if victim_black == victim:
Pieces.black_pieces.remove(victim_black)
del victim_black
break
else:
for victim_white in Pieces.white_pieces:
if victim_white == victim:
Pieces.white_pieces.remove(victim_white)
del victim_white
break
Pieces.pieces.remove(victim)
del victim
break
else:
if Board.attacker.ptype == "Pawn":
for pc in Pieces.pieces:
if pc == Board.attacker:
pc.en_passantable += 1
except:
pass
# Setting up the GUI back.
for check_occupy in Ui_MainWindow.boardSquare_list:
if [check_occupy.coordinate[2], check_occupy.coordinate[1]] == Board.attacker.position_numeric and check_occupy.isOccupied:
check_occupy.isOccupied = False
self.isOccupied = True
for pc in Pieces.pieces:
if pc == Board.attacker:
for sqr in Ui_MainWindow.boardSquare_list:
if [sqr.coordinate[2], sqr.coordinate[1]] == pc.position_numeric:
sqr.squares.setIcon(QIcon(""))
if not sqr.isOccupied:
sqr.squares.setIcon(QIcon(""))
if [self.coordinate[2], self.coordinate[1]] in Board.attacker.area_covered:
for pc in Pieces.pieces:
if pc == Board.attacker and not (pc.position_numeric == prev_movement):
self.squares.setIcon(QIcon(""))
self.squares.setIcon(Board.attacker.image)
self.squares.setIconSize(QSize(80,80))
for pc_moved in Pieces.pieces:
if pc_moved == Board.attacker:
if pc_moved.ptype == 'Pawn':
pc_moved.firstmove = False
try:
if pc_moved.strcolor == 'White':
pc_moved.area_covered.remove([pc_moved.position_numeric[0], pc_moved.position_numeric[1]+2])
else:
pc_moved.area_covered.remove([pc_moved.position_numeric[0], pc_moved.position_numeric[1]-2])
except:
pass
if pc_moved.ptype == "King" or pc_moved.ptype == "Rook":
prev_king_sqr = pc_moved.position_numeric[0]
pc_moved.firstMove = True
pc_moved.position_numeric = [self.coordinate[2], self.coordinate[1]]
# Logic to Implement Castling
if pc_moved.ptype == "King":
if abs(pc_moved.position_numeric[0] - prev_king_sqr) == 2:
for rook in Pieces.pieces:
if rook.ptype == "Rook" and rook.strcolor == pc_moved.strcolor and abs(pc_moved.position_numeric[0] - rook.position_numeric[0]) == 1:
pass
prev_rook_pos = rook.position_numeric
rook.position_numeric = [rook.position_numeric[0]-2, rook.position_numeric[1]]
rook.position_convention = [list(string.ascii_lowercase)[rook.position_numeric[0]-1], rook.position_numeric[1]]
rook.isMoving = False
rook.rank = rook.position_numeric[1]
rook.file = rook.position_numeric[0]
for sqr in Ui_MainWindow.boardSquare_list:
if [sqr.coordinate[2], sqr.coordinate[1]] == rook.position_numeric:
sqr.isOccupied = True
sqr.piece_color = rook.strcolor
sqr.squares.setIcon(rook.image)
sqr.squares.setIconSize(QSize(80, 80))
if [sqr.coordinate[2], sqr.coordinate[1]] == prev_rook_pos:
sqr.isOccupied = False
sqr.piece_color = None
sqr.squares.setIcon(QIcon(""))
elif rook.ptype == "Rook" and rook.strcolor == pc_moved.strcolor and abs(pc_moved.position_numeric[0] - rook.position_numeric[0]) == 2:
pass
prev_rook_pos = rook.position_numeric
rook.position_numeric = [rook.position_numeric[0]+3, rook.position_numeric[1]]
rook.position_convention = [list(string.ascii_lowercase)[rook.position_numeric[0]-1], rook.position_numeric[1]]
rook.isMoving = False
rook.rank = rook.position_numeric[1]
rook.file = rook.position_numeric[0]
for sqr in Ui_MainWindow.boardSquare_list:
if [sqr.coordinate[2], sqr.coordinate[1]] == rook.position_numeric:
sqr.isOccupied = True
sqr.piece_color = rook.strcolor
sqr.squares.setIcon(rook.image)
sqr.squares.setIconSize(QSize(80, 80))
if [sqr.coordinate[2], sqr.coordinate[1]] == prev_rook_pos:
sqr.isOccupied = False
sqr.piece_color = None
sqr.squares.setIcon(QIcon(""))
pc_moved.position_convention = [list(string.ascii_lowercase)[self.coordinate[2]-1], self.coordinate[1]]
pc_moved.isMoving = False
pc_moved.rank = self.coordinate[1]
pc_moved.file = self.coordinate[2]
self.piece_color = pc_moved.strcolor
if pc_moved.strcolor == Board.attacker.strcolor:
pc_moved.isMoving = False
for plyr in Player.player_list:
if plyr.isMyTurn:
plyr.isMyTurn = False
else:
plyr.isMyTurn = True
# En Passant semi-detection
try:
if Board.attacker.position_numeric == enpassant_square and Board.attacker.ptype == "Pawn":
for enemy_pawn in Pieces.pieces:
if enemy_pawn.strcolor != Board.attacker.strcolor and enemy_pawn.ptype == "Pawn" and (enemy_pawn.position_numeric == [enpassant_square[0], enpassant_square[1]-1] or enemy_pawn.position_numeric == [enpassant_square[0], enpassant_square[1]+1]):
for sqr in Ui_MainWindow.boardSquare_list:
if [sqr.coordinate[2], sqr.coordinate[1]] == enemy_pawn.position_numeric:
sqr.squares.setIcon(QIcon(""))
sqr.piece_color = None
sqr.isOccupied = False
Pieces.pieces.remove(enemy_pawn)
del enemy_pawn
except:
pass
enpassant_square = []
for pc in Pieces.pieces:
try:
if pc.en_passantable == 1:
pc.reject_enpassant += 1
except:
pass
# Switching player turn (button-wise)
try:
for sqr in Ui_MainWindow.boardSquare_list:
if sqr.isOccupied and sqr.piece_color != Board.attacker.strcolor:
sqr.squares.setEnabled(True)
except:
pass
# Switching player turn (conventional-wise)
Player.launch_turn()
# Switching player turn part 2 (button-wise)
for sqr in Ui_MainWindow.boardSquare_list:
if sqr.isClicked:
sqr.isClicked = False
for sq in Ui_MainWindow.boardSquare_list:
try:
if not sq.isClicked and not sq.isOccupied and not ([sq.coordinate[2], sq.coordinate[1]] == prev_movement):
sq.squares.setIcon(QIcon(""))
# sq.squares.setStyleSheet(sq.info_padding)
for player in Player.player_list:
if player.isMyTurn:
for sqr in Ui_MainWindow.boardSquare_list:
if sqr.isOccupied and sqr.piece_color != player.color:
sqr.squares.setEnabled(False)
except:
pass
# Refreshing the previous piece touched.
Board.prev = Board.attacker
Board.attacker = None
self.isClicked = True
# Piece area covered logic.
for pc in Pieces.pieces:
if pc.ptype == 'King':
continue
else:
try:
pc.second_checker = False
pc.areaCovered()
except AttributeError:
pass
pc.tester2 += 1
for kg in Pieces.pieces:
if kg.ptype == 'King':
king_temp = kg
kg.areaCovered()
if kg.inCheck():
for pc in Pieces.pieces:
if pc.ptype == "Queen" or pc.ptype == "Rook" or pc.ptype == "Bishop":
pc.second_checker = True
pc.areaCovered()
else:
for pc in Pieces.pieces:
if pc.ptype == "Queen" or pc.ptype == "Rook" or pc.ptype == "Bishop":
pc.second_checker = False
# Logic to create pin and block check system.
# (by testing every possible scenario)
global king_clicked_mode
global checkmate_verificator
global temp_def
global test_sqr
for sqr in Ui_MainWindow.boardSquare_list:
if sqr.isClicked:
checkmate_verificator = False
temp_def = None
test_sqr = sqr
for plyr in Player.player_list:
if plyr.isMyTurn:
turn_color = plyr.color
for pc in Pieces.pieces:
if pc.ptype == "King":
king_clicked_mode = True # Switch to False to back to normal setting
continue
else:
king_clicked_mode = False
if pc.position_numeric == [sqr.coordinate[2], sqr.coordinate[1]] and pc.strcolor == turn_color:
pc.allowMovement = 0
current_pieces = pc
for kings in Pieces.pieces:
if kings.ptype == "King" and current_pieces.strcolor == kings.strcolor:
temp_king = kings
break
proto_ac = pc.area_covered
for area in proto_ac:
for sqr in Ui_MainWindow.boardSquare_list:
if [sqr.coordinate[2], sqr.coordinate[1]] == pc.position_numeric:
sqr.isOccupied = False
for sqr in Ui_MainWindow.boardSquare_list:
if sqr.isOccupied and sqr.piece_color != pc.strcolor and [sqr.coordinate[2], sqr.coordinate[1]] == area:
sqr.isOccupied = True
for s in Pieces.pieces:
if [sqr.coordinate[2], sqr.coordinate[1]] == s.position_numeric:
s.area_covered = []
temp_def = s
if [sqr.coordinate[2], sqr.coordinate[1]] == area and (not sqr.isOccupied):
sqr.isOccupied = True
sqr.piece_color = None
for x in Pieces.pieces:
if x.ptype == 'King' or x == temp_def:
continue
else:
if x == pc or x == temp_def:
continue
try:
if x.strcolor == pc.strcolor:
continue
x.second_checker = False
x.areaCovered()
except:
pass
x.tester2 += 1
for kg in Pieces.pieces:
if kg.ptype == 'King':
king_temp = kg
kg.areaCovered()
if kg.inCheck():
for a in Pieces.pieces:
if a == pc or a.strcolor == pc.strcolor or a == temp_def:
continue
if a.ptype == "Queen" or a.ptype == "Rook" or a.ptype == "Bishop":
a.second_checker = True
a.areaCovered()
else:
for a in Pieces.pieces:
if a == pc or a.strcolor == pc.strcolor or a == temp_def:
continue
if a.ptype == "Queen" or a.ptype == "Rook" or a.ptype == "Bishop":
a.second_checker = False
temp_area = area
if temp_king.inCheck():
new_ac = [a for a in pc.area_covered if not a == temp_area]
pc.area_covered = new_ac
else:
pc.allowMovement += 1
checkmate_verificator = True
temp_def = None
#######
for sqr in Ui_MainWindow.boardSquare_list:
try:
if [sqr.coordinate[2], sqr.coordinate[1]] == temp_area and (sqr.piece_color is None):
sqr.isOccupied = False
except:
pass
for sqr in Ui_MainWindow.boardSquare_list:
if [sqr.coordinate[2], sqr.coordinate[1]] == pc.position_numeric:
sqr.isOccupied = True
enable_sqr = pc
for x in Pieces.pieces:
if x.ptype == "King" or x == temp_def:
continue
try:
if x.strcolor == enable_sqr.strcolor:
continue
x.second_checker = False
x.areaCovered()
except:
pass
x.tester2 += 1
for kg in Pieces.pieces:
if kg.ptype == 'King':
king_temp = kg
kg.areaCovered()
if kg.inCheck():
for a in Pieces.pieces:
try:
if a.strcolor == enable_sqr.strcolor or a == temp_def:
continue
except:
pass
if a.ptype == "Queen" or a.ptype == "Rook" or a.ptype == "Bishop":
a.second_checker = True
a.areaCovered()
else:
for a in Pieces.pieces:
try:
if a.strcolor == enable_sqr.strcolor or a == temp_def:
continue
except:
pass
if a.ptype == "Queen" or a.ptype == "Rook" or a.ptype == "Bishop":
a.second_checker = False
# a.areaCovered()
else:
pc.allowMovement = 0
temp_def = None
# Part of piece protecting each other in case of King is clicked.
for sqr in Ui_MainWindow.boardSquare_list:
if sqr.isClicked:
for pc in Pieces.pieces:
if [sqr.coordinate[2], sqr.coordinate[1]] == pc.position_numeric:
for area in pc.area_covered:
for king_area in Ui_MainWindow.boardSquare_list:
if [king_area.coordinate[2], king_area.coordinate[1]] == area and king_area.isOccupied and king_area.piece_color == pc.strcolor:
pc.area_covered.remove(area)
# Blocking King vs King collision.
for sqr in Ui_MainWindow.boardSquare_list:
if sqr.isClicked:
for pc in Pieces.pieces:
if [sqr.coordinate[2], sqr.coordinate[1]] == pc.position_numeric and pc.ptype == "King":