Advertisement
max2201111

maybe puvodni

Apr 12th, 2024
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | Science | 0 0
  1. import chess
  2.  
  3. def simplify_fen_string(fen):
  4.     """Simplify the FEN string to just include the position of pieces without turn, castling, etc."""
  5.     parts = fen.split(' ')
  6.     simplified_fen = ' '.join(parts[:4])
  7.     return simplified_fen
  8.  
  9. def evaluate_position(board):
  10.     """Evaluate the board position to determine if it's a checkmate, stalemate, or ongoing game."""
  11.     if board.is_checkmate():
  12.         # Determine who is in checkmate
  13.         if board.turn == chess.WHITE:
  14.             return -1000  # White is checkmated, black wins
  15.         else:
  16.             return 1000   # Black is checkmated, white wins
  17.     elif board.is_stalemate() or board.is_insufficient_material() or board.can_claim_draw():
  18.         return 0  # Game is a draw
  19.     return None  # Game is still ongoing
  20.  
  21. def create_AR_entry(fen, result, parent):
  22.     """Create an entry for the analysis record (AR)."""
  23.     return {
  24.         'fen': fen,
  25.         'parent': parent,
  26.         'children': [],
  27.         'result': result,
  28.         'sequence': []
  29.     }
  30.  
  31. def generate_positions(board, AR, parent_fen):
  32.     """Generate all possible game positions from the current board state."""
  33.     current_fen = board.fen()
  34.     if len(AR) % 100000 == 0:
  35.         print(len(AR))
  36.     if current_fen in AR:
  37.         return  # Avoid processing the same position twice
  38.  
  39.     result = evaluate_position(board)
  40.     AR[current_fen] = create_AR_entry(simplify_fen_string(current_fen), result, parent_fen)
  41.  
  42.     if parent_fen:
  43.         AR[parent_fen]['children'].append(current_fen)
  44.  
  45.     if result is not None:
  46.         return  # Stop further generation if the game has ended
  47.  
  48.     for move in board.legal_moves:
  49.         board.push(move)
  50.         generate_positions(board, AR, current_fen)
  51.         board.pop()
  52.  
  53. def back_propagation(AR):
  54.     """Propagate the results from the leaf nodes to the root based on the analysis record."""
  55.     for fen in AR:
  56.         node = AR[fen]
  57.         if node['result'] is not None and node['parent']:
  58.             propagate_upwards(AR, fen)
  59.  
  60. def propagate_upwards(AR, child_fen):
  61.     """Recursively update parent nodes based on the results of child nodes."""
  62.     child_node = AR[child_fen]
  63.     parent_fen = child_node['parent']
  64.  
  65.     if AR[initial_fen]['result'] is not None:
  66.         return
  67.    
  68.     while parent_fen:
  69.         parent_node = AR[parent_fen]
  70.         if parent_node['result'] is None or abs(child_node['result']) < abs(parent_node['result']):
  71.             parent_node['result'] = -child_node['result']
  72.             parent_node['sequence'] = [child_fen] + child_node['sequence']
  73.         child_fen = parent_fen
  74.         child_node = parent_node
  75.         parent_fen = child_node['parent']
  76.  
  77. def main():
  78.     initial_fen = "8/8/8/8/3Q4/5K2/8/4k3 w - - 0 1"
  79.     board = chess.Board(initial_fen)
  80.     AR = {}
  81.     generate_positions(board, AR, None)
  82.     back_propagation(AR)
  83.  
  84.     # Output results
  85.     initial_entry = AR[initial_fen]
  86.     print(f"Result for initial position: {initial_entry['result']}")
  87.     print("Optimal sequence of moves:")
  88.     for fen in initial_entry['sequence']:
  89.         print(chess.Board(fen))
  90.         print()
  91.  
  92. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement