Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- def initialize_game_tree(initial_fen, stronger, seeking_draw):
- """
- Initializes the game tree with the root node, specifying who is stronger and who is seeking a draw.
- """
- return {
- 1: {
- 'fen': initial_fen,
- 'moves_to_mate': None,
- 'parent': None,
- 'color': chess.WHITE if initial_fen.split()[1] == 'w' else chess.BLACK,
- 'result': None,
- 'processed': False,
- 'sequence': [],
- 'up': False,
- 'stronger': stronger,
- 'seeking_draw': seeking_draw
- }
- }
- depthh =0
- def generate_descendants(game_tree, key, depth, max_depth=11):
- """
- Recursively generates descendant nodes up to a specified depth, considering all legal moves.
- """
- global depthh
- if depthh < depth:
- print(depth)
- depthh = depth
- if depth >= max_depth:
- return
- node = game_tree[key]
- board = chess.Board(node['fen'])
- for move in board.legal_moves:
- board.push(move)
- next_fen = board.fen()
- new_key = max(game_tree.keys()) + 1
- game_tree[new_key] = {
- 'fen': next_fen,
- 'moves_to_mate': None,
- 'parent': key,
- 'color': chess.WHITE if board.turn else chess.BLACK,
- 'result': None,
- 'processed': False,
- 'sequence': node['sequence'] + [move.uci()],
- 'up': False,
- 'stronger': node['stronger'],
- 'seeking_draw': node['seeking_draw']
- }
- board.pop()
- generate_descendants(game_tree, new_key, depth + 1, max_depth)
- def evaluate_terminal_positions(game_tree):
- """
- Evaluates terminal positions to identify wins, losses, and draws, considering who is seeking a draw.
- """
- for key, node in game_tree.items():
- board = chess.Board(node['fen'])
- if board.is_checkmate():
- # A win is marked as 1 for the player not seeking a draw if they're stronger.
- node['result'] = 1 if node['color'] != node['stronger'] and not node['seeking_draw'][node['color']] else 0
- node['processed'] = True
- elif board.is_stalemate() or board.is_insufficient_material() or board.can_claim_draw():
- node['result'] = 0.5 # Draw
- node['processed'] = True
- def propagate_results_upwards(game_tree):
- """
- Propagates the evaluation results upwards through the game tree, considering seeking_draw.
- """
- for key in sorted(game_tree.keys(), reverse=True):
- node = game_tree[key]
- if node['parent'] is None or node['processed']:
- continue
- parent_key = node['parent']
- parent_node = game_tree[parent_key]
- if parent_node['up']:
- continue
- children = [game_tree[k] for k in game_tree if game_tree[k]['parent'] == parent_key and game_tree[k]['processed']]
- if any(child['result'] == 1 for child in children if child['color'] != parent_node['color']):
- parent_node['result'] = 1
- elif all(child['result'] == 0.5 for child in children):
- parent_node['result'] = 0.5
- else:
- parent_node['result'] = 0 if parent_node['color'] == parent_node['stronger'] else 1
- parent_node['processed'] = True
- parent_node['up'] = True
- def main():
- initial_fen = "7K/8/k1P5/7p/8/8/8/8 w - - 0 1"
- stronger = chess.BLACK # Black is considered the stronger side
- seeking_draw = {chess.WHITE: True, chess.BLACK: False} # White is seeking a draw
- game_tree = initialize_game_tree(initial_fen, stronger, seeking_draw)
- generate_descendants(game_tree, 1, 0)
- evaluate_terminal_positions(game_tree)
- propagate_results_upwards(game_tree)
- # Print the result at the root node after propagation
- root = game_tree[1]
- print(f"Root Node: Result: {root['result']}, Processed: {root['processed']}")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement