From bb5b2c28b60f7db1162908351321f1337f562ea7 Mon Sep 17 00:00:00 2001 From: Chen6528 Date: Sun, 16 Feb 2025 15:14:45 -0500 Subject: [PATCH] Update connect4_with_ai.py fixed minimax function to allow multiple "best" options --- connect4_with_ai.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/connect4_with_ai.py b/connect4_with_ai.py index de0ddcb..d028308 100644 --- a/connect4_with_ai.py +++ b/connect4_with_ai.py @@ -135,7 +135,7 @@ def minimax(board, depth, alpha, beta, maximizingPlayer): return (None, score_position(board, AI_PIECE)) if maximizingPlayer: value = -math.inf - column = random.choice(valid_locations) + best_col = [] for col in valid_locations: row = get_next_open_row(board, col) b_copy = board.copy() @@ -143,10 +143,14 @@ def minimax(board, depth, alpha, beta, maximizingPlayer): new_score = minimax(b_copy, depth-1, alpha, beta, False)[1] if new_score > value: value = new_score - column = col + best_col = [col] + elif new_score == value: + best_col.append(col) alpha = max(alpha, value) if alpha >= beta: break + + column = random.choice(best_col) return column, value else: # Minimizing player