Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- import time
- def ten_moves_rule(board):
- """Pravidlo pro vyhodnocení remízového stavu na základě posledních deseti tahů bez záběru nebo pohybu pěšce."""
- history = list(board.move_stack)
- if len(history) < 10:
- return False
- for move in history[-10:]:
- if board.is_capture(move) or board.piece_type_at(move.from_square) == chess.PAWN:
- return False
- return True
- def evaluate_board(board, depth):
- """Vyhodnoťte stav šachovnice pro rozhodování minimaxem."""
- if board.is_checkmate():
- return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
- elif board.is_stalemate():
- return 4
- elif board.is_insufficient_material():
- return 3
- elif ten_moves_rule(board):
- return 2
- return 1 # Výchozí heuristika, pokud žádná z výše uvedených podmínek není splněna
- def minimax(board, depth, alpha, beta, maximizing_player, position_count, memo, start_time, last_print_time, depth_limit):
- current_time = time.time()
- if current_time - start_time > 5: # Limit výpočtu na 5 sekund
- return [], 0 # Vrátí neutrální hodnotu, pokud vyprší čas
- position_count[0] += 1
- key = (board.fen(), maximizing_player, depth, alpha, beta)
- if key in memo:
- return memo[key]
- if board.is_game_over() or depth >= depth_limit:
- eval = evaluate_board(board, depth)
- 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)
- current_depth_limit = depth_limit if not board.is_check() else depth_limit + 1 # Zvyš hloubku, pokud je král v šachu
- sequence, eval = minimax(board, depth + 1, alpha, beta, not maximizing_player, position_count, memo, start_time, last_print_time, current_depth_limit)
- 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)
- return best_sequence, best_eval
- start_time = time.time()
- position_count = [0]
- memo = {}
- last_print_time = [start_time]
- depth_limit = 3 # Nastav počáteční omezení hloubky
- 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, 0, float('-inf'), float('inf'), True, position_count, memo, start_time, last_print_time, depth_limit)
- 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
Advertisement