Advertisement
max2201111

great print ascii boards

May 7th, 2024
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | Science | 0 0
  1. import chess
  2. import time
  3.  
  4. def ten_moves_rule(board):
  5.     history = list(board.move_stack)
  6.     if len(history) < 10:
  7.         return False
  8.     for move in history[-10:]:
  9.         if board.is_capture(move) or board.piece_type_at(move.from_square) == chess.PAWN:
  10.             return False
  11.     return True
  12.  
  13. def evaluate_board(board, depth):
  14.     if board.is_checkmate():
  15.         return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
  16.     elif board.is_stalemate():
  17.         return 4
  18.     elif board.is_insufficient_material():
  19.         return 3
  20.     elif ten_moves_rule(board):
  21.         return 2
  22.     return 1
  23.  
  24. def minimax(board, depth, alpha, beta, maximizing_player, move_sequence, memo):
  25.     if depth == 0 or board.is_game_over():
  26.         return None, evaluate_board(board, depth), move_sequence
  27.  
  28.     best_move = None
  29.     best_eval = float('-inf') if maximizing_player else float('inf')
  30.     best_sequence = []
  31.  
  32.     for move in board.legal_moves:
  33.         board.push(move)
  34.         current_sequence = move_sequence + [move]
  35.         _, eval, seq = minimax(board, depth - 1, alpha, beta, not maximizing_player, current_sequence, memo)
  36.         board.pop()
  37.  
  38.         if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
  39.             best_eval = eval
  40.             best_move = move
  41.             best_sequence = seq
  42.  
  43.         if maximizing_player:
  44.             alpha = max(alpha, eval)
  45.         else:
  46.             beta = min(beta, eval)
  47.  
  48.         if beta <= alpha:
  49.             break
  50.  
  51.     return best_move, best_eval, best_sequence + [best_move] if best_move else best_sequence
  52.  
  53. def print_board_sequence(start_fen, move_sequence):
  54.     board = chess.Board(start_fen)
  55.     print("Počáteční stav:")
  56.     print(board, "\nFEN:", board.fen(), "\n")
  57.  
  58.     for move in move_sequence:
  59.         if move and move in board.legal_moves:  # Kontrola, zda je tah legální
  60.             san = board.san(move)  # Získání SAN tahu
  61.             board.push(move)  # Provedení tahu na šachovnici
  62.             print("\nDalší tah:", san)
  63.             print(board, "\nFEN:", board.fen(), "\n")
  64.         else:
  65.      #       print("Nelegální nebo neexistující tah: {}", format(move))
  66.             break  # Přerušení smyčky v případě nelegálního tahu
  67.  
  68. # Spouštěcí kód
  69.  
  70. # Výpočet a tisk tahů
  71. #start_fen = "7k/8/3Q4/5K2/8/8/8/8 w
  72.  
  73.  
  74. # Initialization
  75. start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
  76. board = chess.Board(start_fen)
  77. memo = {}
  78.  
  79. # Run minimax
  80. _, _, sequence = minimax(board, 3, float('-inf'), float('inf'), True, [], memo)
  81. print_board_sequence(start_fen, sequence[::-1])  # Print the sequence from the initial position
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement