andretafta

Untitled

Jan 24th, 2026
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. def minimax(player, depth, alpha, beta):
  2.     # If the game is over or we've reached the maximum depth, return a score for the board
  3.     if depth == 0 or check_win("black") or check_win("red") or not check_moves_left():
  4.         return check_curr_score(), -1, -1  # Return the score and an invalid move (-1, -1)
  5.    
  6.     best_score = -float('inf') if player == "black" else float('inf')
  7.     best_row, best_col = -1, -1
  8.  
  9.     # Explore all possible columns for the next move
  10.     for col in range(7):
  11.         if is_valid_move(col):  # If the column is valid, try a move
  12.             row = check_column_depth(col)  # Get the next available row
  13.             place_player(player, row, col)  # Make the move on the board
  14.             score, _, _ = minimax("red" if player == "black" else "black", depth-1, alpha, beta)  # Recursively call minimax
  15.            
  16.             # Undo the move after evaluating
  17.             board[row][col] = "-"
  18.            
  19.             # Update the best score and column
  20.             if player == "black" and score > best_score:
  21.                 best_score = score
  22.                 best_row, best_col = row, col
  23.                 alpha = max(alpha, score)
  24.             elif player == "red" and score < best_score:
  25.                 best_score = score
  26.                 best_row, best_col = row, col
  27.                 beta = min(beta, score)
  28.            
  29.             # Alpha-Beta Pruning
  30.             if alpha >= beta:
  31.                 break
  32.    
  33.     return best_score, best_row, best_col
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment