0

The minimax function has a max function and a min function. The recursion takes place between them, where the max function calls the min function and the min, calls the max. While exploring a node, the program sets the board back to its deepcopy state after exploring each branch connected to the node. This seems to be working fine for a few cases, but after a point the board doesn't seem to set back to the correct deepcopy. My suspicion is that the saved deepcopies are getting altered somewhere down the recursion process, but i just cant seem to find WHERE?!

Below is my code for the minimax implimentation.

P.S: Ignore the print statements as they're for dubugging.

def minimax(board):

    deepcpy2 = copy.deepcopy(board)

    deepcpy1 = copy.deepcopy(board)

    best_action = []

    if board == initial_state():

        return [0, 2]

    def Minvalu(board):
        v = 2
       
        print("min", board)

        if terminal(board):
            print("returned", utility(board))
            v = utility(board)
            return v

        else:
            deepcpy = copy.deepcopy(board)
            print("POSSIBLE MOVES", actions(board))
           
            for K in actions(board):
                
                result_board = result(board, K)
                print("resulting board", result_board)
               
               
                v = min(v, Maxvalu(result_board))
               
                board = deepcpy
                print("back to DC", board)
               
                if v == -1:

                    break
      
        return v

    def Maxvalu(board):
        

        v = -2

        print("max", board)

        if terminal(board):
            print("returned", utility(board))
            v = utility(board)
            return v

        else:
            deepcpy = copy.deepcopy(board)
            print("POSSIBLE MOVES", (actions(board)))
            for Z in actions(board):
                result_board = result(board, Z)
                print("resulting board", result_board)
                v = max(v, Minvalu(result_board))
                board = deepcpy
                print("back to DC", board)            
                if v == 1:
                    break

        return v

    if player(deepcpy2) == X:

        best_val = -1
        best_action = [-1, -1]

        for Y in actions(deepcpy2):           
            move_value = Minvalu(result(deepcpy2, Y))

            if move_value == 1:
                best_action = Y
                board = deepcpy2
                break
            if move_value > best_val:
                best_action = Y
                board = deepcpy2
      
    if player(deepcpy1) == O:

        best_val = 1

        best_action = [-1, -1]

        for S in actions(deepcpy1):
            move_value = Maxvalu(result(deepcpy1, S))
            if move_value == -1:
                best_action = S
                board = deepcpy1
                break
            if move_value < best_val:
                best_action = S
                board = deepcpy1
       

You must log in to answer this question.

Browse other questions tagged .