-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.py
More file actions
1443 lines (1088 loc) · 41.1 KB
/
chess.py
File metadata and controls
1443 lines (1088 loc) · 41.1 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 itertools
import random
import re
from enum import Enum
from functools import wraps
from string import ascii_lowercase
from typing import Iterator, List, Tuple, Dict, Optional
BOARD_SIZE = 8
def rule_validator(func):
"""Tag a function as a rule validator,
which should raise RuleBroken if it does not
"""
return func
class Color(Enum):
WHITE = 'WHITE'
BLACK = 'BLACK'
class Camp(Enum):
A = 'CAMP_A'
B = 'CAMP_B'
@property
def another(self):
if self == Camp.A:
return Camp.B
elif self == Camp.B:
return Camp.A
class Job(Enum):
"""which kind a piece is"""
KING = 'KING'
QUEEN = 'QUEEN'
BISHOP = 'BISHOP'
KNIGHT = 'KNIGHT'
CASTLE = 'CASTLE'
PAWN = 'PAWN'
class Square(object):
def __init__(self, x, y):
self.x = x
self.y = y
@property
def color(self):
return Color.BLACK if (self.x + self.y) % 2 == 0 else Color.WHITE
@property
def name(self):
return ascii_lowercase[self.x] + str(self.y + 1)
def format(self):
return str(self)
def __str__(self):
return 'square {}'.format(self.name)
def __repr__(self):
return 'Square(x={s.x}, y={s.y})'.format(s=self)
@classmethod
def by_name(cls, name):
column = name[0]
line = name[1]
x = ascii_lowercase.index(column)
y = int(line) - 1
return cls(x, y)
def __hash__(self):
return hash((self.x, self.y))
def __eq__(self, other):
return isinstance(other, Square) and self.x == other.x and self.y == other.y
def is_ending_line(self, camp, index=0):
if camp == Camp.A:
return self.y == (BOARD_SIZE - 1 - index)
else:
return self.y == (0 + index)
def is_starting_line(self, camp, index=0):
if camp == Camp.A:
return self.y == (0 + index)
else:
return self.y == (BOARD_SIZE - 1 - index)
def __add__(self, delta: 'Delta'):
return self.__class__(x=(self.x + delta.x), y=(self.y + delta.y))
def __sub__(self, sq: 'Square'):
return Delta(x=(self.x - sq.x), y=(self.y - sq.y))
def is_on_board(self):
return 0 <= self.x < BOARD_SIZE and 0 <= self.y < BOARD_SIZE
def within_board(f):
@wraps(f)
def ff(*args, **kwargs):
return filter(lambda x: x.is_on_board(), f(*args, **kwargs))
return ff
class Piece(object):
@property
def job(self):
raise NotImplementedError
@property
def abbr_char(self):
raise NotImplementedError
@property
def abbr(self):
return self.abbr_char.upper() if self.camp == Camp.A else self.abbr_char.lower()
def __init__(self, camp):
self.camp = camp
def __str__(self):
return '{} of {}'.format(self.job.value, self.camp.value)
def __repr__(self):
return '{cls}({cm})'.format(cls=self.__class__.__name__, cm=self.camp)
def __eq__(self, other):
return isinstance(other, Piece) and self.camp == other.camp and self.job == other.job
def format(self):
return str(self)
@rule_validator
def validate_movement(self, chess, mv: 'Movement') -> None:
"""if the movement <mv> about the job is valid. If not, tell reason"""
raise NotImplementedError
def generate_movements(self, chess, sq: Square) -> Iterator['Movement']:
"""generate all movements launched by the piece from square <sq>"""
raise NotImplementedError
def attack_lst(self, chess, sq: Square) -> Iterator[Square]:
"""generate all squares it is able to capture in the next movement from square <sq>,
even if there's not a piece or the piece is of same camp"""
raise NotImplementedError
def cons_piece(camp, job):
return {
Job.KNIGHT: PKnight,
Job.PAWN: PPawn,
Job.KING: PKing,
Job.QUEEN: PQueen,
Job.CASTLE: PCastle,
Job.BISHOP: PBishop
}[job](camp)
class Chess(object):
@classmethod
def setup(cls, initial=None):
def s(name):
return Square.by_name(name)
c = Chess()
initial = initial or [
[Camp.A, Job.CASTLE, s('a1')],
[Camp.A, Job.KNIGHT, s('b1')],
[Camp.A, Job.BISHOP, s('c1')],
[Camp.A, Job.QUEEN, s('d1')],
[Camp.A, Job.KING, s('e1')],
[Camp.A, Job.BISHOP, s('f1')],
[Camp.A, Job.KNIGHT, s('g1')],
[Camp.A, Job.CASTLE, s('h1')],
[Camp.A, Job.PAWN, s('a2')],
[Camp.A, Job.PAWN, s('b2')],
[Camp.A, Job.PAWN, s('c2')],
[Camp.A, Job.PAWN, s('d2')],
[Camp.A, Job.PAWN, s('e2')],
[Camp.A, Job.PAWN, s('f2')],
[Camp.A, Job.PAWN, s('g2')],
[Camp.A, Job.PAWN, s('h2')],
[Camp.B, Job.CASTLE, s('a8')],
[Camp.B, Job.KNIGHT, s('b8')],
[Camp.B, Job.BISHOP, s('c8')],
[Camp.B, Job.QUEEN, s('d8')],
[Camp.B, Job.KING, s('e8')],
[Camp.B, Job.BISHOP, s('f8')],
[Camp.B, Job.KNIGHT, s('g8')],
[Camp.B, Job.CASTLE, s('h8')],
[Camp.B, Job.PAWN, s('a7')],
[Camp.B, Job.PAWN, s('b7')],
[Camp.B, Job.PAWN, s('c7')],
[Camp.B, Job.PAWN, s('d7')],
[Camp.B, Job.PAWN, s('e7')],
[Camp.B, Job.PAWN, s('f7')],
[Camp.B, Job.PAWN, s('g7')],
[Camp.B, Job.PAWN, s('h7')],
]
for loc in initial:
c.put(piece=cons_piece(camp=loc[0], job=loc[1]), square=loc[2])
return c
def __init__(self):
self.square_to_piece: Dict[Square, Optional[Piece]] = {}
for i in range(8):
for j in range(8):
self.square_to_piece[Square(i, j)] = None
self.turn = None # who's turn
self.history = []
self.turn_count_without_capture = 0
def copy(self):
c = self.__class__()
c.square_to_piece = dict(self.square_to_piece)
c.turn = self.turn
c.history = list(self.history)
return c
def put(self, piece, square):
self.square_to_piece[square] = piece
def remove(self, square, piece=None) -> Optional[Piece]:
if not isinstance(square, Square):
raise ValueError('invalid square: {}'.format(square))
if piece is not None:
the_pi = self.square_to_piece.get(square)
if the_pi != piece:
raise ValueError('piece to move is not a {}, but {}'.format(piece, the_pi))
pi = self.square_to_piece.get(square)
if pi:
self.square_to_piece[square] = None
return pi
def apply(self, mv, into_history=True):
if self.square_to_piece.get(mv.frm) is None:
raise mv.MovementError('{} is empty so nothing to move'.format(mv.frm.format()))
if not mv.capture and mv.to and self.square_to_piece.get(mv.to):
raise mv.MovementError('{} is occupied by another piece'.format(mv.to.format()))
if mv.capture and self.square_to_piece.get(mv.capture) is None:
raise mv.MovementError('{} is empty so nothing to capture'.format(mv.capture.format()))
pie = self.remove(square=mv.frm)
if mv.capture:
self.remove(square=mv.capture)
self.put(square=mv.to or mv.capture, piece=mv.replace or pie)
if mv.sub_movement:
self.apply(mv.sub_movement, into_history=False)
if into_history:
self.history.append(mv)
if not mv.capture:
self.turn_count_without_capture += 1
else:
self.turn_count_without_capture = 0
return self
def format(self):
"""a simple way to visualize"""
def format_line(y):
ab_lst = []
for i in range(8):
sq = Square(i, y)
p = self.square_to_piece[sq]
ab = piece_abbr(sq, p)
ab_lst.append(ab)
return ' '.join(ab_lst)
def piece_abbr(sq, p=None):
c = '.' if sq.color == Color.WHITE else '|'
if p is None:
return c + c
else:
return p.abbr + c
lines = []
for j in range(7, -1, -1):
line = format_line(j)
lines.append(line)
return '\n'.join(lines)
def locations(self, camp=None):
if camp is None:
return [(sq, pi) for sq, pi in self.square_to_piece.items() if pi]
else:
return [(sq, pi) for sq, pi in self.square_to_piece.items() if (pi and pi.camp == camp)]
def find_king(self, camp=None) -> Tuple[Square, Piece]:
"""Find out your king"""
if camp is None:
camp = self.turn
for sq, pi in self.square_to_piece.items():
if pi and pi.job == Job.KING and pi.camp == camp:
return sq, pi
raise ValueError('King not found')
@property
def last_movement(self):
if self.history:
return self.history[-1]
class Movement(object):
"""movement that don't take chess rule into account"""
class MovementError(Exception):
pass
def __init__(self, piece: Piece, frm, to=None, capture=None, replace=None, sub_movement: 'Movement' = None):
self.frm = frm
if to is None and capture is None:
raise self.MovementError('either move to or capture should given')
# two arguments for move from and move to respectively,
# for the sake of "En passant"
self.to = to or capture
self.capture = capture
# replace is designed for pawn promotion
self.replace = replace
self.sub_movement = sub_movement
# the piece that moves
self.piece = piece
@property
def name(self):
if self.capture:
return '{}x{}'.format(self.frm.name, self.capture.name)
elif self.to:
return '{}-{}'.format(self.frm.name, self.to.name)
else:
raise ValueError('cant get here')
def __str__(self):
return self.format()
def __repr__(self):
return str(self)
def format(self):
return 'movement {}'.format(self.name)
class MLongCastling(Movement):
def __init__(self, camp: Camp):
if camp == Camp.A:
frm = Square.by_name('e1')
to = Square.by_name('c1')
rook_frm = Square.by_name('a1')
rook_to = Square.by_name('d1')
elif camp == Camp.B:
frm = Square.by_name('e8')
to = Square.by_name('c8')
rook_frm = Square.by_name('a8')
rook_to = Square.by_name('d8')
else:
raise ValueError
super(MLongCastling, self).__init__(
piece=PKing(camp=camp),
frm=frm,
to=to,
sub_movement=Movement(
piece=PCastle(camp=camp),
frm=rook_frm,
to=rook_to
)
)
def format(self):
return 'O-O-O'
class MShortCastling(Movement):
def __init__(self, camp: Camp):
if camp == Camp.A:
frm = Square.by_name('e1')
to = Square.by_name('g1')
rook_frm = Square.by_name('h1')
rook_to = Square.by_name('f1')
elif camp == Camp.B:
frm = Square.by_name('e8')
to = Square.by_name('g8')
rook_frm = Square.by_name('h8')
rook_to = Square.by_name('f8')
else:
raise ValueError
super(MShortCastling, self).__init__(
piece=PKing(camp=camp),
frm=frm,
to=to,
sub_movement=Movement(
piece=PCastle(camp=camp),
frm=rook_frm,
to=rook_to
)
)
def format(self):
return 'O-O'
def guess_movement(command_text, chess):
cmd = re.sub(r'\s', '', command_text).lower()
# remove comments
if '#' in cmd:
cmd = cmd.split('#')[0]
if cmd == 'o-o':
return MShortCastling(camp=chess.turn)
elif cmd == 'o-o-o':
return MLongCastling(camp=chess.turn)
rep = None
if '=' in cmd:
move, rep = cmd.split('=')
for j in [PKing, PQueen, PPawn, PCastle, PBishop, PKnight]:
if rep == j.abbr_char.lower():
rep = j.job
break
else:
raise ValueError('={} does not represent any job'.format(rep))
cmd = move
segs = re.split(r'[-x*]', cmd)
# from and to
if len(segs) != 2:
raise ValueError('Can not understand ' + cmd)
frm, to = segs
frm = Square.by_name(frm)
to = Square.by_name(to)
# find in all valid movements
for _mv in generate_movements(chess, camp=chess.turn):
if _mv.frm == frm and _mv.to == to:
# if found one, then that's it
return _mv
# if not found, that's an invalid movement
# but it's not our job to complain here, it's done in validation procedure
pi = chess.square_to_piece.get(frm)
if has_piece(chess, at=to):
mv = Movement(piece=pi, frm=frm, capture=to, replace=rep)
else:
mv = Movement(piece=pi, frm=frm, to=to, replace=rep)
return mv
class Delta(object):
def __init__(self, x, y):
self.x: int = x
self.y: int = y
@classmethod
def as_camp(cls, forward=None, backward=None, leftward=None, rightward=None, camp=Camp.A):
assert not (forward and backward), 'forward and backward cant both exist'
assert not (leftward and rightward), 'leftward and rightward cant both exist'
forward = forward or 0
backward = backward or 0
leftward = leftward or 0
rightward = rightward or 0
if camp == Camp.A:
x = rightward or -leftward
y = forward or -backward
elif camp == Camp.B:
x = leftward or -rightward
y = backward or -forward
else:
raise ValueError
return cls(x=x, y=y)
@property
def is_zero(self):
return self.x == 0 and self.y == 0
@property
def is_vertical(self):
return self.x == 0 and self.y != 0
@property
def is_horizontal(self):
return self.x != 0 and self.y == 0
@property
def is_north(self):
return self.x == 0 and self.y > 0
@property
def is_south(self):
return self.x == 0 and self.y < 0
@property
def is_west(self):
return self.x < 0 and self.y == 0
@property
def is_east(self):
return self.x > 0 and self.y == 0
@property
def is_north_west(self):
# noinspection PyChainedComparisons
return self.x < 0 and self.y > 0
@property
def is_north_east(self):
return self.x > 0 and self.y > 0
@property
def is_south_west(self):
return self.x < 0 and self.y < 0
@property
def is_south_east(self):
# noinspection PyChainedComparisons
return self.x > 0 and self.y < 0
@property
def is_diagonal(self):
return self.is_north_west or self.is_north_east or self.is_south_west or self.is_south_east
def is_forward(self, camp, just=True):
if just:
if camp == Camp.A:
return self.is_north
else:
return self.is_south
else:
if camp == Camp.A:
return self.y > 0
else:
return self.y < 0
def is_backward(self, camp, just=True):
if just:
if camp == Camp.A:
return self.is_south
else:
return self.is_north
else:
if camp == Camp.A:
return self.y > 0
else:
return self.y < 0
@property
def is_l_shape(self):
"""knight move in l shape"""
return {abs(self.x), abs(self.y)} == {1, 2}
@property
def dis(self) -> int:
return max(abs(self.x), abs(self.y))
def __add__(self, other):
return self.__class__(x=self.x + other.x, y=self.y + other.y)
def __mul__(self, other):
return self.__class__(x=self.x * other, y=self.y * other)
def __neg__(self):
return self.__class__(x=-self.x, y=-self.y)
def moved(self):
return self.x != 0 or self.y != 0
def __bool__(self):
return self.moved()
@property
def unit(self):
return self.__class__(
x=(self.x // abs(self.x or 1)),
y=(self.y // abs(self.y or 1))
)
ALL_DIRECTIONS = [
Delta(0, 1), # north
Delta(1, 1), # north east
Delta(1, 0), # east
Delta(1, -1), # south east
Delta(0, -1), # south
Delta(-1, -1), # south west
Delta(-1, 0), # west
Delta(-1, 1), # north west
]
HORIZONTAL_AND_VERTICAL_DIRECTIONS = [d for d in ALL_DIRECTIONS if d.is_horizontal or d.is_vertical]
DIAGONAL_DIRECTIONS = [d for d in ALL_DIRECTIONS if d.is_diagonal]
L_SHAPES = [
Delta(-2, 1),
Delta(-1, 2),
Delta(1, 2),
Delta(2, 1),
Delta(2, -1),
Delta(1, -2),
Delta(-1, -2),
Delta(-2, -1)
]
# Implement path
#
# If two different squares <start> and <end> is in same file (column), or same rank (line), or same diagonal, there is a
# path between them. It means, a piece may go straight from <start> to <end>, without interfering piece into account.
#
# This concept is useful:
# 1. check moving is straight or not
# 2. check interfering piece
class InvalidPath(ValueError):
pass
def make_path(start: Square, end: Square = None, delta: Delta = None, straight_only=True) -> List[Square]:
if end and delta:
raise ValueError('either end or delta should be given')
if end:
delta = end - start
err = None
if delta.x != 0 and delta.y != 0 and abs(delta.x) != abs(delta.y):
err = InvalidPath((delta.x, delta.y))
if err:
if straight_only:
raise err
else:
return []
unit = delta.unit
return [start + (unit * i) for i in range(1, delta.dis)]
def has_interfering_piece(chess, path):
"""if some piece is in the way"""
for sq in path:
if chess.square_to_piece.get(sq):
return True
return False
# Implement chess rules
#
# Chess rules have two sides.
# 1. check if a movement is valid of not, and give reason which rule is broken
# 2. generate all possible movements
#
# Both these two sides are mainly delegated to subclass of Piece, like PKing, PQueen etc. , as rules of chess
# are strongly related to piece and job. Just like prototype of abstract class Piece shows, each Piece has several
# methods related to rule:
#
# @rule_validator
# def validate_movement(self, chess, mv: 'Movement') -> None:
# # side 1, validate a movement
# pass
#
# def generate_movements(self, chess, sq: Square) -> Iterator['Movement']:
# # side 2, generate all movements related to the current piece, the piece is at <sq>
# pass
#
# def attack_lst(self, chess, sq: Square) -> Iterator[Square]:
# # list all squares it can capture suppose there stand an enemy piece from square <sq>
# pass
#
# All rule validators are decorated by "rule_validator", and they should raise RuleBroken exception with reason.
class RuleBroken(Exception):
ok = False
def __bool__(self):
return False
class KingInDanger(RuleBroken):
pass
@rule_validator
def validate_movement(chess, mv: Movement) -> None:
# 1. You have to move a piece, but not move air
# 2. You can't move piece out of board
# 3. The piece you move must be of your own camp, you can't move your partner's piece
# 4. The movement must comply move rule of the job ... It differs from the job , but generally speaking,
# a. If you want to capture another piece, make sure there's one to capture
# b. If you capture a piece, it can't be of your own camp
# 5. If your king is currently in danger, the movement must save your king.
# (note: If the king is currently in danger, and none of possible movements is able to save him, it's checkmate
# and should be found right after last movement.)
# 6. And make sure that the movement will not expose your king to danger.
pi: Piece = chess.square_to_piece.get(mv.frm)
if pi is None:
raise RuleBroken('There is not a piece')
if pi != mv.piece:
raise RuleBroken('Piece on the board differ from given')
if not mv.to.is_on_board():
raise RuleBroken('You cant move out of board')
if pi.camp != chess.turn:
raise RuleBroken('You can\'t move your partner\'s piece')
# special logic for castling
if isinstance(mv, (MLongCastling, MShortCastling)):
_, king_pi = chess.find_king(camp=chess.turn)
pi = king_pi
pi.validate_movement(chess, mv)
# check King safety
chess = chess.copy()
chess = chess.apply(mv)
king_loc, _ = chess.find_king(camp=chess.turn)
# traverse piece from other camp
if is_under_attack(chess, sq=king_loc, by_camp=chess.turn.another):
raise KingInDanger('King in danger')
def generate_movements(chess, camp) -> Iterator[Movement]:
"""All possible movements that will not put King in danger"""
for sq, pi in chess.locations(camp=camp):
for mv in pi.generate_movements(chess, sq=sq):
try:
validate_movement(chess, mv)
except RuleBroken:
pass
else:
yield mv
def is_under_attack(chess, sq: Square, by_camp=None) -> bool:
"""check if square <sq> is under attack by camp <by_camp>"""
for at_sq, pi in chess.locations(camp=by_camp):
for att_sq in pi.attack_lst(chess, sq=at_sq):
if att_sq == sq:
return True
return False
def has_enemy(chess, camp, at: Square):
return chess.square_to_piece.get(at) and chess.square_to_piece.get(at).camp != camp
def has_alias(chess, camp, at: Square):
return chess.square_to_piece.get(at) and chess.square_to_piece.get(at).camp == camp
def has_piece(chess, at: Square):
return bool(chess.square_to_piece.get(at))
@rule_validator
def is_valid_capture(chess, capture, camp) -> None:
ca = chess.square_to_piece.get(capture)
if ca is None:
raise RuleBroken('There is no piece to capture')
if ca.camp != camp:
pass
else:
raise RuleBroken('You can not capture piece of your own camp')
@rule_validator
def is_valid_move_to(chess, move: Square) -> None:
if has_piece(chess, at=move):
raise RuleBroken('You can not move to a square with piece')
@rule_validator
def is_clear_path(chess, path: Iterator[Square]) -> None:
if has_interfering_piece(chess, path):
raise RuleBroken('You can not move over other pieces')
def move_to_or_capture(chess, frm, to, camp, piece):
if has_enemy(chess, camp=camp, at=to):
return Movement(piece=piece, frm=frm, capture=to)
elif has_alias(chess, camp=camp, at=to):
return
else:
return Movement(piece=piece, frm=frm, to=to)
def make_movements_by_target(chess, frm, to_lst, camp, piece):
for to in to_lst:
mv = move_to_or_capture(chess, frm, to, camp, piece)
if mv:
yield mv
def move_down_straight(chess, square, direction):
"""generate squares along a direction <direction> from a start point <square>,
and stops till a interfering piece or chess border is met"""
hidden = False
for dis in range(1, BOARD_SIZE):
if hidden:
break
delta = direction * dis
to = square + delta
if has_piece(chess, at=to):
hidden = True
yield to
else:
yield to
class PKing(Piece):
abbr_char = 'K'
job = Job.KING
@within_board
def attack_lst(self, chess, sq: Square) -> Iterator[Square]:
# squares surrounding it in all eight direction on board
for di in ALL_DIRECTIONS:
yield sq + di
def generate_movements(self, chess, sq: Square) -> Iterator['Movement']:
# regular moves
# squares surrounding it in all eight direction on board, able to move there and capture a enemy
# castling
# positive condition:
# 1. king has not moved
# 2. the castle involved has not moved
# 3. no any other pieces in the way
# 4. squares in the way is not currently attacked by any enemy piece
# movement:
# 1. king move toward the castle by two square
# 2. the castle move toward, over, and right beside king
frm = sq
for mv in make_movements_by_target(chess, frm, self.attack_lst(chess, sq=frm), camp=self.camp, piece=self):
yield mv
cas = MLongCastling(camp=self.camp)
try:
self.validate_castling(chess, cas)
except RuleBroken:
pass
else:
yield cas
cas = MShortCastling(camp=self.camp)
try:
self.validate_castling(chess, cas)
except RuleBroken:
pass
else:
yield cas
@rule_validator
def validate_movement(self, chess, mv: 'Movement') -> None:
if isinstance(mv, (MLongCastling, MShortCastling)):
self.validate_castling(chess, mv)
else:
king_rule_caption = 'King only move straight by one square'
try:
delta = mv.to - mv.frm
make_path(start=mv.frm, delta=delta, straight_only=True)
except InvalidPath:
raise RuleBroken(king_rule_caption)
if delta.dis >= 2:
raise RuleBroken(king_rule_caption)
if mv.capture:
is_valid_capture(chess, mv.capture, camp=self.camp)
else:
is_valid_move_to(chess, mv.to)
@rule_validator
def validate_castling(self, chess, cas: 'Movement') -> None:
king_loc = cas.frm
rook_loc = cas.sub_movement.frm
if has_piece(chess, king_loc):
if chess.square_to_piece[king_loc].job != Job.KING:
raise RuleBroken('King has moved')
else:
raise RuleBroken('King has moved')
if has_piece(chess, rook_loc):
if chess.square_to_piece[rook_loc].job != Job.CASTLE:
raise RuleBroken('Castle has moved')
else:
raise RuleBroken('Castle has moved')
path = make_path(start=king_loc, end=rook_loc)
# if interfered by other pieces
try:
is_clear_path(chess, path)
except RuleBroken:
raise RuleBroken('Castling interfered by other pieces')
# king or rook in currently attacked
if is_under_attack(chess, sq=king_loc, by_camp=self.camp.another) or \
is_under_attack(chess, sq=rook_loc, by_camp=self.camp.another):
raise RuleBroken('You cannot do castling when king or rook is attacked')
# danger in path way
for psq in path:
if is_under_attack(chess, sq=psq, by_camp=self.camp.another):
raise RuleBroken('Castling requires a totally safe path')
# check if rook has moved
for his_mv in chess.history:
if his_mv.frm == king_loc:
raise RuleBroken('King has moved')
elif his_mv.frm == rook_loc:
raise RuleBroken('Castle has moved')
class PPawn(Piece):
abbr_char = 'P'
job = Job.PAWN
@within_board
def attack_lst(self, chess, sq: Square) -> Iterator[Square]:
# one square ahead of columns on both side
# special case: en passant
# If a pawn is beside an enemy pawn who has just charged two square,
# he can move diagonally as usual and attack back to capture the enemy pawn
yield sq + Delta.as_camp(forward=1, leftward=1, camp=self.camp)
yield sq + Delta.as_camp(forward=1, rightward=1, camp=self.camp)
lmv = chess.last_movement