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(board, depth, alpha, beta, maximizing_player, depth2, printed_depths, position_count, memo, start_time, last_print_time):
- current_time = time.time()
- if current_time - last_print_time[0] >= 1:
- elapsed_hours, remainder = divmod(current_time - 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
- position_count[0] += 1
- if position_count[0] % 1000000 == 0:
- print(f"\nProzkoumano {position_count[0]} pozic.")
- key = (board.fen(), maximizing_player, depth, alpha, beta)
- if key in memo:
- return memo[key]
- if depth == 0 or board.is_game_over():
- eval = evaluate_board(board, depth2)
- memo[key] = ([], eval)
- return [], eval
- best_eval = float('-inf') if maximizing_player else float('inf')
- best_sequence = []
- for move in board.legal_moves:
- move_san = board.san(move)
- board.push(move)
- sequence, eval = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, printed_depths, position_count, memo, start_time, last_print_time)
- board.pop()
- if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
- best_eval = eval
- best_sequence = [(move, move_san)] + sequence
- if maximizing_player:
- alpha = max(alpha, eval)
- else:
- beta = min(beta, eval)
- if beta <= alpha:
- break
- memo[key] = (best_sequence, best_eval)
- if depth2 not in printed_depths:
- printed_depths.add(depth2)
- print(f"\nHloubka {depth2} prozkoumána, čas: {time.time() - start_time:.2f}s")
- return best_sequence, best_eval
- start_time = time.time()
- position_count = [0]
- memo = {}
- last_print_time = [start_time]
- printed_depths = set()
- start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
- board = chess.Board(start_fen)
- print("Počáteční šachovnice:")
- print(board)
- print("Počáteční FEN:", board.fen(), "\n")
- sequence, final_score = minimax(board, 12, float('-inf'), float('inf'), True, 0, printed_depths, position_count, memo, start_time, last_print_time)
- 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