Advertisement
max2201111

tak toto ano: >3 #

May 12th, 2024
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | Science | 0 0
  1. import chess
  2. from itertools import permutations
  3.  
  4. def generate_chess_positions(pieces):
  5.     board_size = 8
  6.     all_squares = [(i, j) for i in range(board_size) for j in range(board_size)]
  7.     for squares in permutations(all_squares, len(pieces)):
  8.         board = chess.Board(None)
  9.         for piece, (row, col) in zip(pieces, squares):
  10.             square_index = row * 8 + col  # Convert from (row, col) to square index
  11.             board.set_piece_at(square_index, chess.Piece.from_symbol(piece))
  12.             yield board.fen()
  13.  
  14. def simplify_fen_string(fen):
  15.     parts = fen.split(' ')
  16.     simplified_fen = ' '.join(parts[:4])  # Keeping only the position information
  17.     return simplified_fen
  18.  
  19. def evaluate_board(board):
  20.     if board.is_checkmate():
  21.         return 1000 if board.turn == chess.BLACK else -1000
  22.     elif board.is_stalemate() or board.is_insufficient_material():
  23.         return 0
  24.     return 4  # Default heuristic
  25.  
  26. def minimax(board, depth, alpha, beta, maximizing_player, node_count, completed_depths):
  27.     node_count[0] += 1
  28.     if node_count[0] % 1000000 == 0:
  29.         print(f"Nodes explored: {node_count[0]}")
  30.  
  31.     if board.is_game_over():
  32.         if depth not in completed_depths:
  33.             print(f"Depth {depth} completed for the first time.")
  34.             completed_depths.add(depth)
  35.         return [], evaluate_board(board)
  36.  
  37.     if depth > 3:  # Practical limit to prevent infinite recursion
  38.         if depth not in completed_depths:
  39.             print(f"Depth {depth} completed for the first time.")
  40.             completed_depths.add(depth)
  41.         return [], evaluate_board(board)
  42.  
  43.     best_eval = float('-inf') if maximizing_player else float('inf')
  44.     best_sequence = []
  45.  
  46.     for move in board.legal_moves:
  47.         board.push(move)
  48.         sequence, eval = minimax(board, depth + 1, alpha, beta, not maximizing_player, node_count, completed_depths)
  49.         board.pop()
  50.  
  51.         if maximizing_player and eval > best_eval:
  52.             best_eval = eval
  53.             best_sequence = [board.san(move)] + sequence
  54.             alpha = max(alpha, eval)
  55.             if beta <= alpha:
  56.                 break
  57.         elif not maximizing_player and eval < best_eval:
  58.             best_eval = eval
  59.             best_sequence = [board.san(move)] + sequence
  60.             beta = min(beta, eval)
  61.             if beta <= alpha:
  62.                 break
  63.  
  64.     if depth not in completed_depths:
  65.         print(f"Depth {depth} completed for the first time.")
  66.         completed_depths.add(depth)
  67.  
  68.     return best_sequence, best_eval
  69.  
  70. # Main execution
  71. initial_pieces = "KkQ"
  72. positions = list(generate_chess_positions(initial_pieces))
  73.  
  74. if positions:
  75.     # Use the first position from generated positions for minimax
  76.     first_position_fen = "7k/2Q5/5K2/8/8/8/8/8 w - - 0 1" #positions[0]
  77.     board = chess.Board(first_position_fen)
  78.  
  79.     node_count = [0]  # Track nodes across recursive calls
  80.     completed_depths = set()  # Track completion of each depth level
  81.     sequence, evaluation = minimax(board, 0, float('-inf'), float('inf'), True, node_count, completed_depths)
  82.  
  83.     # Output results
  84.     simplified_fen = simplify_fen_string(first_position_fen)
  85.     print("Simplified FEN for starting position:", simplified_fen)
  86.     print("Best move sequence for first position:", sequence)
  87.     print("Evaluation for first position:", evaluation)
  88.     print("Total nodes explored:", node_count[0])
  89. else:
  90.     print("No positions were generated.")
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement