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 count_dice(self, number):
|
|
count = 0
|
|
for die in dice:
|
|
if die.face == number:
|
|
count = count + 1
|
|
return count
|
|
|
|
def is_three_of_a_kind(self):
|
|
for number in [1, 2, 3, 4, 5, 6]:
|
|
count = 0
|
|
for die in self.dice:
|
|
if die.face == number:
|
|
count = count + 1
|
|
if count >= 3:
|
|
return True
|
|
return False
|
|
|
|
|
|
def is_four_of_a_kind(self):
|
|
for number in [1, 2, 3, 4, 5, 6]:
|
|
count = 0
|
|
for die in self.dice:
|
|
if die.face == number:
|
|
count = count + 1
|
|
if count >= 4:
|
|
return True
|
|
return False
|
|
|
|
dice = FiveDice()
|
|
successes = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
# if dice.all_ones():
|
|
# if dice.is_three_of_a_kind():
|
|
if dice.is_four_of_a_kind():
|
|
successes += 1
|
|
|
|
print(successes/trials)
|
|
|
|
|
|
|