Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- import time
- def ten_moves_rule(board):
- """Custom rule to evaluate a draw condition based on the last ten moves, considering no captures or pawn moves."""
- history = list(board.move_stack)
- if len(history) < 10:
- return False
- for move in history[-10:]:
- if board.is_capture(move):
- return False
- if board.piece_type_at(move.from_square) == chess.PAWN:
- return False
- return True
- def evaluate_board(board, depth):
- """Evaluate the board state for minimax decision-making."""
- if board.is_checkmate():
- return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
- elif board.is_stalemate():
- return 1
- elif board.is_insufficient_material():
- return 2
- elif ten_moves_rule(board):
- return 3
- return 4 # Default heuristic if none of the above conditions are met
- def minimax(node, AR, depth, alpha, beta, maximizing_player, memo):
- current_time = time.time()
- last_print_time = AR['last_print_time']
- if current_time - last_print_time[0] >= 1:
- elapsed_hours, remainder = divmod(current_time - AR['start_time'], 3600)
- elapsed_minutes, elapsed_seconds = divmod(remainder, 60)
- print(f"\r{int(elapsed_hours):02d}h {int(elapsed_minutes):02d}m {int(elapsed_seconds):02d}s", end='', flush=True)
- last_print_time[0] = current_time
- AR['position_count'][0] += 1
- if AR['position_count'][0] % 1000000 == 0:
- print(f"\nProzkoumano {AR['position_count'][0]} pozic.")
- board = chess.Board(AR[node]['fen'])
- key = (node, maximizing_player, depth, alpha, beta)
- if key in memo:
- return memo[key][0], memo[key][1]
- if depth == 0 or board.is_game_over():
- eval = evaluate_board(board, AR[node]['depth'])
- memo[key] = ([], eval)
- return [], eval
- best_eval = float('-inf') if maximizing_player else float('inf')
- best_sequence = []
- for move in board.legal_moves:
- board.push(move)
- next_node = board.fen()
- if next_node not in AR:
- AR[next_node] = {
- 'fen': next_node,
- 'parent': node,
- 'color': chess.WHITE if board.turn else chess.BLACK,
- 'children': [],
- 'result': None,
- 'depth': AR[node]['depth'] + 1,
- 'score': None
- }
- sequence, eval = minimax(next_node, AR, depth - 1, alpha, beta, not maximizing_player, memo)
- board.pop()
- if maximizing_player:
- if eval > best_eval:
- best_eval = eval
- best_sequence = [(move, board.san(move))] + sequence
- alpha = max(alpha, eval)
- if beta <= alpha:
- break
- else:
- if eval < best_eval:
- best_eval = eval
- best_sequence = [(move, board.san(move))] + sequence
- beta = min(beta, eval)
- if beta <= alpha:
- break
- memo[key] = (best_sequence, best_eval)
- return best_sequence, best_eval
- start_time = time.time()
- AR = {
- 'start_time': start_time,
- 'position_count': [0],
- 'last_print_time': [start_time]
- }
- memo = {}
- start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
- board = chess.Board(start_fen)
- AR[start_fen] = {
- 'fen': start_fen,
- 'parent': None,
- 'color': chess.WHITE if board.turn == chess.WHITE else chess.BLACK,
- 'children': [],
- 'result': None,
- 'depth': 0,
- 'score': None
- }
- print("Počáteční šachovnice:")
- print(board)
- print("Počáteční FEN:", board.fen(), "\n")
- sequence, final_score = minimax(start_fen, AR, 9, float('-inf'), float('inf'), True, memo)
- print("\n\nOptimal move sequence:")
- for move, san in sequence:
- print("Move:", san)
- board.push(move)
- print("Board:\n", board)
- print("FEN:", board.fen())
- print("Evaluation:", evaluate_board(board, 0), "\n")
- print("Final evaluation score:", final_score)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement