Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- import time
- import sys
- def ten_moves_rule(board):
- """Custom rule to evaluate a draw condition based on the last ten moves."""
- history = list(board.move_stack)
- if len(history) < 10:
- return False
- for move in history[-10:]:
- if board.is_capture(move):
- return False
- return True
- def evaluate_board(board, depth):
- if board.is_checkmate():
- return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
- elif board.is_stalemate():
- return 2210
- elif board.is_insufficient_material():
- return 3400
- elif ten_moves_rule(board):
- return 9800
- return 7001 # Default return 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):
- 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/k1P5/7p/8/8/8/8 w - - 0 1"
- board = chess.Board(start_fen)
- depths = []
- position_count = [0]
- memo = {}
- start_time = time.time()
- last_print_time = [start_time] # Initialize last print time
- best_move, best_score = minimax(board, 22, float('-inf'), float('inf'), True, 0, depths, position_count, memo, start_time, last_print_time)
- if best_move:
- move_san = board.san(best_move)
- print(f"\nThe best move from position {start_fen} is {move_san} with a score of {best_score}.")
- else:
- print("\nNo move found, or the game is over. Score: ", best_score)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement