Advertisement
max2201111

Reti endgame

Feb 18th, 2024
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | Science | 0 0
  1. import chess
  2.  
  3. def initialize_game_tree(initial_fen, stronger, seeking_draw):
  4.     """
  5.    Initializes the game tree with the root node, specifying who is stronger and who is seeking a draw.
  6.    """
  7.     return {
  8.         1: {
  9.             'fen': initial_fen,
  10.             'moves_to_mate': None,
  11.             'parent': None,
  12.             'color': chess.WHITE if initial_fen.split()[1] == 'w' else chess.BLACK,
  13.             'result': None,
  14.             'processed': False,
  15.             'sequence': [],
  16.             'up': False,
  17.             'stronger': stronger,
  18.             'seeking_draw': seeking_draw
  19.         }
  20.     }
  21.  
  22. depthh =0
  23.  
  24. def generate_descendants(game_tree, key, depth, max_depth=11):
  25.     """
  26.    Recursively generates descendant nodes up to a specified depth, considering all legal moves.
  27.    """
  28.     global depthh
  29.     if depthh < depth:
  30.         print(depth)
  31.         depthh = depth
  32.        
  33.     if depth >= max_depth:
  34.         return
  35.    
  36.     node = game_tree[key]
  37.     board = chess.Board(node['fen'])
  38.    
  39.     for move in board.legal_moves:
  40.         board.push(move)
  41.         next_fen = board.fen()
  42.         new_key = max(game_tree.keys()) + 1
  43.         game_tree[new_key] = {
  44.             'fen': next_fen,
  45.             'moves_to_mate': None,
  46.             'parent': key,
  47.             'color': chess.WHITE if board.turn else chess.BLACK,
  48.             'result': None,
  49.             'processed': False,
  50.             'sequence': node['sequence'] + [move.uci()],
  51.             'up': False,
  52.             'stronger': node['stronger'],
  53.             'seeking_draw': node['seeking_draw']
  54.         }
  55.         board.pop()
  56.         generate_descendants(game_tree, new_key, depth + 1, max_depth)
  57.  
  58. def evaluate_terminal_positions(game_tree):
  59.     """
  60.    Evaluates terminal positions to identify wins, losses, and draws, considering who is seeking a draw.
  61.    """
  62.     for key, node in game_tree.items():
  63.         board = chess.Board(node['fen'])
  64.         if board.is_checkmate():
  65.             # A win is marked as 1 for the player not seeking a draw if they're stronger.
  66.             node['result'] = 1 if node['color'] != node['stronger'] and not node['seeking_draw'][node['color']] else 0
  67.             node['processed'] = True
  68.         elif board.is_stalemate() or board.is_insufficient_material() or board.can_claim_draw():
  69.             node['result'] = 0.5  # Draw
  70.             node['processed'] = True
  71.  
  72. def propagate_results_upwards(game_tree):
  73.     """
  74.    Propagates the evaluation results upwards through the game tree, considering seeking_draw.
  75.    """
  76.     for key in sorted(game_tree.keys(), reverse=True):
  77.         node = game_tree[key]
  78.         if node['parent'] is None or node['processed']:
  79.             continue
  80.  
  81.         parent_key = node['parent']
  82.         parent_node = game_tree[parent_key]
  83.         if parent_node['up']:
  84.             continue
  85.  
  86.         children = [game_tree[k] for k in game_tree if game_tree[k]['parent'] == parent_key and game_tree[k]['processed']]
  87.        
  88.         if any(child['result'] == 1 for child in children if child['color'] != parent_node['color']):
  89.             parent_node['result'] = 1
  90.         elif all(child['result'] == 0.5 for child in children):
  91.             parent_node['result'] = 0.5
  92.         else:
  93.             parent_node['result'] = 0 if parent_node['color'] == parent_node['stronger'] else 1
  94.  
  95.         parent_node['processed'] = True
  96.         parent_node['up'] = True
  97.  
  98. def main():
  99.     initial_fen = "7K/8/k1P5/7p/8/8/8/8 w - - 0 1"
  100.     stronger = chess.BLACK  # Black is considered the stronger side
  101.     seeking_draw = {chess.WHITE: True, chess.BLACK: False}  # White is seeking a draw
  102.    
  103.     game_tree = initialize_game_tree(initial_fen, stronger, seeking_draw)
  104.     generate_descendants(game_tree, 1, 0)
  105.     evaluate_terminal_positions(game_tree)
  106.     propagate_results_upwards(game_tree)
  107.  
  108.     # Print the result at the root node after propagation
  109.     root = game_tree[1]
  110.     print(f"Root Node: Result: {root['result']}, Processed: {root['processed']}")
  111.  
  112. if __name__ == "__main__":
  113.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement