@@ -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,41 @@ 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 parse_file (name : str ) -> File :
279+ """
280+ Gets the file index for the given file *name*
281+ (e.g., ``a`` returns ``0``).
282+
283+ :raises: :exc:`ValueError` if the file name is invalid.
284+ """
285+ return FILE_NAMES .index (name )
286+
287+ def file_name (file : File ) -> str :
288+ """Gets the name of the file, like ``a``."""
289+ return FILE_NAMES [file ]
290+
291+ def parse_rank (name : str ) -> File :
292+ """
293+ Gets the rank index for the given rank *name*
294+ (e.g., ``1`` returns ``0``).
295+
296+ :raises: :exc:`ValueError` if the rank name is invalid.
297+ """
298+ return FILE_NAMES .index (name )
299+
300+ def rank_name (rank : Rank ) -> str :
301+ """Gets the name of the rank, like ``1``."""
302+ return FILE_NAMES [rank ]
303+
304+ def square_file (square : Square ) -> File :
259305 """Gets the file index of the square where ``0`` is the a-file."""
260306 return square & 7
261307
262- def square_rank (square : Square ) -> int :
308+ def square_rank (square : Square ) -> Rank :
263309 """Gets the rank index of the square where ``0`` is the first rank."""
264310 return square >> 3
265311
@@ -376,24 +422,24 @@ def square_mirror(square: Square) -> Square:
376422BB_LIGHT_SQUARES : Bitboard = 0x55aa_55aa_55aa_55aa
377423BB_DARK_SQUARES : Bitboard = 0xaa55_aa55_aa55_aa55
378424
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
425+ BB_FILE_A : Bitboard = 0x0101_0101_0101_0101 << FILE_A
426+ BB_FILE_B : Bitboard = 0x0101_0101_0101_0101 << FILE_B
427+ BB_FILE_C : Bitboard = 0x0101_0101_0101_0101 << FILE_C
428+ BB_FILE_D : Bitboard = 0x0101_0101_0101_0101 << FILE_D
429+ BB_FILE_E : Bitboard = 0x0101_0101_0101_0101 << FILE_E
430+ BB_FILE_F : Bitboard = 0x0101_0101_0101_0101 << FILE_F
431+ BB_FILE_G : Bitboard = 0x0101_0101_0101_0101 << FILE_G
432+ BB_FILE_H : Bitboard = 0x0101_0101_0101_0101 << FILE_H
387433BB_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 ]
388434
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 )
435+ BB_RANK_1 : Bitboard = 0xff << (8 * RANK_1 )
436+ BB_RANK_2 : Bitboard = 0xff << (8 * RANK_2 )
437+ BB_RANK_3 : Bitboard = 0xff << (8 * RANK_3 )
438+ BB_RANK_4 : Bitboard = 0xff << (8 * RANK_4 )
439+ BB_RANK_5 : Bitboard = 0xff << (8 * RANK_5 )
440+ BB_RANK_6 : Bitboard = 0xff << (8 * RANK_6 )
441+ BB_RANK_7 : Bitboard = 0xff << (8 * RANK_7 )
442+ BB_RANK_8 : Bitboard = 0xff << (8 * RANK_8 )
397443BB_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 ]
398444
399445BB_BACKRANKS : Bitboard = BB_RANK_1 | BB_RANK_8
@@ -1847,7 +1893,7 @@ def generate_pseudo_legal_moves(self, from_mask: Bitboard = BB_ALL, to_mask: Bit
18471893 self .occupied_co [not self .turn ] & to_mask )
18481894
18491895 for to_square in scan_reversed (targets ):
1850- if square_rank (to_square ) in [0 , 7 ]:
1896+ if square_rank (to_square ) in [RANK_1 , RANK_8 ]:
18511897 yield Move (from_square , to_square , QUEEN )
18521898 yield Move (from_square , to_square , ROOK )
18531899 yield Move (from_square , to_square , BISHOP )
@@ -1870,7 +1916,7 @@ def generate_pseudo_legal_moves(self, from_mask: Bitboard = BB_ALL, to_mask: Bit
18701916 for to_square in scan_reversed (single_moves ):
18711917 from_square = to_square + (8 if self .turn == BLACK else - 8 )
18721918
1873- if square_rank (to_square ) in [0 , 7 ]:
1919+ if square_rank (to_square ) in [RANK_1 , RANK_8 ]:
18741920 yield Move (from_square , to_square , QUEEN )
18751921 yield Move (from_square , to_square , ROOK )
18761922 yield Move (from_square , to_square , BISHOP )
@@ -1897,7 +1943,7 @@ def generate_pseudo_legal_ep(self, from_mask: Bitboard = BB_ALL, to_mask: Bitboa
18971943 capturers = (
18981944 self .pawns & self .occupied_co [self .turn ] & from_mask &
18991945 BB_PAWN_ATTACKS [not self .turn ][self .ep_square ] &
1900- BB_RANKS [4 if self .turn else 3 ])
1946+ BB_RANKS [RANK_5 if self .turn else RANK_4 ])
19011947
19021948 for capturer in scan_reversed (capturers ):
19031949 yield Move (capturer , self .ep_square )
@@ -1977,9 +2023,9 @@ def is_pseudo_legal(self, move: Move) -> bool:
19772023 if piece != PAWN :
19782024 return False
19792025
1980- if self .turn == WHITE and square_rank (move .to_square ) != 7 :
2026+ if self .turn == WHITE and square_rank (move .to_square ) != RANK_8 :
19812027 return False
1982- elif self .turn == BLACK and square_rank (move .to_square ) != 0 :
2028+ elif self .turn == BLACK and square_rank (move .to_square ) != RANK_1 :
19832029 return False
19842030
19852031 # Handle castling.
@@ -2401,18 +2447,18 @@ def push(self, move: Move) -> None:
24012447 else :
24022448 self .castling_rights &= ~ BB_RANK_8
24032449 elif captured_piece_type == KING and not self .promoted & to_bb :
2404- if self .turn == WHITE and square_rank (move .to_square ) == 7 :
2450+ if self .turn == WHITE and square_rank (move .to_square ) == RANK_8 :
24052451 self .castling_rights &= ~ BB_RANK_8
2406- elif self .turn == BLACK and square_rank (move .to_square ) == 0 :
2452+ elif self .turn == BLACK and square_rank (move .to_square ) == RANK_1 :
24072453 self .castling_rights &= ~ BB_RANK_1
24082454
24092455 # Handle special pawn moves.
24102456 if piece_type == PAWN :
24112457 diff = move .to_square - move .from_square
24122458
2413- if diff == 16 and square_rank (move .from_square ) == 1 :
2459+ if diff == 16 and square_rank (move .from_square ) == RANK_2 :
24142460 self .ep_square = move .from_square + 8
2415- elif diff == - 16 and square_rank (move .from_square ) == 6 :
2461+ elif diff == - 16 and square_rank (move .from_square ) == RANK_7 :
24162462 self .ep_square = move .from_square - 8
24172463 elif move .to_square == ep_square and abs (diff ) in [7 , 9 ] and not captured_piece_type :
24182464 # Remove pawns captured en passant.
@@ -3605,11 +3651,11 @@ def _valid_ep_square(self) -> Optional[Square]:
36053651 return None
36063652
36073653 if self .turn == WHITE :
3608- ep_rank = 5
3654+ ep_rank = RANK_6
36093655 pawn_mask = shift_down (BB_SQUARES [self .ep_square ])
36103656 seventh_rank_mask = shift_up (BB_SQUARES [self .ep_square ])
36113657 else :
3612- ep_rank = 2
3658+ ep_rank = RANK_3
36133659 pawn_mask = shift_up (BB_SQUARES [self .ep_square ])
36143660 seventh_rank_mask = shift_down (BB_SQUARES [self .ep_square ])
36153661
0 commit comments