Advertisement
Guest User

mates.py

a guest
Apr 8th, 2025
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | Source Code | 0 0
  1. """
  2. Generates 1-ply checkmates for Black in initial 2x2 positions with White's
  3. King-11. May not generate all possible positions.
  4. """
  5.  
  6. import cshogi
  7.  
  8. # First, generates possible SFEN strings. Then, extracts those that are 1-ply
  9. # checkmate. Note that the output may include illegal positions (e.g., Two
  10. # Pawns).
  11.  
  12. # Possible White pieces in initial positions. Promoted pieces are excluded.
  13. white_pieces: list[str] = ["p", "l", "n", "s", "g", "b", "r"]
  14.  
  15. # Possible SFEN patterns for a particular square or rank. Pieces with No Moves
  16. # and pieces that give a check are excluded.
  17. square21_pieces: list[str] = ["S", "B", *white_pieces]
  18. rank1_patterns: list[str] = [
  19.     "8k",
  20.     *(f"7{x}k" for x in square21_pieces),
  21. ]
  22.  
  23. square12_pieces: list[str] = ["B", *white_pieces]
  24. square22_pieces: list[str] = ["P", "L", "R", *white_pieces]
  25. rank2_patterns: list[str] = [
  26.     "9",
  27.     *(f"8{x}" for x in square12_pieces),
  28.     *(f"7{x}{y}" for x in square22_pieces for y in ["1", *square12_pieces]),
  29. ]
  30.  
  31. # Assumes Black has all pieces in hand.
  32. pieces_in_hand: dict[str, int] = {"R": 2, "B": 2, "G": 4, "S": 4, "N": 4, "L": 4, "P": 18}
  33.  
  34.  
  35. def get_pieces_in_hand_field(board_field: str) -> str:
  36.     """
  37.    Generates the SFEN field for the pieces in hand, with the pieces on the
  38.    given board subtracted.
  39.    """
  40.     field: str = ""
  41.  
  42.     for piece, count in pieces_in_hand.items():
  43.         remaining: int = count - board_field.upper().count(piece)
  44.  
  45.         # Too many pieces of one type on the board (e.g., three rooks).
  46.         if remaining < 0:
  47.             raise ValueError
  48.  
  49.         if remaining > 0:
  50.             if remaining > 1:
  51.                 field += str(remaining)
  52.  
  53.             field += piece
  54.  
  55.     return field
  56.  
  57.  
  58. board: cshogi.Board = cshogi.Board()
  59.  
  60. # If White is not checkmated after 1 ply, stops the search (i.e., only finds a
  61. # 1-ply checkmate).
  62. dfpn: cshogi.DfPn = cshogi.DfPn(draw_ply=1)
  63.  
  64. for rank1_pattern in rank1_patterns:
  65.     for rank2_pattern in rank2_patterns:
  66.         board_field: str = f"{rank1_pattern}/{rank2_pattern}/9/9/9/9/9/9/9"
  67.  
  68.         try:
  69.             pieces_in_hand_field: str = get_pieces_in_hand_field(board_field)
  70.         except ValueError:
  71.             # Skips the position with too many pieces of one type.
  72.             continue
  73.  
  74.         sfen: str = f"{board_field} b {pieces_in_hand_field} 1"
  75.         board.set_sfen(sfen)
  76.  
  77.         # Displays the position if a checkmate is found.
  78.         if dfpn.search(board):
  79.             print(board.to_bod())
  80.             print(sfen)
  81.             print()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement