generated from mwc/lab_dice
64 lines
1.4 KiB
Python
64 lines
1.4 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 all_ones(self):
|
|
for face in self.faces():
|
|
if face != 1:
|
|
return False
|
|
return True
|
|
|
|
def face_counts(self):
|
|
counts = {}
|
|
for face in self.faces():
|
|
if face in counts:
|
|
counts[face] += 1
|
|
else:
|
|
counts[face] = 1
|
|
return counts
|
|
|
|
def is_three_of_a_kind(self):
|
|
for count in self.face_counts().values():
|
|
if count >= 3:
|
|
return True
|
|
return False
|
|
|
|
def is_four_of_a_kind(self):
|
|
for count in self.face_counts().values():
|
|
if count >= 4:
|
|
return True
|
|
return False
|
|
|
|
dice = FiveDice()
|
|
all_ones_count = 0
|
|
three_kind_count = 0
|
|
four_kind_count = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.all_ones():
|
|
all_ones_count += 1
|
|
if dice.is_three_of_a_kind():
|
|
three_kind_count += 1
|
|
if dice.is_four_of_a_kind():
|
|
four_kind_count += 1
|
|
|
|
print("all ones:", all_ones_count/trials)
|
|
print("Three of a kind:", three_kind_count / trials)
|
|
print("Four of a kind:", four_kind_count / trials)
|
|
|
|
|
|
|
|
|