0

I have the error: "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" in three lines:

col, minimax_score = int(minimax(board, 2, -math.inf, math.inf, True)) ,

valid_locations = get_valid_locations(board)

and

if available_square(board, col):

I searched for possible solutions in the Internet (for e.g. using (a-b).any() or (a-b).all() instead of a and b) but none of them seemed to apply to my case.

edit:

The "minimax" function is very long so I will just add this:

def minimax(board, depth, alpha, beta, maximizingPlayer):

This is the "get_valid_locations" function:

def get_valid_locations(board):
    valid_locations = []
    for col in range(BOARD_COLS):
        if available_square(board, col):
            valid_locations.append(col)
    return valid_locations

This is the "available_ square" function:

def available_square(row, col):
    return board[row][col] == 0
Cowcake
  • 13
  • 6
  • 1
    We don't know what any of these objects are, so its hard to guess. But if you have an array, what do you consider "true" in this case. Suppose you have `np.arry([0,0,1])` is is True because there is at least one True value (any) or False because not all of the values are True (all)? You say .any and .all don't seem to apply, why not? Show us one example with data and why they don't apply. – tdelaney Jan 02 '21 at 21:12
  • It looks like you are trying to evaluate the 'truthness' of an array at some point in your code which is not really defined. For example, is an array of zeros true, or false? What if all but one element is True? Hence its advising to use the `.any()` or `.all()` functions which reduce to a singular true or false value. Could you provide more of the code in which the error takes place. If you are not sure, just paste the error message that the interpreter is giving to you. – mandulaj Jan 02 '21 at 21:15
  • Whta's the shape of `board`. Form the code you provided, I suspect that it is a 3+ dimensional array. When you call `board[row][col]`, it probably gives you an array which you can not directly compare to a scalar `0` – mandulaj Jan 02 '21 at 21:36
  • It is a 2D 4 by 4 game – Cowcake Jan 02 '21 at 21:40
  • The error's `any/all `suggestion doesn't help if you don't understand why there's an error. The basic point, which is mentioned many times in the related SO questions, is that you have a boolean array (or array comparison) in a Python context that requires a simple True/False value. An `if` statement is most comon. `or/and` also have that. But there are other cases as well. – hpaulj Jan 02 '21 at 21:52
  • Usually to help, we need to see code, traceback, and information on the variables (especially arrays) in the problem expression(s). The side bar has links to lots of questions with this same error message. – hpaulj Jan 02 '21 at 21:54
  • thanks, I will try to understand where the error is. – Cowcake Jan 02 '21 at 22:02

1 Answers1

0

I just noticed, you call available_square with available_square(board, col) board and col as arguments, you probably want the signature be

def available_square(board, row, col):
    ....

and call is as available_square(board, row, col)

mandulaj
  • 733
  • 3
  • 10