Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- from collections import deque
- import hashlib
- def simplify_fen_string(fen):
- parts = fen.split(' ')
- simplified_fen = ' '.join(parts[:4]) # Only includes position information
- return simplified_fen
- def fen_hash(fen):
- return hashlib.md5(fen.encode('utf-8')).hexdigest()
- def evaluate_position(board, depth):
- if board.is_checkmate():
- return 1000 - depth if board.turn == chess.WHITE else -1000 + depth
- if board.is_stalemate() or board.is_insufficient_material() or board.is_seventyfive_moves() or board.is_fivefold_repetition() or board.is_variant_draw():
- return 0
- return None
- def is_white_turn(fen):
- return 'w' in fen.split(' ')[1]
- def propagate_results(AR):
- changed = True
- while changed:
- changed = False
- for fen in AR:
- if 'children' in AR[fen] and any(AR[child]['result'] is not None for child in AR[fen]['children']):
- if is_white_turn(fen):
- candidates = [(child, AR[child]['result']) for child in AR[fen]['children'] if AR[child]['result'] is not None and AR[child]['result'] < 0]
- if candidates:
- fen_ch, min_negative_score = min(candidates, key=lambda x: x[1])
- new_result = 1001 + min_negative_score
- if AR[fen]['result'] != new_result:
- AR[fen]['result'] = new_result
- AR[fen]['sequence'] = [fen_ch]
- changed = True
- else:
- candidates = [(child, AR[child]['result']) for child in AR[fen]['children'] if AR[child]['result'] is not None and AR[child]['result'] > 0]
- if candidates:
- fen_ch, max_positive_score = max(candidates, key=lambda x: x[1])
- new_result = -1001 + max_positive_score
- if AR[fen]['result'] != new_result:
- AR[fen]['result'] = new_result
- AR[fen]['sequence'] = [fen_ch]
- changed = True
- def generate_moves(initial_board):
- queue = deque()
- initial_fen = simplify_fen_string(initial_board.fen())
- queue.append((initial_board.copy(), initial_fen, None, 0))
- transposition_table = {}
- node_count = 0
- explored_depths = set()
- while queue:
- current_board, current_fen, parent_fen, depth = queue.popleft()
- fen_key = fen_hash(current_fen)
- if fen_key not in transposition_table:
- result = evaluate_position(current_board, depth)
- transposition_table[fen_key] = result
- AR[current_fen] = {
- 'fen': current_fen,
- 'parent': parent_fen,
- 'color': chess.WHITE if current_board.turn == chess.WHITE else chess.BLACK,
- 'children': [],
- 'result': result,
- 'depth': depth
- }
- if parent_fen:
- AR[parent_fen]['children'].append(current_fen)
- if depth not in explored_depths:
- print(f"New depth reached: {depth}")
- explored_depths.add(depth)
- if result is None:
- for move in current_board.legal_moves:
- current_board.push(move)
- new_fen = simplify_fen_string(current_board.fen())
- if fen_hash(new_fen) not in transposition_table:
- queue.append((current_board.copy(), new_fen, current_fen, depth + 1))
- current_board.pop()
- # Initialize the chess board and the AR dictionary
- initial_fen = "1k6/3K1Q2/8/8/8/8/8/8 w - - 0 1"
- board = chess.Board(initial_fen)
- AR = {}
- # Start generating moves and propagating results
- generate_moves(board)
- propagate_results(AR) # Pass the AR dictionary as an argument
- # Print current AR data
- def print_current_AR_data():
- for key in sorted(AR, key=lambda k: AR[k]['depth']):
- node = AR[key]
- if node['depth'] <= 3: # Display up to a certain depth
- print(f"Depth {node['depth']} {key}: Result={node['result']} Children={len(node['children'])}")
- print_current_AR_data()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement