-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLogic.py
More file actions
882 lines (748 loc) · 31.8 KB
/
Copy pathGameLogic.py
File metadata and controls
882 lines (748 loc) · 31.8 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
# GameLogic.py (v62 - Further optimisations)
# -----------------------------------------------------------------------
# Global constants
# -----------------------------------------------------------------------
ROWS, COLS = 8, 8
SQUARE_SIZE = 75
BOARD_COLOR_1 = "#D2B48C"
BOARD_COLOR_2 = "#8B5A2B"
DIRECTIONS = {
'king': ((-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)),
'queen': ((-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)),
'rook': ((0, 1), (0, -1), (1, 0), (-1, 0)),
'bishop': ((-1, -1), (-1, 1), (1, -1), (1, 1)),
'knight': ((2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2)),
}
ADJACENT_DIRS = DIRECTIONS['king']
BISHOP_ZIGZAG_DIRS = (
((-1, 1), (-1, -1)), ((-1, -1), (-1, 1)),
((1, 1), (1, -1)), ((1, -1), (1, 1)),
((-1, 1), (1, 1)), ((1, 1), (-1, 1)),
((-1, -1), (1, -1)), ((1, -1), (-1, -1)),
)
# Stored as tuples so CPython uses the faster tuple-iteration C path.
KNIGHT_ATTACKS_FROM = {
(r, c): tuple(
(r + dr, c + dc) for dr, dc in DIRECTIONS['knight']
if 0 <= r + dr < ROWS and 0 <= c + dc < COLS
)
for r in range(ROWS) for c in range(COLS)
}
ADJACENT_SQUARES_MAP = {
(r, c): tuple(
(r + dr, c + dc) for dr, dc in ADJACENT_DIRS
if 0 <= r + dr < ROWS and 0 <= c + dc < COLS
)
for r in range(ROWS) for c in range(COLS)
}
# RAYS[sq_index][direction_index] — inner sequences are tuples (read-only)
RAYS = [[None] * 8 for _ in range(64)]
BISHOP_ZIGZAG_RAYS = [None] * 64
def _init_rays():
dy_dx = [(-1, 0), (1, 0), (0, 1), (0, -1), (-1, 1), (-1, -1), (1, 1), (1, -1)]
tmp = [[[] for _ in range(8)] for _ in range(64)]
zigzag_tmp = [[[] for _ in range(len(BISHOP_ZIGZAG_DIRS))] for _ in range(64)]
for r in range(ROWS):
for c in range(COLS):
idx = r * COLS + c
for i, (dr, dc) in enumerate(dy_dx):
cr, cc = r + dr, c + dc
while 0 <= cr < ROWS and 0 <= cc < COLS:
tmp[idx][i].append((cr, cc))
cr += dr
cc += dc
for i, (d1, d2) in enumerate(BISHOP_ZIGZAG_DIRS):
cr, cc, cd = r, c, d1
while True:
cr += cd[0]
cc += cd[1]
if not (0 <= cr < ROWS and 0 <= cc < COLS):
break
zigzag_tmp[idx][i].append((cr, cc))
cd = d2 if cd == d1 else d1
for sq in range(64):
RAYS[sq] = tuple(tuple(ray) for ray in tmp[sq])
BISHOP_ZIGZAG_RAYS[sq] = tuple(tuple(ray) for ray in zigzag_tmp[sq])
_init_rays()
def _clone_piece_fast(piece):
cls = piece.__class__
new_piece = cls.__new__(cls)
new_piece.color = piece.color
new_piece.opponent_color = piece.opponent_color
new_piece.pos = piece.pos
new_piece._list_pos = piece._list_pos # preserve index in new board's list
if cls is Pawn:
new_piece.direction = piece.direction
new_piece.starting_row = piece.starting_row
new_piece.promo_rank = piece.promo_rank
return new_piece
# -----------------------------------------------------------------------
# Piece classes
# -----------------------------------------------------------------------
class Piece:
def __init__(self, color):
self.color = color
self.opponent_color = "black" if color == "white" else "white"
self.pos = None
self._list_pos = -1 # index in Board.white_pieces / black_pieces
def clone(self):
new_piece = self.__class__(self.color)
new_piece.pos = self.pos
return new_piece
def symbol(self): return "?"
def get_valid_moves(self, board, pos): return []
class King(Piece):
z_idx = 5
def symbol(self): return "♔" if self.color == "white" else "♚"
def get_valid_moves(self, board, pos):
moves = []
r_start, c_start = pos
opp = self.opponent_color
grid = board.grid
for dr, dc in DIRECTIONS['king']:
r1, c1 = r_start + dr, c_start + dc
if 0 <= r1 < ROWS and 0 <= c1 < COLS:
target = grid[r1][c1]
if target is None or target.color == opp:
moves.append((pos, (r1, c1)))
if target is None:
r2, c2 = r1 + dr, c1 + dc
if 0 <= r2 < ROWS and 0 <= c2 < COLS:
t2 = grid[r2][c2]
if t2 is None or t2.color == opp:
moves.append((pos, (r2, c2)))
return moves
class Queen(Piece):
z_idx = 4
def symbol(self): return "♕" if self.color == "white" else "♛"
def get_valid_moves(self, board, pos):
moves = []
grid = board.grid
start_index = pos[0] * COLS + pos[1]
for i in range(8):
for r, c in RAYS[start_index][i]:
target = grid[r][c]
if target is None:
moves.append((pos, (r, c)))
else:
if target.color != self.color:
moves.append((pos, (r, c)))
break
return moves
class Rook(Piece):
z_idx = 3
def symbol(self): return "♖" if self.color == "white" else "♜"
def get_valid_moves(self, board, pos):
moves = []
grid = board.grid
start_index = pos[0] * COLS + pos[1]
for i in range(4):
for r, c in RAYS[start_index][i]:
target = grid[r][c]
if target and target.color == self.color:
break
moves.append((pos, (r, c)))
return moves
class Bishop(Piece):
z_idx = 2
def symbol(self): return "♗" if self.color == "white" else "♝"
def get_valid_moves(self, board, pos):
moves = set()
grid = board.grid
r_start, c_start = pos
start_index = r_start * COLS + c_start
for i in range(4, 8):
for r, c in RAYS[start_index][i]:
target = grid[r][c]
if target:
if target.color != self.color:
moves.add((pos, (r, c)))
break
moves.add((pos, (r, c)))
for ray in BISHOP_ZIGZAG_RAYS[start_index]:
for r, c in ray:
target = grid[r][c]
if target:
if target.color != self.color:
moves.add((pos, (r, c)))
break
moves.add((pos, (r, c)))
return sorted(moves, key=lambda m: m[1])
class Knight(Piece):
z_idx = 1
def symbol(self): return "♘" if self.color == "white" else "♞"
def get_valid_moves(self, board, pos):
moves = []
grid = board.grid
for r, c in KNIGHT_ATTACKS_FROM[pos]:
if grid[r][c] is None:
moves.append((pos, (r, c)))
return moves
class Pawn(Piece):
z_idx = 0
def __init__(self, color):
super().__init__(color)
self.direction = -1 if color == "white" else 1
self.starting_row = 6 if color == "white" else 1
self.promo_rank = 0 if color == "white" else ROWS - 1
def symbol(self): return "♙" if self.color == "white" else "♟"
def get_valid_moves(self, board, pos):
moves = []
r, c = pos
direction = self.direction
grid = board.grid
opp = self.opponent_color
one_r = r + direction
if 0 <= one_r < ROWS:
target1 = grid[one_r][c]
if target1 is None or target1.color == opp:
moves.append((pos, (one_r, c)))
if r == self.starting_row and target1 is None:
two_r = r + (2 * direction)
target2 = grid[two_r][c]
if target2 is None or target2.color == opp:
moves.append((pos, (two_r, c)))
if c > 0:
target = grid[r][c - 1]
if target and target.color == opp:
moves.append((pos, (r, c - 1)))
if c < COLS - 1:
target = grid[r][c + 1]
if target and target.color == opp:
moves.append((pos, (r, c + 1)))
return moves
# -----------------------------------------------------------------------
# Board
# -----------------------------------------------------------------------
class Board:
def __init__(self, setup=True):
self.grid = [[None] * COLS for _ in range(ROWS)]
self.white_king_pos = None
self.black_king_pos = None
self.white_pieces = []
self.black_pieces = []
self.piece_counts = {
'white': {Pawn: 0, Knight: 0, Bishop: 0, Rook: 0, Queen: 0, King: 0},
'black': {Pawn: 0, Knight: 0, Bishop: 0, Rook: 0, Queen: 0, King: 0},
}
if setup:
self._setup_initial_board()
def _setup_initial_board(self):
pieces = {
0: [(0, Rook), (1, Knight), (2, Bishop), (3, Queen), (4, King),
(5, Bishop), (6, Knight), (7, Rook)],
1: [(i, Pawn) for i in range(8)],
6: [(i, Pawn) for i in range(8)],
7: [(0, Rook), (1, Knight), (2, Bishop), (3, Queen), (4, King),
(5, Bishop), (6, Knight), (7, Rook)],
}
for r, piece_list in pieces.items():
color = "black" if r < 2 else "white"
for c, piece_class in piece_list:
self.add_piece(piece_class(color), r, c)
# ---- O(1) piece-list helpers ------------------------------------------
def _list_append(self, piece):
"""Append to the colour list and record the resulting index."""
lst = self.white_pieces if piece.color == 'white' else self.black_pieces
piece._list_pos = len(lst)
lst.append(piece)
def _list_remove(self, piece):
"""
O(1) swap-and-pop removal. Swaps piece with the last element so the
list stays compact, then pops the tail. Safe to call on a piece that
is already absent (_list_pos == -1) — treated as a no-op.
"""
idx = piece._list_pos
if idx < 0:
return
lst = self.white_pieces if piece.color == 'white' else self.black_pieces
last = lst[-1]
lst[idx] = last
last._list_pos = idx
lst.pop()
piece._list_pos = -1
# ---- Board mutation primitives ----------------------------------------
def add_piece(self, piece, r, c):
if self.grid[r][c] is not None:
self.remove_piece(r, c)
self.grid[r][c] = piece
piece.pos = (r, c)
self._list_append(piece)
self.piece_counts[piece.color][type(piece)] += 1
if type(piece) is King:
if piece.color == 'white': self.white_king_pos = (r, c)
else: self.black_king_pos = (r, c)
def remove_piece(self, r, c):
piece = self.grid[r][c]
if not piece:
return
self._list_remove(piece)
self.piece_counts[piece.color][type(piece)] -= 1
if type(piece) is King:
if piece.color == 'white': self.white_king_pos = None
else: self.black_king_pos = None
piece.pos = None
self.grid[r][c] = None
def move_piece(self, start, end):
piece = self.grid[start[0]][start[1]]
if not piece:
return
piece.pos = end
if type(piece) is King:
if piece.color == 'white': self.white_king_pos = end
else: self.black_king_pos = end
self.grid[start[0]][start[1]] = None
self.grid[end[0]][end[1]] = piece
def find_king_pos(self, color):
return self.white_king_pos if color == 'white' else self.black_king_pos
def clone(self):
new_board = Board.__new__(Board)
new_board.grid = [[None] * COLS for _ in range(ROWS)]
new_board.white_king_pos = self.white_king_pos
new_board.black_king_pos = self.black_king_pos
white_pieces = [_clone_piece_fast(p) for p in self.white_pieces]
black_pieces = [_clone_piece_fast(p) for p in self.black_pieces]
new_board.white_pieces = white_pieces
new_board.black_pieces = black_pieces
grid = new_board.grid
for p in white_pieces:
r, c = p.pos; grid[r][c] = p
for p in black_pieces:
r, c = p.pos; grid[r][c] = p
pc = self.piece_counts
new_board.piece_counts = {
'white': pc['white'].copy(),
'black': pc['black'].copy(),
}
return new_board
# -----------------------------------------------------------------------
# make_move (UI path — delegates to make_move_track)
# -----------------------------------------------------------------------
def make_move(self, start, end):
self.make_move_track(start, end)
# -----------------------------------------------------------------------
# make_move_track / unmake_move (search path — no cloning)
# -----------------------------------------------------------------------
def make_move_track(self, start, end):
moving_piece = self.grid[start[0]][start[1]]
removed = []
added = []
mc = moving_piece.color
mp_z = moving_piece.z_idx
target_piece = self.grid[end[0]][end[1]]
is_capture = target_piece is not None
# ── 1. Rook piercing ──
if mp_z == 3: # Rook
dr = (end[0] > start[0]) - (start[0] > end[0])
dc = (end[1] > start[1]) - (start[1] > end[1])
cr, cc = start[0] + dr, start[1] + dc
while (cr, cc) != end:
t = self.grid[cr][cc]
if t is not None and t.color != mc:
removed.append((t, cr, cc))
self.remove_piece(cr, cc)
cr += dr
cc += dc
# ── 2. Standard capture ──
if is_capture:
removed.append((target_piece, end[0], end[1]))
self.remove_piece(end[0], end[1])
# ── 3. Move ──
self.move_piece(start, end)
# ── 4. Queen AOE explosion ──
if mp_z == 4 and is_capture: # Queen
removed.append((moving_piece, end[0], end[1]))
self.remove_piece(end[0], end[1])
for r, c in ADJACENT_SQUARES_MAP[end]:
adj = self.grid[r][c]
if adj is not None and adj.color != mc:
removed.append((adj, r, c))
self.remove_piece(r, c)
# ── 5. Pawn promotion ──
elif mp_z == 0 and end[0] == moving_piece.promo_rank: # Pawn
removed.append((moving_piece, end[0], end[1]))
self.remove_piece(end[0], end[1])
new_queen = Queen(mc)
self.add_piece(new_queen, end[0], end[1])
added.append((new_queen, end[0], end[1]))
# ── 6. Knight AOE ──
grid = self.grid
if mp_z == 1: # Knight
enemy_knight_coords = []
for r, c in KNIGHT_ATTACKS_FROM[end]:
target = grid[r][c]
if target is not None and target.color != mc:
if target.z_idx == 1:
enemy_knight_coords.append((r, c))
for ek_r, ek_c in enemy_knight_coords:
for r, c in KNIGHT_ATTACKS_FROM[(ek_r, ek_c)]:
target = grid[r][c]
if target is not None and target.color == mc:
removed.append((target, r, c))
self.remove_piece(r, c)
for r, c in KNIGHT_ATTACKS_FROM[end]:
target = grid[r][c]
if target is not None and target.color != mc:
removed.append((target, r, c))
self.remove_piece(r, c)
else:
# Passive evaporation check
victim = grid[end[0]][end[1]]
if victim is not None:
for r, c in KNIGHT_ATTACKS_FROM[end]:
killer = grid[r][c]
if killer is not None and killer.z_idx == 1 and killer.color != mc:
removed.append((victim, end[0], end[1]))
self.remove_piece(end[0], end[1])
break
return (start, end, moving_piece, removed, added)
def unmake_move(self, record_tuple):
"""Restore the board to the exact state before make_move_track()."""
start, end, moving_piece, removed, added = record_tuple
added_ids = {id(p) for p, _r, _c in added} if added else set()
# ── 1. Undo promoted pieces ──
for piece, r, c in reversed(added):
if piece.pos is not None:
self.grid[r][c] = None
piece.pos = None
self._list_remove(piece)
self.piece_counts[piece.color][type(piece)] -= 1
mp_pos = moving_piece.pos
if mp_pos is not None:
self.grid[mp_pos[0]][mp_pos[1]] = None
self.grid[start[0]][start[1]] = moving_piece
moving_piece.pos = start
if moving_piece.z_idx == 5:
if moving_piece.color == 'white': self.white_king_pos = start
else: self.black_king_pos = start
else:
self.grid[start[0]][start[1]] = moving_piece
moving_piece.pos = start
self._list_append(moving_piece)
self.piece_counts[moving_piece.color][type(moving_piece)] += 1
if moving_piece.z_idx == 5:
if moving_piece.color == 'white': self.white_king_pos = start
else: self.black_king_pos = start
for piece, r, c in removed:
if piece is moving_piece: continue
if id(piece) in added_ids: continue
self.grid[r][c] = piece
piece.pos = (r, c)
self._list_append(piece)
self.piece_counts[piece.color][type(piece)] += 1
if piece.z_idx == 5:
if piece.color == 'white': self.white_king_pos = (r, c)
else: self.black_king_pos = (r, c)
# -----------------------------------------------------------------------
# Global game logic
# -----------------------------------------------------------------------
def _bishop_attacks_square(board, start, tr, tc, bishop_color):
if ((start[0] + start[1] - tr - tc) & 1) != 0:
return False
grid = board.grid
start_index = start[0] * COLS + start[1]
for ray in RAYS[start_index][4:]:
for r, c in ray:
piece = grid[r][c]
if r == tr and c == tc:
return (piece is None) or (piece.color != bishop_color)
if piece is not None:
break
for ray in BISHOP_ZIGZAG_RAYS[start_index]:
for r, c in ray:
piece = grid[r][c]
if r == tr and c == tc:
return (piece is None) or (piece.color != bishop_color)
if piece is not None:
break
return False
def is_square_attacked(board, r, c, attacking_color):
grid = board.grid
defending_color = 'black' if attacking_color == 'white' else 'white'
attacking_pieces = board.white_pieces if attacking_color == 'white' else board.black_pieces
attacker_counts = board.piece_counts[attacking_color]
attacking_king_pos = board.white_king_pos if attacking_color == 'white' else board.black_king_pos
if len(attacking_pieces) == attacker_counts[King]:
if attacking_king_pos:
kr, kc = attacking_king_pos
dr, dc = r - kr, c - kc
abs_dr, abs_dc = abs(dr), abs(dc)
m_dist = max(abs_dr, abs_dc)
if m_dist == 1:
return True
if m_dist == 2 and (abs_dr == abs_dc or abs_dr == 0 or abs_dc == 0):
if grid[kr + dr // 2][kc + dc // 2] is None:
return True
return False
if attacker_counts[Knight] > 0:
for pr, pc in KNIGHT_ATTACKS_FROM[(r, c)]:
p = grid[pr][pc]
if p is not None:
if p.z_idx == 1 and p.color == attacking_color:
return True
else:
for qr, qc in KNIGHT_ATTACKS_FROM[(pr, pc)]:
q = grid[qr][qc]
if q is not None and q.z_idx == 1 and q.color == attacking_color:
return True
if attacker_counts[Queen] > 0:
for piece in attacking_pieces:
if piece.z_idx == 4 and piece.pos:
qr, qc = piece.pos
q_idx = qr * COLS + qc
for i in range(8):
for cr, cc in RAYS[q_idx][i]:
target = grid[cr][cc]
if target is not None:
if target.color == defending_color:
if abs(cr - r) <= 1 and abs(cc - c) <= 1:
return True
break
has_rooks = attacker_counts[Rook] > 0
start_index = r * COLS + c
for direction_idx, ray_path in enumerate(RAYS[start_index]):
is_orthogonal = direction_idx < 4
defenders_passed = 0
for cr, cc in ray_path:
piece = grid[cr][cc]
if piece is None:
continue
p_z = piece.z_idx
if piece.color == attacking_color:
if is_orthogonal:
if p_z == 3:
return True
else:
if p_z == 2 and defenders_passed == 0:
return True
break
else:
defenders_passed += 1
if not has_rooks:
break
pawn_move_dir = -1 if attacking_color == 'white' else 1
pr = r - pawn_move_dir
if 0 <= pr < ROWS:
p = grid[pr][c]
if p is not None and p.color == attacking_color and p.z_idx == 0:
return True
elif p is None:
two_pr = r - (2 * pawn_move_dir)
starting_row = 6 if attacking_color == 'white' else 1
if 0 <= two_pr < ROWS and two_pr == starting_row:
p2 = grid[two_pr][c]
if p2 is not None and p2.color == attacking_color and p2.z_idx == 0:
return True
for dc_off in (-1, 1):
pc = c + dc_off
if 0 <= pc < COLS:
p = grid[r][pc]
if p is not None and p.color == attacking_color and p.z_idx == 0:
return True
if attacking_king_pos:
kr, kc = attacking_king_pos
dr, dc = r - kr, c - kc
abs_dr, abs_dc = abs(dr), abs(dc)
m_dist = max(abs_dr, abs_dc)
if m_dist == 1:
return True
if m_dist == 2 and (abs_dr == abs_dc or abs_dr == 0 or abs_dc == 0):
if grid[kr + dr // 2][kc + dc // 2] is None:
return True
if attacker_counts[Bishop] > 0:
target_parity = (r + c) & 1
for piece in attacking_pieces:
pos = piece.pos
if piece.z_idx == 2 and pos and ((pos[0] + pos[1]) & 1) == target_parity:
if _bishop_attacks_square(board, pos, r, c, attacking_color):
return True
return False
def is_in_check(board, color):
king_pos = board.white_king_pos if color == 'white' else board.black_king_pos
if not king_pos:
return True
opponent_color = "black" if color == "white" else "white"
return is_square_attacked(board, king_pos[0], king_pos[1], opponent_color)
def generate_legal_moves_generator(board, color, yield_boards=False):
opp_color = "black" if color == "white" else "white"
piece_list = list(board.white_pieces if color == 'white' else board.black_pieces)
for piece in piece_list:
if piece.pos is None: continue
for move in piece.get_valid_moves(board, piece.pos):
record = board.make_move_track(move[0], move[1])
my_kp = board.white_king_pos if color == 'white' else board.black_king_pos
legal = (my_kp is not None and not is_square_attacked(board, my_kp[0], my_kp[1], opp_color))
if legal and yield_boards:
result_board = board.clone()
board.unmake_move(record)
if legal:
if yield_boards: yield move, result_board
else: yield move
def get_all_legal_moves(board, color):
return list(generate_legal_moves_generator(board, color))
def get_all_pseudo_legal_moves(board, color):
moves = []
piece_list = board.white_pieces if color == 'white' else board.black_pieces
for piece in piece_list:
if piece.pos is not None:
moves.extend(piece.get_valid_moves(board, piece.pos))
return moves
def has_legal_moves(board, color):
try:
next(generate_legal_moves_generator(board, color))
return True
except StopIteration:
return False
def is_insufficient_material(board):
return (len(board.white_pieces) + len(board.black_pieces)) <= 2
def get_game_state(board, turn_to_move, position_counts, ply_count, max_moves):
if not has_legal_moves(board, turn_to_move):
winner = 'black' if turn_to_move == 'white' else 'white'
return ("checkmate", winner)
if is_insufficient_material(board):
return ("insufficient_material", None)
try:
from AI import board_hash
if position_counts.get(board_hash(board, turn_to_move), 0) >= 3:
return ("repetition", None)
except ImportError:
pass
if ply_count >= max_moves:
return ("move_limit", None)
return "ongoing", None
def is_draw(board, turn_to_move, position_counts, ply_count, max_moves):
state, _ = get_game_state(board, turn_to_move, position_counts, ply_count, max_moves)
return state in ("insufficient_material", "repetition", "move_limit")
def fast_approximate_material_swing(board, move, moving_piece, target_piece, piece_values_list):
swing = 0
is_tactic = False
my_z = moving_piece.z_idx
my_color = moving_piece.color
if target_piece is not None:
swing += piece_values_list[target_piece.z_idx]
is_tactic = True
if my_z == 0 and move[1][0] == moving_piece.promo_rank:
swing += piece_values_list[4] - piece_values_list[0]
is_tactic = True
if my_z == 4 and target_piece is not None:
swing -= piece_values_list[4]
for r, c in ADJACENT_SQUARES_MAP[move[1]]:
adj = board.grid[r][c]
if adj and adj.color != my_color:
swing += piece_values_list[adj.z_idx]
is_tactic = True
return swing, is_tactic
pierced_knights = []
if my_z == 3:
start, end = move
dr = (end[0] > start[0]) - (start[0] > end[0])
dc = (end[1] > start[1]) - (start[1] > end[1])
cr, cc = start[0] + dr, start[1] + dc
while (cr, cc) != end:
target = board.grid[cr][cc]
if target and target.color != my_color:
swing += piece_values_list[target.z_idx]
is_tactic = True
if target.z_idx == 1:
pierced_knights.append((cr, cc))
cr += dr
cc += dc
if my_z == 1:
seen_passive = set()
for r, c in KNIGHT_ATTACKS_FROM[move[1]]:
target = board.grid[r][c]
if target and target.color != my_color:
swing += piece_values_list[target.z_idx]
is_tactic = True
if target.z_idx == 1:
for pr, pc in KNIGHT_ATTACKS_FROM[(r, c)]:
if (pr, pc) == move[1]:
if 1 not in seen_passive:
swing -= piece_values_list[1]
seen_passive.add(1)
else:
ptarget = board.grid[pr][pc]
if ptarget and ptarget.color == my_color and ptarget.z_idx not in seen_passive:
swing -= piece_values_list[ptarget.z_idx]
seen_passive.add(ptarget.z_idx)
return swing, is_tactic
for r, c in KNIGHT_ATTACKS_FROM[move[1]]:
pk = board.grid[r][c]
if (pk and pk.z_idx == 1
and pk.color != my_color
and (r, c) not in pierced_knights):
evap_z = 4 if (my_z == 0 and move[1][0] == moving_piece.promo_rank) else my_z
swing -= piece_values_list[evap_z]
is_tactic = True
break
return swing, is_tactic
def format_move(move):
if not move:
return "None"
(r1, c1), (r2, c2) = move
return f"{'abcdefgh'[c1]}{'87654321'[r1]}-{'abcdefgh'[c2]}{'87654321'[r2]}"
def format_move_san(board_before, board_after, move):
if not move:
return "None"
start_pos, end_pos = move
moving_piece = board_before.grid[start_pos[0]][start_pos[1]]
if not moving_piece:
return format_move(move)
ptype = type(moving_piece)
is_capture = False
if ptype is not Knight:
is_capture = board_before.grid[end_pos[0]][end_pos[1]] is not None
def file_of(c): return "abcdefgh"[c]
def rank_of(r): return "87654321"[r]
def sq_name(pos): return file_of(pos[1]) + rank_of(pos[0])
# Disambiguation — O(n_pieces) piece-list scan instead of O(64) grid scan
disambig = ""
if ptype not in (Pawn, King):
others = []
piece_list = (board_before.white_pieces if moving_piece.color == 'white'
else board_before.black_pieces)
for p in piece_list:
if type(p) is ptype and p.pos != start_pos:
if any(m[1] == end_pos for m in p.get_valid_moves(board_before, p.pos)):
others.append(p.pos)
if others:
same_file = any(pos[1] == start_pos[1] for pos in others)
same_rank = any(pos[0] == start_pos[0] for pos in others)
if not same_file:
disambig = file_of(start_pos[1])
elif not same_rank:
disambig = rank_of(start_pos[0])
else:
disambig = sq_name(start_pos)
if ptype is Pawn:
base_str = (file_of(start_pos[1]) + "x" + sq_name(end_pos)) if is_capture \
else sq_name(end_pos)
else:
p_char = {King: 'K', Queen: 'Q', Rook: 'R', Bishop: 'B', Knight: 'N'}[ptype]
cap_str = "x" if is_capture else ""
base_str = p_char + disambig + cap_str + sq_name(end_pos)
if ptype is Pawn and end_pos[0] == moving_piece.promo_rank:
base_str += "=Q"
# Dead squares — O(n_pieces) piece-list scan (retained from v52)
after_grid = board_after.grid
end_r, end_c = end_pos
dead_squares = []
if after_grid[end_r][end_c] is None:
dead_squares.append(end_pos)
for piece in board_before.white_pieces + board_before.black_pieces:
pr, pc = piece.pos
if (pr, pc) == start_pos or (pr, pc) == end_pos:
continue
if after_grid[pr][pc] is None:
dead_squares.append((pr, pc))
if dead_squares:
dead_squares.sort(key=lambda pos: (8 - pos[0], pos[1]))
cas_str = " ".join(f"x{sq_name(pos)}" for pos in dead_squares)
base_str += f" ({cas_str})"
opp_color = "black" if moving_piece.color == "white" else "white"
is_mate = not has_legal_moves(board_after, opp_color)
if is_mate:
base_str += "#"
elif is_in_check(board_after, opp_color):
base_str += "+"
return base_str