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 4
- elif board.is_insufficient_material():
- return 3
- elif ten_moves_rule(board):
- return 2
- return 1 # Default heuristic if none of the above conditions are met
- def minimax(board, depth, alpha, beta, maximizing_player, depth2, depths, position_count, memo, start_time, last_print_time):
- """Minimax algorithm with alpha-beta pruning."""
- 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] = (None, eval)
- return None, eval
- best_move = None
- if maximizing_player:
- max_eval = float('-inf')
- for move in board.legal_moves:
- board.push(move)
- _, eval = minimax(board, depth - 1, alpha, beta, False, depth2 + 1, depths, position_count, memo, start_time, last_print_time)
- board.pop()
- if eval > max_eval:
- max_eval = eval
- best_move = move
- alpha = max(alpha, eval)
- if beta <= alpha:
- break
- memo[key] = (best_move, max_eval)
- return best_move, max_eval
- else:
- min_eval = float('inf')
- for move in board.legal_moves:
- board.push(move)
- _, eval = minimax(board, depth - 1, alpha, beta, True, depth2 + 1, depths, position_count, memo, start_time, last_print_time)
- board.pop()
- if eval < min_eval:
- min_eval = eval
- best_move = move
- beta = min(beta, eval)
- if beta <= alpha:
- break
- memo[key] = (best_move, min_eval)
- return best_move, min_eval
- # Initialization and main execution logic
- start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
- start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
- board = chess.Board(start_fen)
- # Tisk počáteční šachovnice a jejího FEN
- print("Počáteční šachovnice:")
- print(board)
- print("Počáteční FEN:", board.fen(), "\n")
- sequence = []
- depths = []
- position_count = [0]
- memo = {}
- start_time = time.time()
- last_print_time = [start_time] # Initialize last print time
- move_sequence = []
- best_move, best_score = minimax(board, 3, float('-inf'), float('inf'), True, 0, depths, position_count, memo, start_time, last_print_time)
- while best_move:
- move_sequence.append(best_move)
- board.push(best_move)
- print(board, "\nFEN:", board.fen(), "\n") # Printing each board state with FEN
- if board.is_game_over():
- break
- best_move, _ = minimax(board, 5, float('-inf'), float('inf'), board.turn, 0, depths, position_count, memo, start_time, last_print_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement