hari.anbarasu
hari.anbarasu

Reputation: 9

Python: ValueError: need more than 0 values to unpack - Negamax Algorithm for Chess

I'm trying to build a chess AI, and I'm running into a strange error. As a foreword, I did go through stackoverflow and try to find similar issues, but it didn't help me as I thought I was implementing tuple unpacking correctly and I can't seem to find the bug in my code.

Below is the code for my negamax function. It takes in the chess board, the current player and the current depth.
It uses the board.evalFunction() function which returns a number representing the value at the board position to the current player. That function is working properly. I have marked the line where I am getting the error: ValueError: need more than 0 values to unpack.
Can someone help me out?

    def negamax(board, player, depth):
        if player == 'human':
            opposite = 'computer'
        else:
            opposite = 'human'
        if depth == 0:
            return (board.evalFunction(player), None)
        moveset = board.generateMoves(player)
        maximum = -99999999999
        optMove = ""
        for move in moveset:
            newBoard = copy.deepcopy(board)
            newBoard.makeMove(move)
            newBoard.rotate()
            #The following line throws the error
            score, move = -1 * negamax(board, opposite, depth - 1)
            if score > maximum:
                maximum = score
                optMove = move
            elif score == maximum:
                if(random.choice([True, False])):
                    maximum = score
                    optMove = move
        return (maximum, optMove)

Upvotes: 0

Views: 353

Answers (3)

Saeid
Saeid

Reputation: 4255

If you multiply a tuple by a number you get an empty tuple

>>> -1*(1,2)
()

This is causing you the error. You should manually negate every element of the tuple. Also you should check out this question: Multiplying a tuple by a scalar

Upvotes: 0

JDurstberger
JDurstberger

Reputation: 4255

you cannot just multiply a tuple with a constant. You should use list comprehension

score, move = tuple([-1*i for i in negamax(board, opposite, depth - 1)])

Upvotes: 0

SamChi
SamChi

Reputation: 59

change this statement

score, move = -1 * negamax(board, opposite, depth - 1)

to

score, move = tuple(-1 * x for x in negamax(board, opposite, depth - 1))

Use the generator expression with the function args will be much better.

Upvotes: 2

Related Questions