Advertisement
max2201111

konecne vse pro 4 figury a 993 very good

May 14th, 2024
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | Science | 0 0
  1. import chess
  2. import random
  3. from itertools import combinations, permutations
  4. from functools import lru_cache
  5. from concurrent.futures import ThreadPoolExecutor, as_completed
  6.  
  7. def generate_chess_positions(pieces):
  8.     all_squares = [chess.SQUARES[i] for i in range(64)]
  9.     unique_fens = set()
  10.     for squares in combinations(all_squares, len(pieces)):
  11.         perm = permutations(squares)
  12.         for square_perm in perm:
  13.             board = chess.Board(None)
  14.             board.clear_board()
  15.             for piece, square in zip(pieces, square_perm):
  16.                 board.set_piece_at(square, chess.Piece.from_symbol(piece))
  17.             if board.is_valid():
  18.                 if random.choice([True, False]):
  19.                     board.turn = chess.BLACK  # Náhodně nastavíme tah pro černého
  20.                 unique_fens.add(board.fen())
  21.     return unique_fens
  22.  
  23. def evaluate_board(board, depth):
  24.     if board.is_checkmate():
  25.         return 1000 - depth if not board.turn else -1000 + depth
  26.     elif board.is_stalemate() or board.is_insufficient_material():
  27.         return 0
  28.     return 4  # Basic heuristic for non-terminal positions
  29.  
  30. @lru_cache(maxsize=None)
  31. def minimax(fen, depth, alpha, beta, maximizing_player, max_depth):
  32.     board = chess.Board(fen)
  33.     if depth == max_depth or board.is_game_over():
  34.         return evaluate_board(board, depth), None  # Vrátit tuple s hodnocením a None pro tah
  35.  
  36.     best_eval = float('-inf') if maximizing_player else float('inf')
  37.     best_move = None
  38.     for move in board.legal_moves:
  39.         board.push(move)
  40.         eval, _ = minimax(board.fen(), depth + 1, alpha, beta, not maximizing_player, max_depth)
  41.         board.pop()
  42.  
  43.         if maximizing_player:
  44.             if eval > best_eval:
  45.                 best_eval = eval
  46.                 best_move = move
  47.             alpha = max(alpha, eval)
  48.             if beta <= alpha:
  49.                 break
  50.         else:
  51.             if eval < best_eval:
  52.                 best_eval = eval
  53.                 best_move = move
  54.             beta = min(beta, eval)
  55.             if beta <= alpha:
  56.                 break
  57.  
  58.     return best_eval, best_move  # Vrátit tuple s hodnocením a nejlepším tahem
  59.  
  60. # Hlavní část kódu
  61. initial_pieces = ['K', 'k', 'Q', 'B']
  62. unique_positions = generate_chess_positions(initial_pieces)
  63. evaluations = []
  64.  
  65. print("Počet unikátních pozic:", len(unique_positions))
  66.  
  67. # Funkce pro paralelní zpracování
  68. def process_position(fen, max_depth):
  69.     print(".", end='')  # Tisk tečky před každým voláním minimax
  70.     board = chess.Board(fen)
  71.     evaluation, best_move = minimax(fen, 0, float('-inf'), float('inf'), board.turn == chess.WHITE, max_depth)
  72.     return (fen, evaluation, best_move)
  73.  
  74. # Paralelní zpracování pozic
  75. max_depth = 8  # Limit depth for demonstration purposes
  76. with ThreadPoolExecutor(max_workers=8) as executor:
  77.     futures = [executor.submit(process_position, fen, max_depth) for fen in list(unique_positions)[:2000]]
  78.     for future in as_completed(futures):
  79.         fen, evaluation, best_move = future.result()
  80.         evaluations.append((fen, evaluation, best_move))
  81.  
  82. # Print results
  83. for position, eval, move in evaluations:
  84.     if eval != 0 and eval != 4 and eval < 994:
  85.         turn = "White" if chess.Board(position).turn == chess.WHITE else "Black"
  86.         print(f"FEN: {position}, Turn: {turn}, Best Move: {move}, Evaluation: {eval}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement