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,T):
- # 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() or T:
- # eval = evaluate_board(board, depth2)
- # memo[key] = ([], eval)
- # T = True
- # 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, T)
- # 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
- # def minimax(board, depth, alpha, beta, maximizing_player, depth2, printed_depths, position_count, memo, start_time, last_print_time, T):
- # 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][0], memo[key][1], T
- # if depth == 0 or board.is_game_over():
- # eval = evaluate_board(board, depth2)
- # memo[key] = ([], eval)
- # if abs(eval) == 1000 - depth2: # Specificky kontroluje, zda je hodnocení rovno 1000 nebo -1000 s přihlédnutím k hloubce
- # return [], eval, True
- # return [], eval, T
- # best_eval = float('-inf') if maximizing_player else float('inf')
- # best_sequence = []
- # for move in board.legal_moves:
- # board.push(move)
- # sequence, eval, T = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, printed_depths, position_count, memo, start_time, last_print_time, T)
- # board.pop()
- # if T: # Pokud bylo T nastaveno na True v nějaké z rekurzí, předčasně ukončit všechny další
- # return [], eval, True
- # if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
- # best_eval = eval
- # best_sequence = [(move, board.san(move))] + 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, T
- # def minimax(board, depth, alpha, beta, maximizing_player, depth2, printed_depths, position_count, memo, start_time, last_print_time, T):
- # 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][0], memo[key][1], T
- # if depth == 0 or board.is_game_over():
- # eval = evaluate_board(board, depth2)
- # memo[key] = ([], eval)
- # return [], eval, T
- # best_eval = float('-inf') if maximizing_player else float('inf')
- # best_sequence = []
- # for move in board.legal_moves:
- # board.push(move)
- # sequence, eval, T = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, printed_depths, position_count, memo, start_time, last_print_time, T)
- # board.pop()
- # if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
- # best_eval = eval
- # best_sequence = [(move, board.san(move))] + sequence # Ujistěte se, že sekvence je správně aktualizována
- # 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, T
- def minimax(board, depth, alpha, beta, maximizing_player, depth2, printed_depths, position_count, memo, start_time, last_print_time, T):
- 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][0], memo[key][1], T
- if depth == 0 or board.is_game_over():
- eval = evaluate_board(board, depth2)
- memo[key] = ([], eval)
- T = T or abs(eval) >= 1000 # Nastavit T na True, pokud dosáhneme kritické eval hodnoty
- return [], eval, T
- best_eval = float('-inf') if maximizing_player else float('inf')
- best_sequence = []
- for move in board.legal_moves:
- board.push(move)
- sequence, eval, returned_T = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, printed_depths, position_count, memo, start_time, last_print_time, T)
- board.pop()
- # Nastavení T na True, pokud některá z rekurzí vrátí True
- if returned_T:
- T = True
- if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
- best_eval = eval
- best_sequence = [(move, board.san(move))] + sequence
- # Předčasné ukončení prohledávání, pokud T je True
- if T:
- memo[key] = (best_sequence, best_eval)
- return best_sequence, best_eval, T
- 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, T
- 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"
- #start_fen = "7K/8/k1P5/7p/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, T = minimax(board, 19, float('-inf'), float('inf'), True, 0, printed_depths, position_count, memo, start_time, last_print_time,T=False)
- 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