Search code examples
python-3.xnumpyobject-oriented-database

Error with Yahtzee Code: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


This is my code for finding the probability of different die cases in Yahtzee. I've figured out the yahtzee, and full house methods. But when I do the is_it_large_straight method it gives me an error of this:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

What do I need to change in my code to make it produce what I want it to?

import numpy as np
import random

class Die:

    def __init__(self, sides):
        """A constructor method to create a die"""
        self.sides = sides

    def roll(self):
        """A general method to roll the die"""
        return random.randint(1, self.sides)

-------------------------------------------------

class Yahtzee:

    def __init__(self):
        """A constructor method that can record 5 dice rolls"""
        self.rolls = np.zeros(5, dtype=np.int16)

    def roll_dice(self):
        """A general method that rolls 5 dice"""
        for i in range(len(self.rolls)):
            self.rolls[i] = Die(6).roll()

    def count_outcomes(self):
        """A helper method that determines how many 1s, 2s, etc. were rolled"""
        counts = np.zeros(7, dtype=np.int16)
        for roll in self.rolls:
            counts[roll] += 1
        return counts
    def is_it_yahtzee(self):
        yahtzeefun = self.count_outcomes()
        if 5 in yahtzeefun:
            return True
        else:
            return False
    def is_it_full_house(self):
        fullhouse = self.count_outcomes()
        if 2 in fullhouse and 3 in fullhouse:
            return True
        else:
            return False
    def is_it_large_straight(self):
        largestraight = self.count_outcomes()
        if largestraight == [0,0,1,1,1,1,1] or largestraight == [0,1,1,1,1,1,0]:
            print("recognize straight")
            return True
        else:
            return False

-------------------------------------------------

def main(how_many):

    yahtzees = 0
    full_houses = 0
    large_straights = 0
    game = Yahtzee()

    for i in range(how_many):
        game.roll_dice()
        if game.is_it_yahtzee():
            yahtzees += 1
        elif game.is_it_full_house():
            full_houses += 1
        elif game.is_it_large_straight():
            large_straights += 1

    print("Number of Rolls:", how_many)
    print("---------------------")
    print("Number of Yahtzees:", yahtzees)
    print("Yahtzee Percent:", "{:.2f}%\n".format(yahtzees * 100 / how_many))
    print("Number of Full Houses:", full_houses)
    print("Full House Percent:", "{:.2f}%\n".format(full_houses * 100 / how_many))
    print("Number of Large Straights:", large_straights)
    print("Large Straight Percent:", "{:.2f}%".format(large_straights * 100 / how_many))

# -------------------------------------------------

main(5000)

Solution

  • In is_it_large_straight replace

    largestraight = self.count_outcomes()
    

    with

    largestraight = self.count_outcomes().tolist()
    

    currently largestraight is a numpy array and it doesn't have a definite boolean value, and thus can't be compared to your list in largestraight == [0,0,1,1,1,1,1]