generated from mwc/lab_dice
wrote the methods for checking how many times a three of a kid or four of a kind was rolled
This commit is contained in:
@@ -13,12 +13,67 @@ class FiveDice:
|
||||
def faces(self):
|
||||
return [die.face for die in self.dice]
|
||||
|
||||
def value_count(self):
|
||||
# will count how many of each kind of dice and return a list of how many of each value
|
||||
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}
|
||||
# print (counts)
|
||||
return counts
|
||||
|
||||
def all_ones(self):
|
||||
for face in self.faces():
|
||||
if face != 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_three_of_a_kind(self):
|
||||
#In the line below, use self.value_count so that it knows that value_count refers to the method in this instance of this class
|
||||
|
||||
counts = self.value_count()
|
||||
# because we need to refer to the values in the dict vs the dict item as a whole, we need to use the count.values() function to refer to the values in the dict.
|
||||
for count in counts.values():
|
||||
##logic question, this will show only exactly 3 of a kind. If you roll 4 of a kind would that be a success of three of a kind? If it would, how would you change this?
|
||||
if count >= 3:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def is_four_of_a_kind(self):
|
||||
#In the line below, use self.value_count so that it knows that value_count refers to the method in this instance of this class
|
||||
counts = self.value_count()
|
||||
for count in counts.values():
|
||||
if count == 4:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
|
||||
dice = FiveDice()
|
||||
successes = 0
|
||||
trials = 1000000
|
||||
@@ -27,7 +82,26 @@ for trial in tqdm(range(trials)):
|
||||
if dice.all_ones():
|
||||
successes += 1
|
||||
|
||||
print(successes/trials)
|
||||
|
||||
print("Odds for rolling all ones: "+ str(successes/trials))
|
||||
|
||||
dice = FiveDice()
|
||||
successes_3 = 0
|
||||
trials = 1000000
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_three_of_a_kind():
|
||||
successes_3 += 1
|
||||
###If you want to modify this to make the print more clear about what you are printing you can concatinate a description string like I did for the all ones trial above.
|
||||
print("Odds for rolling all threes: "+ str(successes_3/trials))
|
||||
|
||||
dice = FiveDice()
|
||||
successes_4 = 0
|
||||
trials = 1000000
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_four_of_a_kind():
|
||||
successes_4 += 1
|
||||
|
||||
###Don't forget to call the print again here for the trial for 4 of a kind
|
||||
print("Odds for rolling all fours: "+ str(successes_4/trials))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user