Advertisement
max2201111

very good petr 2

May 1st, 2024
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | Science | 0 0
  1. import chess
  2.  
  3. def simplify_fen_string(fen):
  4.     parts = fen.split(' ')
  5.     simplified_fen = ' '.join(parts[:4])  # Zachováváme informace o pozici, hráči na tahu, rošádách a en passant
  6.     return simplified_fen
  7.  
  8. def minimax(board, depth, alpha, beta, maximizing_player, depth2):
  9.     if depth == 0 or board.is_game_over():
  10.         return None, evaluate_board(board, depth2)
  11.    
  12.     best_move = None
  13.    
  14.     if maximizing_player:
  15.         max_eval = float('-inf')
  16.         for move in board.legal_moves:
  17.             board.push(move)
  18.             _, eval = minimax(board, depth - 1, alpha, beta, False, depth2 + 1)
  19.             board.pop()
  20.             if eval > max_eval:
  21.                 max_eval = eval
  22.                 best_move = move
  23.             alpha = max(alpha, eval)
  24.             if beta <= alpha:
  25.                 break
  26.         return best_move, max_eval
  27.     else:
  28.         min_eval = float('inf')
  29.         for move in board.legal_moves:
  30.             board.push(move)
  31.             _, eval = minimax(board, depth - 1, alpha, beta, True, depth2 + 1)
  32.             board.pop()
  33.             if eval < min_eval:
  34.                 min_eval = eval
  35.                 best_move = move
  36.             beta = min(beta, eval)
  37.             if beta <= alpha:
  38.                 break
  39.         return best_move, min_eval
  40.  
  41. def evaluate_board(board, depth):
  42.     if board.is_checkmate():
  43.         return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
  44.     elif board.is_stalemate() or board.is_insufficient_material():
  45.         return 0
  46.     return 0
  47.  
  48. # Nastavení startovní pozice
  49. start_fen = "8/8/8/8/1B6/2KN4/k7/8 w - - 0 1"
  50.  
  51. #start_fen = "8/8/3k4/8/3K4/8/8/8 w - - 0 1"
  52.  
  53. #start_fen = "7K/8/k1P5/7p/8/8/8/8 w - - 0 1"
  54.  
  55. board = chess.Board(start_fen)
  56.  
  57. # Spuštění minimaxu s maximální hloubkou 3
  58. best_move, best_score = minimax(board, 7, float('-inf'), float('inf'), True, 0)
  59. if best_move:
  60.     move_san = board.san(best_move)
  61.     print(f"Nejlepší tah z pozice {start_fen} je {move_san} s hodnocením {best_score}.")
  62. else:
  63.     print("Nebyl nalezen žádný tah, nebo je hra u konce. Skóre: ", best_score)
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement