Yahtzee 代码错误:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()
Error with Yahtzee Code: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
这是我在 Yahtzee 中查找不同死亡案例概率的代码。我已经弄清楚了 yahtzee 和满屋方法。但是当我执行 is_it_large_straight 方法时,它给我一个错误:
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
我需要更改我的代码中的哪些内容才能生成我想要的结果?
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)
在is_it_large_straight中替换
largestraight = self.count_outcomes()
和
largestraight = self.count_outcomes().tolist()
目前 largestraight 是一个 numpy 数组,它没有确定的布尔值,因此无法与您在 largestraight 中的列表进行比较 == [0,0,1,1,1,1,1 ]
这是我在 Yahtzee 中查找不同死亡案例概率的代码。我已经弄清楚了 yahtzee 和满屋方法。但是当我执行 is_it_large_straight 方法时,它给我一个错误:
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
我需要更改我的代码中的哪些内容才能生成我想要的结果?
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)
在is_it_large_straight中替换
largestraight = self.count_outcomes()
和
largestraight = self.count_outcomes().tolist()
目前 largestraight 是一个 numpy 数组,它没有确定的布尔值,因此无法与您在 largestraight 中的列表进行比较 == [0,0,1,1,1,1,1 ]