generated from mwc/lab_dice
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
from die import Die
|
|
from tqdm import tqdm
|
|
from collections import Counter
|
|
|
|
class FiveDice:
|
|
def __init__(self):
|
|
self.dice = [Die() for number in range(5)] # 5 die are cast at the same time, figuratively
|
|
|
|
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 is_three_of_a_kind(self):
|
|
counts = Counter([die.face for die in self.dice]) # Counter class counts
|
|
# each list item occurrance
|
|
my_dict=dict(counts) # converts Counter class to a dict
|
|
values = list(my_dict.values()) # unpacks dict values into a list
|
|
for a in values: # loops in the list of dict values
|
|
if a == 3: # exactly three die cast are the same number
|
|
|
|
return True # exactly three die cast are the same number
|
|
|
|
return False # other than three die cast are the same number
|
|
|
|
def is_four_of_a_kind(self):
|
|
counts = Counter([die.face for die in self.dice]) # Counter class counts
|
|
# each list item occurrance
|
|
my_dict=dict(counts) # converts Counter class to a dict
|
|
values = list(my_dict.values()) # unpacks dict values into a list
|
|
for a in values: # loops in the list of dict values
|
|
if a == 4: # exactly four die cast are the same number
|
|
|
|
return True # exactly four die cast are the same number
|
|
|
|
return False # other than four die cast are the same number
|
|
|
|
dice = FiveDice()
|
|
successes_three = 0
|
|
successes_four = 0
|
|
trials = 1000000
|
|
for trial in tqdm(range(trials)):
|
|
dice.roll()
|
|
if dice.is_three_of_a_kind(): # if True
|
|
successes_three += 1 # increment by 1
|
|
if dice.is_four_of_a_kind(): # if True
|
|
successes_four += 1 # increment by 1
|
|
|
|
print("exactly three of a kind: ")
|
|
print(successes_three/trials) # decimal
|
|
s3=successes_three/trials*100 # percent
|
|
print(str(s3) + " %")
|
|
print()
|
|
print("exactly four of a kind: ")
|
|
print(successes_four/trials) # decimal
|
|
s4=successes_four/trials*100 # percent
|
|
print(str(s4) + " %")
|