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][0], memo[key][1], T
- if depth == 0 or board.is_game_over():
- eval = evaluate_board(board, depth2)
- memo[key] = ([], eval)
- T = T or eval != 4
- return [], eval, T
- best_eval = float('-inf') if maximizing_player else float('inf')
- best_sequence = []
- all_true = True
- 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()
- if maximizing_player:
- if returned_T: # Okamžitý návrat pokud najdeme vítěznou větev
- return [(move, board.san(move))] + sequence, eval, True
- if eval > best_eval:
- best_eval = eval
- best_sequence = [(move, board.san(move))] + sequence
- alpha = max(alpha, eval)
- else:
- all_true = all_true and returned_T
- if eval < best_eval:
- best_eval = eval
- best_sequence = [(move, board.san(move))] + sequence
- beta = min(beta, eval)
- if beta <= alpha:
- break
- if not maximizing_player and all_true:
- return best_sequence, best_eval, True # Pro minimalizujícího hráče, pokud jsou všechny tahy nevyhnutelně prohrané
- 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"
- 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, 26, 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, T)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement