@@ -64,8 +64,28 @@ def piece_name(piece_type: PieceType) -> str:
6464 "P" : "♙" , "p" : "♟" ,
6565}
6666
67+ File : TypeAlias = int
68+ FILE_A : File = 0
69+ FILE_B : File = 1
70+ FILE_C : File = 2
71+ FILE_D : File = 3
72+ FILE_E : File = 4
73+ FILE_F : File = 5
74+ FILE_G : File = 6
75+ FILE_H : File = 7
76+ FILES = [FILE_A , FILE_B , FILE_C , FILE_D , FILE_E , FILE_F , FILE_G , FILE_H ]
6777FILE_NAMES = ["a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" ]
6878
79+ Rank : TypeAlias = int
80+ RANK_1 : Rank = 0
81+ RANK_2 : Rank = 1
82+ RANK_3 : Rank = 2
83+ RANK_4 : Rank = 3
84+ RANK_5 : Rank = 4
85+ RANK_6 : Rank = 5
86+ RANK_7 : Rank = 6
87+ RANK_8 : Rank = 7
88+ RANKS = [RANK_1 , RANK_2 , RANK_3 , RANK_4 , RANK_5 , RANK_6 , RANK_7 , RANK_8 ]
6989RANK_NAMES = ["1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" ]
7090
7191STARTING_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
@@ -251,15 +271,15 @@ def square_name(square: Square) -> str:
251271 """Gets the name of the square, like ``a3``."""
252272 return SQUARE_NAMES [square ]
253273
254- def square (file_index : int , rank_index : int ) -> Square :
274+ def square (file_index : File , rank_index : Rank ) -> Square :
255275 """Gets a square number by file and rank index."""
256276 return rank_index * 8 + file_index
257277
258- def square_file (square : Square ) -> int :
278+ def square_file (square : Square ) -> File :
259279 """Gets the file index of the square where ``0`` is the a-file."""
260280 return square & 7
261281
262- def square_rank (square : Square ) -> int :
282+ def square_rank (square : Square ) -> Rank :
263283 """Gets the rank index of the square where ``0`` is the first rank."""
264284 return square >> 3
265285
@@ -376,24 +396,24 @@ def square_mirror(square: Square) -> Square:
376396BB_LIGHT_SQUARES : Bitboard = 0x55aa_55aa_55aa_55aa
377397BB_DARK_SQUARES : Bitboard = 0xaa55_aa55_aa55_aa55
378398
379- BB_FILE_A : Bitboard = 0x0101_0101_0101_0101 << 0
380- BB_FILE_B : Bitboard = 0x0101_0101_0101_0101 << 1
381- BB_FILE_C : Bitboard = 0x0101_0101_0101_0101 << 2
382- BB_FILE_D : Bitboard = 0x0101_0101_0101_0101 << 3
383- BB_FILE_E : Bitboard = 0x0101_0101_0101_0101 << 4
384- BB_FILE_F : Bitboard = 0x0101_0101_0101_0101 << 5
385- BB_FILE_G : Bitboard = 0x0101_0101_0101_0101 << 6
386- BB_FILE_H : Bitboard = 0x0101_0101_0101_0101 << 7
399+ BB_FILE_A : Bitboard = 0x0101_0101_0101_0101 << FILE_A
400+ BB_FILE_B : Bitboard = 0x0101_0101_0101_0101 << FILE_B
401+ BB_FILE_C : Bitboard = 0x0101_0101_0101_0101 << FILE_C
402+ BB_FILE_D : Bitboard = 0x0101_0101_0101_0101 << FILE_D
403+ BB_FILE_E : Bitboard = 0x0101_0101_0101_0101 << FILE_E
404+ BB_FILE_F : Bitboard = 0x0101_0101_0101_0101 << FILE_F
405+ BB_FILE_G : Bitboard = 0x0101_0101_0101_0101 << FILE_G
406+ BB_FILE_H : Bitboard = 0x0101_0101_0101_0101 << FILE_H
387407BB_FILES : List [Bitboard ] = [BB_FILE_A , BB_FILE_B , BB_FILE_C , BB_FILE_D , BB_FILE_E , BB_FILE_F , BB_FILE_G , BB_FILE_H ]
388408
389- BB_RANK_1 : Bitboard = 0xff << (8 * 0 )
390- BB_RANK_2 : Bitboard = 0xff << (8 * 1 )
391- BB_RANK_3 : Bitboard = 0xff << (8 * 2 )
392- BB_RANK_4 : Bitboard = 0xff << (8 * 3 )
393- BB_RANK_5 : Bitboard = 0xff << (8 * 4 )
394- BB_RANK_6 : Bitboard = 0xff << (8 * 5 )
395- BB_RANK_7 : Bitboard = 0xff << (8 * 6 )
396- BB_RANK_8 : Bitboard = 0xff << (8 * 7 )
409+ BB_RANK_1 : Bitboard = 0xff << (8 * RANK_1 )
410+ BB_RANK_2 : Bitboard = 0xff << (8 * RANK_2 )
411+ BB_RANK_3 : Bitboard = 0xff << (8 * RANK_3 )
412+ BB_RANK_4 : Bitboard = 0xff << (8 * RANK_4 )
413+ BB_RANK_5 : Bitboard = 0xff << (8 * RANK_5 )
414+ BB_RANK_6 : Bitboard = 0xff << (8 * RANK_6 )
415+ BB_RANK_7 : Bitboard = 0xff << (8 * RANK_7 )
416+ BB_RANK_8 : Bitboard = 0xff << (8 * RANK_8 )
397417BB_RANKS : List [Bitboard ] = [BB_RANK_1 , BB_RANK_2 , BB_RANK_3 , BB_RANK_4 , BB_RANK_5 , BB_RANK_6 , BB_RANK_7 , BB_RANK_8 ]
398418
399419BB_BACKRANKS : Bitboard = BB_RANK_1 | BB_RANK_8
@@ -1847,7 +1867,7 @@ def generate_pseudo_legal_moves(self, from_mask: Bitboard = BB_ALL, to_mask: Bit
18471867 self .occupied_co [not self .turn ] & to_mask )
18481868
18491869 for to_square in scan_reversed (targets ):
1850- if square_rank (to_square ) in [0 , 7 ]:
1870+ if square_rank (to_square ) in [RANK_1 , RANK_8 ]:
18511871 yield Move (from_square , to_square , QUEEN )
18521872 yield Move (from_square , to_square , ROOK )
18531873 yield Move (from_square , to_square , BISHOP )
@@ -1870,7 +1890,7 @@ def generate_pseudo_legal_moves(self, from_mask: Bitboard = BB_ALL, to_mask: Bit
18701890 for to_square in scan_reversed (single_moves ):
18711891 from_square = to_square + (8 if self .turn == BLACK else - 8 )
18721892
1873- if square_rank (to_square ) in [0 , 7 ]:
1893+ if square_rank (to_square ) in [RANK_1 , RANK_8 ]:
18741894 yield Move (from_square , to_square , QUEEN )
18751895 yield Move (from_square , to_square , ROOK )
18761896 yield Move (from_square , to_square , BISHOP )
@@ -1897,7 +1917,7 @@ def generate_pseudo_legal_ep(self, from_mask: Bitboard = BB_ALL, to_mask: Bitboa
18971917 capturers = (
18981918 self .pawns & self .occupied_co [self .turn ] & from_mask &
18991919 BB_PAWN_ATTACKS [not self .turn ][self .ep_square ] &
1900- BB_RANKS [4 if self .turn else 3 ])
1920+ BB_RANKS [RANK_5 if self .turn else RANK_4 ])
19011921
19021922 for capturer in scan_reversed (capturers ):
19031923 yield Move (capturer , self .ep_square )
@@ -1977,9 +1997,9 @@ def is_pseudo_legal(self, move: Move) -> bool:
19771997 if piece != PAWN :
19781998 return False
19791999
1980- if self .turn == WHITE and square_rank (move .to_square ) != 7 :
2000+ if self .turn == WHITE and square_rank (move .to_square ) != RANK_8 :
19812001 return False
1982- elif self .turn == BLACK and square_rank (move .to_square ) != 0 :
2002+ elif self .turn == BLACK and square_rank (move .to_square ) != RANK_1 :
19832003 return False
19842004
19852005 # Handle castling.
@@ -2401,18 +2421,18 @@ def push(self, move: Move) -> None:
24012421 else :
24022422 self .castling_rights &= ~ BB_RANK_8
24032423 elif captured_piece_type == KING and not self .promoted & to_bb :
2404- if self .turn == WHITE and square_rank (move .to_square ) == 7 :
2424+ if self .turn == WHITE and square_rank (move .to_square ) == RANK_8 :
24052425 self .castling_rights &= ~ BB_RANK_8
2406- elif self .turn == BLACK and square_rank (move .to_square ) == 0 :
2426+ elif self .turn == BLACK and square_rank (move .to_square ) == RANK_1 :
24072427 self .castling_rights &= ~ BB_RANK_1
24082428
24092429 # Handle special pawn moves.
24102430 if piece_type == PAWN :
24112431 diff = move .to_square - move .from_square
24122432
2413- if diff == 16 and square_rank (move .from_square ) == 1 :
2433+ if diff == 16 and square_rank (move .from_square ) == RANK_2 :
24142434 self .ep_square = move .from_square + 8
2415- elif diff == - 16 and square_rank (move .from_square ) == 6 :
2435+ elif diff == - 16 and square_rank (move .from_square ) == RANK_7 :
24162436 self .ep_square = move .from_square - 8
24172437 elif move .to_square == ep_square and abs (diff ) in [7 , 9 ] and not captured_piece_type :
24182438 # Remove pawns captured en passant.
@@ -3605,11 +3625,11 @@ def _valid_ep_square(self) -> Optional[Square]:
36053625 return None
36063626
36073627 if self .turn == WHITE :
3608- ep_rank = 5
3628+ ep_rank = RANK_6
36093629 pawn_mask = shift_down (BB_SQUARES [self .ep_square ])
36103630 seventh_rank_mask = shift_up (BB_SQUARES [self .ep_square ])
36113631 else :
3612- ep_rank = 2
3632+ ep_rank = RANK_3
36133633 pawn_mask = shift_up (BB_SQUARES [self .ep_square ])
36143634 seventh_rank_mask = shift_down (BB_SQUARES [self .ep_square ])
36153635
0 commit comments