generated from mwc/lab_dice
89 lines
2.4 KiB
Python
89 lines
2.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 is_three_of_a_kind(self):
|
|
#set current roll of 5 dice to a list variable dice
|
|
dice = self.faces()
|
|
for die in range(len(dice)):
|
|
#take each roll value and compare it to the other values.
|
|
face_value = (dice[die])
|
|
how_many = 0
|
|
|
|
for d in range(len(dice)):
|
|
#if the roll value is the same as another value, increase the count of how many
|
|
if die == face_value:
|
|
how_many +=1
|
|
#if the cound of how many of that value is 3 or more, then you got three of a kind. So, return true.
|
|
if how_many >= 3:
|
|
return True
|
|
return False
|
|
|
|
def is_four_of_a_kind(self):
|
|
#set current roll of 5 dice to a list variable dice
|
|
dice = self.faces()
|
|
for die in range(len(dice)):
|
|
#take each roll value and compare it to the other values.
|
|
face_value = (dice[die])
|
|
how_many = 0
|
|
|
|
for d in range(len(dice)):
|
|
#if the roll value is the same as another value, increase the count of how many
|
|
if die == face_value:
|
|
how_many +=1
|
|
#if the count of how many of that value is 4 or more, then you got three of a kind. So, return true.
|
|
if how_many >= 4:
|
|
return True
|
|
return False
|
|
|
|
|
|
dice = FiveDice()
|
|
successes = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.all_ones():
|
|
successes += 1
|
|
|
|
print(f"All Ones: {successes/trials}")
|
|
|
|
dice = FiveDice()
|
|
successes_3 = 0
|
|
trials_3 = 1000000
|
|
for trial in tqdm(range(trials_3)):
|
|
dice.roll()
|
|
if dice.is_three_of_a_kind():
|
|
successes_3 += 1
|
|
|
|
print(f"Three of a Kind: {successes_3/trials_3}")
|
|
|
|
dice = FiveDice()
|
|
successes_4 = 0
|
|
trials_4 = 1000000
|
|
for trial in tqdm(range(trials_4)):
|
|
dice.roll()
|
|
if dice.is_four_of_a_kind():
|
|
successes_4 += 1
|
|
|
|
print(f"Four of a Kind: {successes_4/trials_4}")
|
|
|
|
|
|
|