generated from mwc/lab_dice
124 lines
3.0 KiB
Python
124 lines
3.0 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 value_count(self):
|
|
|
|
counts = {}
|
|
count_ones = 0
|
|
count_twos = 0
|
|
count_threes = 0
|
|
count_fours = 0
|
|
count_fives = 0
|
|
count_sixes = 0
|
|
|
|
for face in self.faces():
|
|
if face == 1:
|
|
count_ones+=1
|
|
if face == 2:
|
|
count_twos+=1
|
|
if face == 3:
|
|
count_threes+=1
|
|
if face == 4:
|
|
count_fours+=1
|
|
if face == 5:
|
|
count_fives+=1
|
|
if face == 6:
|
|
count_sixes+=1
|
|
|
|
counts = {
|
|
"ones":count_ones,
|
|
"twos":count_twos,
|
|
"threes":count_threes,
|
|
"fours":count_fours,
|
|
"fives":count_fives,
|
|
"sixes":count_sixes
|
|
}
|
|
|
|
return counts
|
|
|
|
|
|
|
|
def all_ones(self):
|
|
value_count = None
|
|
value_count = self.value_count()
|
|
if value_count["ones"] == 5:
|
|
return True
|
|
return False
|
|
|
|
|
|
def is_three_of_a_kind(self):
|
|
"""take the return of false out of the if statement.
|
|
We want to check all of the counts first and return true if any one of them is greater than or equal to 3.
|
|
We only return false if we've already gone through all of the counts."""
|
|
|
|
value_count = self.value_count()
|
|
for value in value_count.values():
|
|
if value >= 3:
|
|
return True
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_four_of_a_kind(self):
|
|
value_count = None
|
|
value_count = self.value_count()
|
|
for value in value_count.values():
|
|
if value >= 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("Odds of rolling all ones:" + str(successes/trials))
|
|
|
|
"""Create a new trial here to test for three of a kind. Repeat the code above, but make it for three of a kind."""
|
|
"""create another for four of a kind."""
|
|
|
|
dice = FiveDice()
|
|
successes1 = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.is_three_of_a_kind():
|
|
successes1 += 1
|
|
print("Odds of rolling three of a kind:" + str(successes1/trials))
|
|
|
|
dice = FiveDice()
|
|
successes2 = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.is_four_of_a_kind():
|
|
successes2 += 1
|
|
|
|
|
|
print("Odds of rolling four of a kind:" + str(successes2/trials))
|
|
|