-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
1439 lines (1250 loc) · 54.9 KB
/
GUI.py
File metadata and controls
1439 lines (1250 loc) · 54.9 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
import os
import queue
import random
import sys
import threading
import time
import multiprocessing as mp
from enum import Enum, auto
import datetime
from tkinter import filedialog, Tk
import msgpack
import pygame
import numpy as np
import chess
import chess.pgn
from Board import Board, is_square_attacked, parse_fen
from Board_Move_gen import (
Move,
Move_generator,
get_flag,
get_start_square,
get_target_square,
move_to_uci,
unmove,
)
from Constants import Color, Flag, Pieces
from Minimax import AI, SingleSearch
from pregen.Utilities import get_lsb1_index
try:
with open(r"Process Opening\opening_tree.msgpack", "rb") as f:
OPENING_TREE = msgpack.unpack(f, raw=False)
OPENING_BOOK_LOADED = True
except:
OPENING_TREE = {}
OPENING_BOOK_LOADED = False
def board_to_state(board):
return (
board.bitboard.copy(),
board.occupancy.copy(),
np.uint8(board.side),
np.uint8(board.castle),
np.uint8(board.enpassant),
np.uint8(board.halfmove),
)
def state_to_board(state):
p, o, s, c, e, pl = state
return Board(p, o, s, c, e, pl)
def engine_loop(cmd_q, info_q, result_q):
p, o, s, c, e, pl = parse_fen(STARTING_FEN)
warm_board = Board(p, o, s, c, e, pl)
Move_generator(warm_board)
is_square_attacked(warm_board, 0, 0)
SingleSearch(warm_board, depth=1).search()
while True:
cmd = cmd_q.get()
if not cmd:
continue
if cmd[0] == "stop":
break
if cmd[0] == "search":
_, state, depth, delay, sid = cmd
if delay:
time.sleep(delay)
board = state_to_board(state)
def info_cb(msg):
info_q.put(msg)
searcher = SingleSearch(board, depth=depth, info_callback=info_cb)
best_move = searcher.search()
result_q.put(("engine", int(best_move), sid))
def analysis_worker(state, info_q):
board = state_to_board(state)
def info_cb(msg):
info_q.put(msg)
searcher = SingleSearch(board, depth=24, info_callback=info_cb)
searcher.search()
class FENS:
START = b"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
STARTING_FEN = FENS.START
class GameMode(Enum):
MENU = auto()
PVP = auto()
P_VS_AI = auto()
AI_VS_AI = auto()
ANALYSIS = auto()
class GameState(Enum):
PLAYING = auto()
CHECKMATE = auto()
STALEMATE = auto()
DRAW = auto()
class MoveClass(Enum):
BEST = ("Best", (39, 174, 96))
EXCELLENT = ("Excellent", (46, 204, 113))
GOOD = ("Good", (52, 152, 219))
INACCURACY = ("Inaccuracy", (241, 196, 15))
MISTAKE = ("Mistake", (230, 126, 34))
BLUNDER = ("Blunder", (231, 76, 60))
BOOK = ("Book", (155, 89, 182))
class OpeningNavigator:
def __init__(self, tree):
self.tree = tree
self.reset()
def reset(self):
self.current = self.tree
self.history = []
self.active = True
def apply(self, uci_move: str) -> bool:
if not self.active:
return False
if uci_move in self.current:
self.current = self.current[uci_move]
self.history.append(uci_move)
return True
else:
self.active = False
return False
def get_options(self) -> list:
if not self.active or not isinstance(self.current, dict):
return []
return list(self.current.keys())
def select_random(self) -> str | None:
opts = self.get_options()
return random.choice(opts) if opts else None
class PieceCache:
def __init__(self, size: int = 80):
self.size = size
self.cache = {}
self.names = ["pawn", "knight", "bishop", "rook", "queen", "king"] * 2
self.colors = ["white"] * 6 + ["black"] * 6
def get(self, idx: int) -> pygame.Surface:
key = f"{self.colors[idx]}_{self.names[idx]}"
if key not in self.cache:
path = f"images/{key}.png"
if os.path.exists(path):
try:
img = pygame.image.load(path)
self.cache[key] = pygame.transform.smoothscale(
img, (self.size, self.size)
)
except:
self.cache[key] = self._fallback(self.names[idx], self.colors[idx])
else:
self.cache[key] = self._fallback(self.names[idx], self.colors[idx])
return self.cache[key]
def _fallback(self, name, color):
s = pygame.Surface((self.size, self.size), pygame.SRCALPHA)
font = pygame.font.SysFont("Arial", self.size // 2, bold=True)
fg = (255, 255, 255) if color == "white" else (0, 0, 0)
bg = (220, 220, 220) if color == "white" else (60, 60, 60)
border = (0, 0, 0) if color == "white" else (255, 255, 255)
pygame.draw.circle(s, bg, (self.size // 2, self.size // 2), self.size // 2 - 5)
pygame.draw.circle(s, border, (self.size // 2, self.size // 2), self.size // 2 - 5, 2)
txt = font.render(name[0].upper(), True, fg)
s.blit(txt, (self.size // 2 - txt.get_width() // 2, self.size // 2 - txt.get_height() // 2))
return s
class UIComponent:
def __init__(self, rect):
self.rect = rect
def draw(self, surf): pass
def handle(self, event): return False
class Button(UIComponent):
def __init__(self, rect, text, callback, color_theme, active=False, font_key="normal"):
super().__init__(rect)
self.text = text
self.callback = callback
self.theme = color_theme
self.active = active
self.hover = False
self.font_key = font_key
self.hover_t = 0.0
def draw(self, surf, fonts):
target = 1.0 if (self.active or self.hover) else 0.0
self.hover_t += (target - self.hover_t) * 0.2
col = tuple(int(self.theme["btn"][i] + (self.theme["accent_hover"][i] - self.theme["btn"][i]) * self.hover_t) for i in range(3))
shadow = pygame.Rect(self.rect.x, self.rect.y + int(3 - 2 * self.hover_t), self.rect.w, self.rect.h)
pygame.draw.rect(surf, (12, 14, 18), shadow, border_radius=8)
pygame.draw.rect(surf, col, self.rect, border_radius=8)
if self.hover or self.active:
pygame.draw.rect(surf, self.theme["accent"], self.rect, 2, border_radius=8)
t = fonts[self.font_key].render(self.text, True, self.theme["text"])
surf.blit(t, (self.rect.centerx - t.get_width() // 2, self.rect.centery - t.get_height() // 2))
def handle(self, event):
if event.type == pygame.MOUSEMOTION:
self.hover = self.rect.collidepoint(event.pos)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.rect.collidepoint(event.pos):
if self.callback: self.callback()
return True
return False
class Slider(UIComponent):
def __init__(self, x, y, w, h, min_v, max_v, init, label, theme):
super().__init__(pygame.Rect(x, y, w, h))
self.min, self.max, self.val = min_v, max_v, init
self.label = label
self.theme = theme
self.drag = False
self.radius = h // 2 + 4
def handle(self, ev):
if ev.type == pygame.MOUSEBUTTONDOWN:
mx, my = ev.pos
hx = self.rect.x + ((self.val - self.min) / (self.max - self.min)) * self.rect.width
if ((mx - hx) ** 2 + (my - self.rect.centery) ** 2) <= self.radius**2:
self.drag = True
return True
elif ev.type == pygame.MOUSEBUTTONUP:
self.drag = False
elif ev.type == pygame.MOUSEMOTION and self.drag:
ratio = max(0, min(1, (ev.pos[0] - self.rect.x) / self.rect.width))
self.val = int(self.min + ratio * (self.max - self.min))
return True
return False
def draw(self, surf, fonts):
track = pygame.Rect(self.rect.x, self.rect.centery - 3, self.rect.width, 6)
pygame.draw.rect(surf, (60, 60, 60), track, border_radius=3)
fill_w = ((self.val - self.min) / (self.max - self.min)) * self.rect.width
fill = pygame.Rect(self.rect.x, self.rect.centery - 3, fill_w, 6)
pygame.draw.rect(surf, self.theme["accent"], fill, border_radius=3)
hx = self.rect.x + fill_w
pygame.draw.circle(surf, (40, 40, 40), (hx, self.rect.centery + 2), self.radius)
pygame.draw.circle(surf, self.theme["accent"] if self.drag else (220, 220, 220), (hx, self.rect.centery), self.radius)
lbl = fonts["small"].render(f"{self.label}: {self.val}", True, self.theme["text"])
surf.blit(lbl, (self.rect.x, self.rect.y - 25))
class GameAnalysis:
"""Performs full game analysis"""
def __init__(self, pgn_game, engine_depth, callback):
self.game = pgn_game
self.depth = engine_depth
self.callback = callback
self.stop_flag = False
def run(self):
board = self.game.board()
analysis = []
self._analyze_pos(board, 0, analysis)
node = self.game
ply = 0
while node.next() and not self.stop_flag:
node = node.next()
ply += 1
move = node.move
board.push(move)
self._analyze_pos(board, ply, analysis)
self.callback(analysis)
def _analyze_pos(self, chess_board, ply, analysis_list):
fen = chess_board.fen()
p, o, s, c, e, pl = parse_fen(bytes(fen, "utf-8"))
b = Board(p, o, s, c, e, pl)
searcher = SingleSearch(b, depth=self.depth)
best_move = searcher.search()
pass
class ChessGUI:
DARK_THEME = {
"light_sq": (228, 230, 236),
"dark_sq": (118, 140, 176),
"bg": (16, 18, 23),
"panel": (26, 30, 38),
"selected": (186, 202, 68, 160),
"last_move": (255, 226, 130, 120),
"hover": (120, 176, 255, 90),
"mark": (255, 200, 80, 110),
"arrow": (120, 168, 255, 200),
"text": (240, 244, 250),
"text_muted": (170, 178, 190),
"btn": (36, 40, 50),
"btn_hover": (52, 58, 72),
"accent": (94, 132, 255),
"accent_hover": (122, 160, 255),
"overlay": (0, 0, 0, 180),
"check": (200, 60, 60, 180),
"book": (76, 175, 80),
"engine": (255, 170, 64),
"eval_border": (74, 86, 102),
"eval_white": (245, 247, 250),
"eval_black": (18, 20, 24),
}
LIGHT_THEME = {
"light_sq": (248, 249, 252),
"dark_sq": (176, 196, 220),
"bg": (236, 238, 242),
"panel": (250, 252, 255),
"selected": (180, 205, 90, 150),
"last_move": (255, 226, 150, 140),
"hover": (100, 160, 240, 80),
"mark": (255, 188, 80, 110),
"arrow": (86, 130, 210, 190),
"text": (26, 30, 36),
"text_muted": (90, 96, 108),
"btn": (228, 232, 238),
"btn_hover": (210, 216, 226),
"accent": (72, 110, 200),
"accent_hover": (92, 130, 220),
"overlay": (255, 255, 255, 180),
"check": (220, 70, 70, 160),
"book": (56, 145, 80),
"engine": (210, 120, 0),
"eval_border": (150, 160, 170),
"eval_white": (250, 250, 250),
"eval_black": (50, 54, 60),
}
THEMES = {"dark": DARK_THEME, "light": LIGHT_THEME}
def __init__(self, w=1600, h=1000):
pygame.init()
self.disp = pygame.display.set_mode((w, h), pygame.HWSURFACE | pygame.DOUBLEBUF)
pygame.display.set_caption("Chess Engine - Pro Interface")
self.w, self.h = w, h
self.clock = pygame.time.Clock()
self.theme_name = "dark"
self.C = self.THEMES[self.theme_name]
self.fonts = {
"header": pygame.font.SysFont("Segoe UI", 80, bold=True),
"subhead": pygame.font.SysFont("Segoe UI", 50, bold=True),
"title": pygame.font.SysFont("Segoe UI", 32, bold=True),
"normal": pygame.font.SysFont("Segoe UI", 20),
"small": pygame.font.SysFont("Segoe UI", 16),
"tiny": pygame.font.SysFont("Segoe UI", 14),
"coord": pygame.font.SysFont("Segoe UI", 14, bold=True),
"mono": pygame.font.SysFont("Consolas", 14),
}
self._compute_layout()
self.pieces = PieceCache(self.sqsize)
self.openings = OpeningNavigator(OPENING_TREE)
self.mode = GameMode.MENU
self.board = None
self.sel = None
self.valids = []
self.log = []
self.states = []
self.lastmv = None
self.gstate = GameState.PLAYING
self.winner = ""
self.reason = ""
self.flipped = False
self.aiqueue = mp.Queue()
self.infoqueue = mp.Queue()
self.engine_cmd_q = mp.Queue()
self.thinking = False
self.ai_thread = None
self.ai_process = None
self.analysis_process = None
self.analysis_thread = None
self.analysis_stop = None
self.analysis_info = {"depth": 0, "score": "0.00", "pv": "-", "pv_uci": "", "nps": 0}
self.search_id = 0
self.pending_search_id = None
self.move_classifications = []
self.analysis_scores = []
self.pgn_headers = {}
self.depth = 6
self.pcolor = Color.WHITE
self.ai_auto = False
self.ai_manual_trigger = False
self.ai_delay = 0.5
self.arrows = []
self.arrow_start = None
self.marked = {}
self.hover_sq = None
self.show_eval_bar = True
self.show_debug = False
self.color_picker_active = False
self.color_buttons = []
self.pending_pcolor = None
self.panel_reserved_h = 140
self.menu_buttons = []
self.menu_slider = None
self.panel_buttons = []
self.analysis_buttons = []
self._warmup()
self._start_engine_process()
self._init_menu()
def _compute_layout(self):
left = 48
right = 40
top = 60
bottom = 60
eval_w = 34
gap_be = 16
gap_ep = 18
panel_min = 360
max_board_w = self.w - left - right - eval_w - gap_be - gap_ep - panel_min
max_board_h = self.h - top - bottom
bsize = min(max_board_w, max_board_h)
if bsize < 320:
bsize = max(260, min(max_board_w, max_board_h))
panel_w = self.w - left - right - eval_w - gap_be - gap_ep - bsize
if panel_w < panel_min:
bsize = max(260, self.w - left - right - eval_w - gap_be - gap_ep - panel_min)
panel_w = self.w - left - right - eval_w - gap_be - gap_ep - bsize
self.bsize = bsize
self.sqsize = self.bsize // 8
self.offx = left
self.offy = (self.h - self.bsize) // 2
self.eval_w = eval_w
self.eval_x = self.offx + self.bsize + gap_be
self.eval_y = self.offy + 12
self.panel_x = self.eval_x + self.eval_w + gap_ep
self.panel_w = panel_w
self.panel_y = self.offy
self.panel_h = self.bsize
def _warmup(self):
self.disp.fill(self.C["bg"])
t = self.fonts["title"].render("Initializing Engine...", True, self.C["accent"])
self.disp.blit(t, (self.w//2 - t.get_width()//2, self.h//2))
pygame.display.flip()
p, o, s, c, e, pl = parse_fen(STARTING_FEN)
b = Board(p, o, s, c, e, pl)
Move_generator(b)
is_square_attacked(b, 0, 0)
AI(b, s, 1)
def _stop_ai_process(self):
if self.ai_process is not None:
try:
self.engine_cmd_q.put(("stop",))
except:
pass
self.ai_process.join(timeout=0.2)
if self.ai_process.is_alive():
self.ai_process.terminate()
self.ai_process.join(timeout=0.2)
self.ai_process = None
self.pending_search_id = None
def _start_engine_process(self):
if self.ai_process is not None and self.ai_process.is_alive():
return
ctx = mp.get_context("spawn")
self.ai_process = ctx.Process(
target=engine_loop,
args=(self.engine_cmd_q, self.infoqueue, self.aiqueue),
daemon=True,
)
self.ai_process.start()
def _stop_analysis_process(self):
if self.analysis_process is not None:
if self.analysis_process.is_alive():
self.analysis_process.terminate()
self.analysis_process.join(timeout=0.2)
self.analysis_process = None
def _init_menu(self):
cx, cy = self.w // 2, self.h // 2
bw, bh = 420, 76
gap = 22
slider_h = 30
total_h = 4 * bh + 3 * gap + slider_h + 40
start_y = max(140, (self.h - total_h) // 2 + 40)
self.menu_buttons = [
Button(pygame.Rect(cx - bw//2, start_y, bw, bh), "Player vs Player", lambda: self.set_mode(GameMode.PVP), self.C),
Button(pygame.Rect(cx - bw//2, start_y + (bh + gap), bw, bh), "Player vs AI", self.open_color_picker, self.C),
Button(pygame.Rect(cx - bw//2, start_y + 2*(bh + gap), bw, bh), "AI vs AI", lambda: self.set_mode(GameMode.AI_VS_AI), self.C),
Button(pygame.Rect(cx - bw//2, start_y + 3*(bh + gap), bw, bh), "Analysis Board", lambda: self.set_mode(GameMode.ANALYSIS), self.C),
]
slider_y = start_y + 4 * (bh + gap) + 20
self.menu_slider = Slider(cx - bw//2, slider_y, bw, slider_h, 1, 12, self.depth, "Engine Depth", self.C)
self.color_buttons = []
self.color_picker_active = False
def set_mode(self, mode):
self.mode = mode
self.depth = self.menu_slider.val
if mode == GameMode.P_VS_AI and self.pending_pcolor is not None:
self.pcolor = self.pending_pcolor
self.pending_pcolor = None
else:
self.pcolor = Color.WHITE
self.flipped = False
self.init_game()
def init_game(self):
p, o, s, c, e, pl = parse_fen(STARTING_FEN)
self.board = Board(p, o, s, c, e, pl)
self.openings.reset()
self.sel, self.valids = None, []
self.log, self.states, self.lastmv = [], [], None
self.thinking = False
self.gstate = GameState.PLAYING
self.analysis_info = {"depth": 0, "score": "0.00", "pv": "-", "pv_uci": "", "nps": 0}
self.move_classifications = []
self.pgn_headers = {"Event": "Engine Game", "Site": "Local", "Date": datetime.date.today().strftime("%Y.%m.%d")}
self.ai_auto = False
self.ai_manual_trigger = False
self.arrows = []
self.arrow_start = None
self.marked = {}
self.hover_sq = None
self._stop_analysis_process()
if self.analysis_stop: self.analysis_stop[0] = 1
try:
while True:
self.aiqueue.get_nowait()
except:
pass
try:
while True:
self.infoqueue.get_nowait()
except:
pass
self.pending_search_id = None
self._build_panel_buttons()
if self.mode == GameMode.ANALYSIS:
self.start_analysis()
elif self.mode == GameMode.AI_VS_AI:
self.start_ai()
def toggle_auto(self):
self.ai_auto = not self.ai_auto
for btn in self.panel_buttons:
if "Auto:" in btn.text:
btn.text = f"Auto: {'On' if self.ai_auto else 'Off'}"
btn.active = self.ai_auto
if self.ai_auto:
self.start_ai()
def trigger_manual(self):
self.ai_manual_trigger = True
self.start_ai()
def toggle_theme(self):
self._apply_theme("light" if self.theme_name == "dark" else "dark")
def toggle_eval_bar(self):
self.show_eval_bar = not self.show_eval_bar
for btn in self.panel_buttons:
if btn.text.startswith("Eval:"):
btn.text = f"Eval: {'On' if self.show_eval_bar else 'Off'}"
btn.active = self.show_eval_bar
def toggle_debug(self):
self.show_debug = not self.show_debug
for btn in self.panel_buttons:
if btn.text.startswith("Debug:"):
btn.text = f"Debug: {'On' if self.show_debug else 'Off'}"
btn.active = self.show_debug
def clear_marks(self):
self.arrows = []
self.marked = {}
def open_color_picker(self):
self.color_picker_active = True
bw, bh = 320, 68
gap = 20
cx, cy = self.w // 2, self.h // 2
total_h = 4 * bh + 3 * gap
start_y = cy - total_h // 2
self.color_buttons = [
Button(pygame.Rect(cx - bw//2, start_y, bw, bh), "Play White", lambda: self._choose_color(Color.WHITE), self.C),
Button(pygame.Rect(cx - bw//2, start_y + (bh + gap), bw, bh), "Play Black", lambda: self._choose_color(Color.BLACK), self.C),
Button(pygame.Rect(cx - bw//2, start_y + 2*(bh + gap), bw, bh), "Random Color", self._choose_random_color, self.C),
Button(pygame.Rect(cx - bw//2, start_y + 3*(bh + gap), bw, bh), "Back", self._close_color_picker, self.C),
]
def _choose_random_color(self):
self._choose_color(Color.WHITE if random.randint(0, 1) == 0 else Color.BLACK)
def _choose_color(self, color):
self.pending_pcolor = color
self.color_picker_active = False
self.color_buttons = []
self.set_mode(GameMode.P_VS_AI)
def _close_color_picker(self):
self.color_picker_active = False
self.color_buttons = []
def load_pgn(self):
root = Tk()
root.withdraw()
file_path = filedialog.askopenfilename(filetypes=[("PGN Files", "*.pgn")])
root.destroy()
if not file_path: return
try:
with open(file_path) as f:
game = chess.pgn.read_game(f)
if not game: return
self.init_game()
self.pgn_headers = dict(game.headers)
board = game.board()
for move in game.mainline_moves():
uci = move.uci()
mvs = Move_generator(self.board)
found = False
for i in range(mvs.counter):
m = mvs.moves[i]
if move_to_uci(m) == uci:
self.push(m)
found = True
break
if not found:
print(f"Error parsing PGN move: {uci}")
break
except Exception as e:
print(f"PGN Load Error: {e}")
def save_pgn(self):
game = chess.pgn.Game()
game.headers.update(self.pgn_headers)
game.headers["White"] = "Engine" if self.pcolor == Color.BLACK else "Player"
game.headers["Black"] = "Engine" if self.pcolor == Color.WHITE else "Player"
node = game
for uci in self.log:
move = chess.Move.from_uci(uci)
node = node.add_variation(move)
filename = f"game_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pgn"
with open(filename, "w") as f:
print(game, file=f, end="\n\n")
print(f"Saved to {filename}")
def run_full_analysis(self):
threading.Thread(target=self._full_analysis_worker, daemon=True).start()
def _full_analysis_worker(self):
pass
def info_callback(self, msg):
self.infoqueue.put(msg)
def start_ai(self):
if self.thinking or self.gstate != GameState.PLAYING:
return
if self.mode == GameMode.AI_VS_AI and not self.ai_auto and not self.ai_manual_trigger:
return
self.thinking = True
self.ai_manual_trigger = False
book_move = None
if self.openings.active:
book_uci = self.openings.select_random()
if book_uci:
mvs = Move_generator(self.board)
for i in range(mvs.counter):
m = mvs.moves[i]
if move_to_uci(m) == book_uci:
book_move = m
break
self.search_id += 1
self.pending_search_id = self.search_id
if book_move:
self.aiqueue.put(("book", book_move, self.pending_search_id))
return
state = board_to_state(self.board)
delay = self.ai_delay if self.mode == GameMode.AI_VS_AI else 0.0
self._start_engine_process()
self.engine_cmd_q.put(("search", state, int(self.depth), delay, self.pending_search_id))
def start_analysis(self):
self._stop_analysis_process()
state = board_to_state(self.board)
ctx = mp.get_context("spawn")
self.analysis_process = ctx.Process(
target=analysis_worker,
args=(state, self.infoqueue),
daemon=True,
)
self.analysis_process.start()
def parse_info(self):
while True:
try:
msg = self.infoqueue.get_nowait()
except:
break
if msg.startswith("info depth"):
parts = msg.split()
try:
d_idx = parts.index("depth")
s_idx = parts.index("score")
pv_idx = parts.index("pv")
self.analysis_info["depth"] = parts[d_idx + 1]
score_type = parts[s_idx + 1]
val = int(parts[s_idx + 2])
if score_type == "cp":
self.analysis_info["score"] = f"{val/100:.2f}"
else:
self.analysis_info["score"] = f"M{val}"
pv_moves = parts[pv_idx + 1:pv_idx + 6]
self.analysis_info["pv"] = " ".join(pv_moves)
self.analysis_info["pv_uci"] = pv_moves[0] if pv_moves else ""
except: pass
elif msg.startswith("info stats"):
parts = msg.split()
try:
nps_idx = parts.index("nps")
self.analysis_info["nps"] = parts[nps_idx + 1]
except: pass
def push(self, mv):
self.states.append({
"board": self.board.copy(),
"lastmv": self.lastmv,
"log": self.log[:],
"gstate": self.gstate,
"opnav": self._copy_opnav(),
"classifications": self.move_classifications[:]
})
if self.openings.active:
self.move_classifications.append(MoveClass.BOOK)
else:
self.move_classifications.append(MoveClass.GOOD)
if Move(self.board, mv):
uci = move_to_uci(mv)
self.lastmv = (get_start_square(mv), get_target_square(mv))
self.log.append(uci)
self.openings.apply(uci)
self.sel, self.valids = None, []
self.check_end()
if self.mode == GameMode.ANALYSIS:
self.start_analysis()
return True
return False
def undo(self):
if not self.states: return
st = self.states.pop()
self.board, self.lastmv, self.log = st["board"], st["lastmv"], st["log"]
self.gstate, self.openings = st["gstate"], st["opnav"]
self.move_classifications = st["classifications"]
self.sel, self.valids, self.thinking = None, [], False
if self.mode == GameMode.ANALYSIS:
self.start_analysis()
elif self.mode == GameMode.P_VS_AI and self.board.side != self.pcolor:
self.undo()
def _copy_opnav(self):
nav = OpeningNavigator(OPENING_TREE)
nav.current, nav.history, nav.active = self.openings.current, self.openings.history[:], self.openings.active
return nav
def check_end(self):
mvs = Move_generator(self.board)
has_legal = False
pc, pe, pp = self.board.castle, self.board.enpassant, self.board.halfmove
for i in range(mvs.counter):
if Move(self.board, mvs.moves[i]):
has_legal = True
unmove(self.board, mvs.moves[i], pc, pe, pp)
break
if not has_legal:
kp = Pieces.K.value if self.board.side == Color.WHITE else Pieces.k.value
ksq = get_lsb1_index(self.board.bitboard[kp])
att = Color.BLACK if self.board.side == Color.WHITE else Color.WHITE
if is_square_attacked(self.board, ksq, att):
self.gstate = GameState.CHECKMATE
self.winner = "Black Wins" if self.board.side == Color.WHITE else "White Wins"
self.reason = "Checkmate"
else:
self.gstate = GameState.STALEMATE
self.winner, self.reason = "Draw", "Stalemate"
elif self.board.halfmove >= 100:
self.gstate = GameState.DRAW
self.winner, self.reason = "Draw", "50-Move Rule"
if self.gstate != GameState.PLAYING and self.analysis_stop:
self.analysis_stop[0] = 1
def draw_board(self):
frame = pygame.Rect(self.offx - 15, self.offy - 15, self.bsize + 30, self.bsize + 30)
shadow = pygame.Surface((frame.w + 12, frame.h + 12), pygame.SRCALPHA)
pygame.draw.rect(shadow, (0, 0, 0, 180), shadow.get_rect(), border_radius=16)
self.disp.blit(shadow, (frame.x + 8, frame.y + 10))
pygame.draw.rect(self.disp, self.C["panel"], frame, border_radius=16)
pygame.draw.rect(self.disp, self.C["accent"], frame, 3, border_radius=16)
for r in range(8):
for f in range(8):
ar, af = (7 - r, 7 - f) if self.flipped else (r, f)
col = self.C["light_sq"] if (ar + af) % 2 == 0 else self.C["dark_sq"]
x, y = self.offx + f * self.sqsize, self.offy + r * self.sqsize
pygame.draw.rect(self.disp, col, (x, y, self.sqsize, self.sqsize))
if f == 0:
ccol = self.C["dark_sq"] if col == self.C["light_sq"] else self.C["light_sq"]
t = self.fonts["coord"].render(str(8 - ar), True, ccol)
self.disp.blit(t, (x + 3, y + 3))
if r == 7:
ccol = self.C["dark_sq"] if col == self.C["light_sq"] else self.C["light_sq"]
t = self.fonts["coord"].render(chr(97 + af), True, ccol)
self.disp.blit(t, (x + self.sqsize - 12, y + self.sqsize - 18))
if self.lastmv:
s, e = self.lastmv
self._highlight_sq(s, self.C["last_move"])
self._highlight_sq(e, self.C["last_move"])
if self.sel is not None:
self._highlight_sq(self.sel, self.C["selected"])
if self.marked:
for sq in self.marked:
self._highlight_sq(sq, self.C["mark"])
if self.hover_sq is not None:
self._highlight_sq(self.hover_sq, self.C["hover"])
kp = Pieces.K.value if self.board.side == Color.WHITE else Pieces.k.value
ksq = get_lsb1_index(self.board.bitboard[kp])
att = Color.BLACK if self.board.side == Color.WHITE else Color.WHITE
if is_square_attacked(self.board, ksq, att):
self._highlight_sq(ksq, self.C["check"])
def _highlight_sq(self, sq, col):
r, f = divmod(sq, 8)
if self.flipped: r, f = 7 - r, 7 - f
x, y = self.offx + f * self.sqsize, self.offy + r * self.sqsize
s = pygame.Surface((self.sqsize, self.sqsize), pygame.SRCALPHA)
s.fill(col)
self.disp.blit(s, (x, y))
def draw_pieces(self):
for i in range(12):
bb = self.board.bitboard[i]
while bb:
sq = get_lsb1_index(bb)
r, f = divmod(sq, 8)
if self.flipped: r, f = 7 - r, 7 - f
x = self.offx + f * self.sqsize + self.sqsize // 2
y = self.offy + r * self.sqsize + self.sqsize // 2
img = self.pieces.get(i)
self.disp.blit(img, img.get_rect(center=(x, y)))
bb &= bb - 1
def draw_arrows(self):
for s, e, col in self.arrows:
self._draw_arrow(s, e, col)
pv = self.analysis_info.get("pv_uci", "")
if pv and self.show_eval_bar:
start = self._coord_to_sq(pv[:2])
end = self._coord_to_sq(pv[2:4])
if start is not None and end is not None:
self._draw_arrow(start, end, self.C["arrow"])
def _draw_arrow(self, s_sq, e_sq, col):
if s_sq == e_sq:
return
sx, sy = self._sq_center(s_sq)
ex, ey = self._sq_center(e_sq)
dx, dy = ex - sx, ey - sy
dist = max(1, (dx * dx + dy * dy) ** 0.5)
ux, uy = dx / dist, dy / dist
head_len = self.sqsize * 0.35
head_w = self.sqsize * 0.18
bx, by = ex - ux * head_len, ey - uy * head_len
px, py = -uy * head_w, ux * head_w
arrow_surf = pygame.Surface((self.w, self.h), pygame.SRCALPHA)
pygame.draw.line(arrow_surf, col, (sx, sy), (bx, by), 6)
pygame.draw.polygon(arrow_surf, col, [(ex, ey), (bx + px, by + py), (bx - px, by - py)])
self.disp.blit(arrow_surf, (0, 0))
def _sq_center(self, sq):
r, f = divmod(sq, 8)
if self.flipped: r, f = 7 - r, 7 - f
x = self.offx + f * self.sqsize + self.sqsize // 2
y = self.offy + r * self.sqsize + self.sqsize // 2
return x, y
def _coord_to_sq(self, coord):
if len(coord) < 2:
return None
f = ord(coord[0]) - 97
r = int(coord[1]) - 1
if f < 0 or f > 7 or r < 0 or r > 7:
return None
return (7 - r) * 8 + f
def draw_moves(self):
for mv in self.valids:
tgt = get_target_square(mv)
r, f = divmod(tgt, 8)
if self.flipped: r, f = 7 - r, 7 - f
is_cap = any((self.board.bitboard[i] >> tgt) & 1 for i in range(12))
s = pygame.Surface((self.sqsize, self.sqsize), pygame.SRCALPHA)
if is_cap:
pygame.draw.circle(s, (0, 0, 0, 60), (self.sqsize//2, self.sqsize//2), self.sqsize//2 - 2, 6)
else:
pygame.draw.circle(s, (0, 0, 0, 40), (self.sqsize//2, self.sqsize//2), self.sqsize//6)
self.disp.blit(s, (self.offx + f * self.sqsize, self.offy + r * self.sqsize))
def draw_panel(self):
px = self.panel_x
w = self.panel_w
y = self.panel_y
panel_rect = pygame.Rect(px - 16, self.offy - 16, w + 32, self.bsize + 32)
panel_shadow = pygame.Surface((panel_rect.w + 10, panel_rect.h + 10), pygame.SRCALPHA)
pygame.draw.rect(panel_shadow, (0, 0, 0, 120), panel_shadow.get_rect(), border_radius=18)
self.disp.blit(panel_shadow, (panel_rect.x + 5, panel_rect.y + 7))
pygame.draw.rect(self.disp, self.C["panel"], panel_rect, border_radius=18)
pygame.draw.rect(self.disp, self.C["accent"], panel_rect, 2, border_radius=18)
status = "White to Move" if self.board.side == Color.WHITE else "Black to Move"
if self.gstate != GameState.PLAYING:
status = self.winner
col = self.C["accent"]