Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def minimax(player, depth, alpha, beta):
- # If the game is over or we've reached the maximum depth, return a score for the board
- if depth == 0 or check_win("black") or check_win("red") or not check_moves_left():
- return check_curr_score(), -1, -1 # Return the score and an invalid move (-1, -1)
- best_score = -float('inf') if player == "black" else float('inf')
- best_row, best_col = -1, -1
- # Explore all possible columns for the next move
- for col in range(7):
- if is_valid_move(col): # If the column is valid, try a move
- row = check_column_depth(col) # Get the next available row
- place_player(player, row, col) # Make the move on the board
- score, _, _ = minimax("red" if player == "black" else "black", depth-1, alpha, beta) # Recursively call minimax
- # Undo the move after evaluating
- board[row][col] = "-"
- # Update the best score and column
- if player == "black" and score > best_score:
- best_score = score
- best_row, best_col = row, col
- alpha = max(alpha, score)
- elif player == "red" and score < best_score:
- best_score = score
- best_row, best_col = row, col
- beta = min(beta, score)
- # Alpha-Beta Pruning
- if alpha >= beta:
- break
- return best_score, best_row, best_col
Advertisement
Add Comment
Please, Sign In to add comment