Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- def simplify_fen_string(fen):
- parts = fen.split(' ')
- simplified_fen = ' '.join(parts[:4]) # Zachováváme informace o pozici, hráči na tahu, rošádách a en passant
- return simplified_fen
- def minimax(board, depth, alpha, beta, maximizing_player, depth2):
- if depth == 0 or board.is_game_over():
- return None, evaluate_board(board, depth2)
- 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)
- board.pop()
- if eval > max_eval:
- max_eval = eval
- best_move = move
- alpha = max(alpha, eval)
- if beta <= alpha:
- break
- 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)
- board.pop()
- if eval < min_eval:
- min_eval = eval
- best_move = move
- beta = min(beta, eval)
- if beta <= alpha:
- break
- return best_move, min_eval
- def evaluate_board(board, depth):
- if board.is_checkmate():
- return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
- elif board.is_stalemate() or board.is_insufficient_material():
- return 0
- return 0
- # Nastavení startovní pozice
- start_fen = "8/8/8/8/1B6/2KN4/k7/8 w - - 0 1"
- #start_fen = "8/8/3k4/8/3K4/8/8/8 w - - 0 1"
- #start_fen = "7K/8/k1P5/7p/8/8/8/8 w - - 0 1"
- board = chess.Board(start_fen)
- # Spuštění minimaxu s maximální hloubkou 3
- best_move, best_score = minimax(board, 7, float('-inf'), float('inf'), True, 0)
- if best_move:
- move_san = board.san(best_move)
- print(f"Nejlepší tah z pozice {start_fen} je {move_san} s hodnocením {best_score}.")
- else:
- print("Nebyl nalezen žádný tah, nebo je hra u konce. Skóre: ", best_score)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement