generated from mwc/lab_dice
107 lines
2.6 KiB
Python
107 lines
2.6 KiB
Python
from die import Die
|
|
from tqdm import tqdm
|
|
|
|
class FiveDice:
|
|
def __init__(self):
|
|
self.dice = [Die() for number in range(5)]
|
|
|
|
def roll(self):
|
|
for die in self.dice:
|
|
die.roll()
|
|
return self.faces()
|
|
|
|
def faces(self):
|
|
return [die.face for die in self.dice]
|
|
|
|
def value_count(self):
|
|
|
|
ones = 0
|
|
twos = 0
|
|
threes = 0
|
|
fours = 0
|
|
fives = 0
|
|
sixes = 0
|
|
|
|
for face in self.faces():
|
|
if face == 1:
|
|
ones += 1
|
|
if face == 2:
|
|
twos += 1
|
|
if face == 3:
|
|
threes += 1
|
|
if face == 4:
|
|
fours += 1
|
|
if face == 5:
|
|
fives += 1
|
|
if face == 6:
|
|
sixes += 1
|
|
|
|
count = {"ones": ones, "twos": twos, "threes": threes, "fours": fours, "fives": fives, "sixes": sixes}
|
|
return count
|
|
|
|
def all_ones(self):
|
|
for face in self.faces():
|
|
if face != 1:
|
|
return False
|
|
return True
|
|
|
|
def is_three_of_a_kind(self):
|
|
count = self.value_count()
|
|
if count["ones"] == 3:
|
|
return True
|
|
if count["twos"] == 3:
|
|
return True
|
|
if count["threes"] == 3:
|
|
return True
|
|
if count["fours"] == 3:
|
|
return True
|
|
if count["fives"] == 3:
|
|
return True
|
|
if count["sixes"] == 3:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def is_four_of_a_kind(self):
|
|
count = self.value_count()
|
|
if count["ones"] == 4:
|
|
return True
|
|
if count["twos"] == 4:
|
|
return True
|
|
if count["threes"] == 4:
|
|
return True
|
|
if count["fours"] == 4:
|
|
return True
|
|
if count["fives"] == 4:
|
|
return True
|
|
if count["sixes"] == 4:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
dice = FiveDice()
|
|
successes_all_ones = 0
|
|
successes_is_three_of_a_kind = 0
|
|
successes_is_four_of_a_kind = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.all_ones():
|
|
successes_all_ones += 1
|
|
|
|
if dice.is_three_of_a_kind():
|
|
successes_is_three_of_a_kind += 1
|
|
|
|
if dice.is_four_of_a_kind():
|
|
successes_is_four_of_a_kind += 1
|
|
"""
|
|
print(successes_all_ones/trials , successes_is_three_of_a_kind/trials , successes_is_four_of_a_kind/trials)
|
|
"""
|
|
print(
|
|
f"All ones: {successes_all_ones/trials:.6f}, "
|
|
f"Three of a kind: {successes_is_three_of_a_kind/trials:.6f}, "
|
|
f"Four of a kind: {successes_is_four_of_a_kind/trials:.6f}"
|
|
)
|
|
|
|
|