My AI will sometimes skip the direct winning move and do some weired actions and avoid direct win in a connect 4 game. According to the minimax, the AI should able to get the MAX value, which is math.inf and use the correspond colum for victory. ALthough it still always win in the endThe AI (O) missed the direct victory move Additionally, I am not sure if it is because the mentioned issue, My model somehow can not pass the hidden test. I have tried to rewrite the code for many times and still can not pass it. I tired to change different ways of rewirting the code yer): global COUNT COUNT += 1 opponent = HUMAN if player == AI else AI valid_columns = get_valid_columns(board) is_terminal = is_terminal_node(board) if depth == 0 or is_terminal: ### [TODO-1] ''' If the depth is 0 (depth limit has been reached) or the node is terminal (indicating a win, lose, or tie state), the function returns the (heuristic) value of the node returned by `evaluate_position`. ''' ########### TODO-1: Your code here ########### if do_max: maxizer = player minimizer = opponent else: maxizer = opponent minimizer = player if is_terminal: if winning_move(board, maxizer): value = 1000000 elif winning_move(board, minimizer): value = -1000000 else: value = 0 else: value = evaluate_position(board, maxizer) column = valid_columns[0] if valid_columns else None ########### End of your code ########### return column, value else: value = -1000000 if do_max else 1000000 column = valid_columns[0] if valid_columns else None ### [TODO-2] ''' Minimax is a recursive function that explores the game tree to find the best move. It alternates between maximizing and minimizing the score based on the current player. Here, if 'do_max' is True, function 'minimax' should do maximizing, otherwise it should do minimizing. And 'player' refers to the current player that will drop a disc this turn and be evaluated by 'minimax'. ''' ########### TODO-2: Your code here ########### for col in valid_columns: row = get_curr_row_by_col(board,col) drop_disc(board, col, player) new_score = minimax(board, depth-1, not do_max, opponent)[1] board[row][col] = 0 if do_max: if new_score > value: value = new_score column = col else: if new_score < value: value = new_score column = col ######### End of your code ########### return column, value Or: def minimax(board, depth, do_max, player): #version with instant terminal while win global COUNT COUNT += 1 opponent = HUMAN if player == AI else AI valid_columns = get_valid_columns(board) is_terminal = is_terminal_node(board) if depth == 0 or is_terminal: ### [TODO-1] ''' If the depth is 0 (depth limit has been reached) or the node is terminal (indicating a win, lose, or tie state), the function returns the (heuristic) value of the node returned by `evaluate_position`. ''' ########### TODO-1: Your code here ########### if is_terminal: if winning_move(board, AI): value = 1000000 elif winning_move(board, HUMAN): value = -1000000 else: value = 0 else: value = evaluate_position(board, AI) column = None ########### End of your code ########### return column, value else: value = -math.inf if do_max else math.inf column = valid_columns[0] if valid_columns else None ### [TODO-2] ''' Minimax is a recursive function that explores the game tree to find the best move. It alternates between maximizing and minimizing the score based on the current player. Here, if 'do_max' is True, function 'minimax' should do maximizing, otherwise it should do minimizing. And 'player' refers to the current player that will drop a disc this turn and be evaluated by 'minimax'. ''' ########### TODO-2: Your code here ########### for col in valid_columns: row = get_curr_row_by_col(board,col) drop_disc(board, col, player) new_score = minimax(board, depth-1, not do_max, opponent)[1] board[row][col] = 0 if do_max: if new_score > value: value = new_score column = col else: if new_score < value: value = new_score column = col ######### End of your code ########### return column, value And: def minimax(board, depth, do_max, player): global COUNT COUNT += 1 opponent = HUMAN if player == AI else AI valid_columns = get_valid_columns(board) is_terminal = is_terminal_node(board) if depth == 0 or is_terminal: # value = 0 # if is_terminal: # if winning_move(board, player): # value = 1000000 if do_max else -1000000 # elif winning_move(board, opponent): # value = -1000000 if do_max else 1000000 # else: # 平局 # value = 0 # else: # value = evaluate_position(board, AI) # column = valid_columns[0] if valid_columns else None # return column, value if depth == 0 or is_terminal: value = evaluate_position(board, AI) return None, value else: value = -math.inf if do_max else math.inf best_column = valid_columns[0] if valid_columns else None for col in valid_columns: row = get_curr_row_by_col(board, col) if row is None: continue drop_disc(board, col, player) new_column, new_score = minimax(board, depth-1, not do_max, opponent) board[row][col] = 0 # 假设有撤销操作 if do_max: if new_score > value: value = new_score best_column = col else: if new_score < value: value = new_score best_column = col return best_column, value But none of them can solve the issue. Continue reading...