Advertisement
max2201111

very good minimax pomoci '.' a korektni result a dpeth

May 13th, 2024
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | Science | 0 0
  1. import chess
  2. from itertools import permutations
  3. from functools import lru_cache
  4.  
  5. def generate_chess_positions(pieces):
  6.     all_squares = [chess.SQUARES[i] for i in range(64)]
  7.     unique_fens = set()
  8.     for squares in permutations(all_squares, len(pieces)):
  9.         board = chess.Board(None)
  10.         board.clear_board()
  11.         for piece, square in zip(pieces, squares):
  12.             board.set_piece_at(square, chess.Piece.from_symbol(piece))
  13.         if board.is_valid():
  14.             unique_fens.add(board.fen())
  15.     return unique_fens
  16.  
  17. def evaluate_board(board, depth):
  18.     if board.is_checkmate():
  19.         return 1000 - depth if not board.turn else -1000 + depth
  20.     elif board.is_stalemate() or board.is_insufficient_material():
  21.         return 0
  22.     return 4  # Basic heuristic for non-terminal positions
  23.  
  24. @lru_cache(maxsize=None)
  25. def minimax(fen, depth, alpha, beta, maximizing_player, max_depth):
  26.     board = chess.Board(fen)
  27.     if depth == max_depth or board.is_game_over():
  28.         return evaluate_board(board, depth)
  29.  
  30.     best_eval = float('-inf') if maximizing_player else float('inf')
  31.     for move in board.legal_moves:
  32.         board.push(move)
  33.         eval = minimax(board.fen(), depth + 1, alpha, beta, not maximizing_player, max_depth)
  34.         board.pop()
  35.  
  36.         if maximizing_player:
  37.             best_eval = max(best_eval, eval)
  38.             alpha = max(alpha, eval)
  39.             if beta <= alpha:
  40.                 break
  41.         else:
  42.             best_eval = min(best_eval, eval)
  43.             beta = min(beta, eval)
  44.             if beta <= alpha:
  45.                 break
  46.  
  47.     return best_eval
  48.  
  49. # Hlavní část kódu
  50. initial_pieces = ['K', 'k', 'Q']
  51. unique_positions = generate_chess_positions(initial_pieces)
  52. evaluations = []
  53.  
  54. print("Počet unikátních pozic:", len(unique_positions))
  55.  
  56. # Omezení na prvních 10000 pozic
  57. for fen in list(unique_positions)[:10000]:
  58.     print(".", end='')  # Tisk teček pro sledování průběhu
  59.     board = chess.Board(fen)
  60.     max_depth = 8  # Limit depth for demonstration purposes
  61.     evaluation = minimax(fen, 0, float('-inf'), float('inf'), board.turn == chess.WHITE, max_depth)
  62.     evaluations.append((fen, evaluation))
  63.  
  64. # Print results
  65. for position, eval in evaluations:
  66.     if eval != 0 and eval != 4:
  67.         print(f"FEN: {position}, Evaluation: {eval}")
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement