generated from mwc/lab_dice
did the code for three and four of a kind
This commit is contained in:
@@ -21,6 +21,7 @@ class FiveDice:
|
|||||||
|
|
||||||
def value_count(self):
|
def value_count(self):
|
||||||
|
|
||||||
|
counts = {}
|
||||||
count_ones = 0
|
count_ones = 0
|
||||||
count_twos = 0
|
count_twos = 0
|
||||||
count_threes = 0
|
count_threes = 0
|
||||||
@@ -28,49 +29,63 @@ class FiveDice:
|
|||||||
count_fives = 0
|
count_fives = 0
|
||||||
count_sixes = 0
|
count_sixes = 0
|
||||||
|
|
||||||
for face in self.faces():
|
for face in self.faces():
|
||||||
count_ones+=1
|
if face == 1:
|
||||||
if face == 2:
|
count_ones+=1
|
||||||
count_twos+=1
|
if face == 2:
|
||||||
if face == 3:
|
count_twos+=1
|
||||||
count_threes+=1
|
if face == 3:
|
||||||
if face == 4:
|
count_threes+=1
|
||||||
count_fours+=1
|
if face == 4:
|
||||||
if face == 5:
|
count_fours+=1
|
||||||
count_fives+=1
|
if face == 5:
|
||||||
if face == 6:
|
count_fives+=1
|
||||||
count_sixes+=1
|
if face == 6:
|
||||||
counts=
|
count_sixes+=1
|
||||||
'ones':count_ones,
|
|
||||||
'twos':count_twos,
|
counts = {
|
||||||
'threes':count_threes,
|
"ones":count_ones,
|
||||||
'fours':count_fours,
|
"twos":count_twos,
|
||||||
'fives':count_fives,
|
"threes":count_threes,
|
||||||
'sixes':count_sixes,
|
"fours":count_fours,
|
||||||
|
"fives":count_fives,
|
||||||
|
"sixes":count_sixes
|
||||||
|
}
|
||||||
|
|
||||||
return counts
|
return counts
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def all_ones(self):
|
def all_ones(self):
|
||||||
|
value_count = None
|
||||||
value_count = self.value_count()
|
value_count = self.value_count()
|
||||||
|
if value_count["ones"] == 5:
|
||||||
if value_count['ones'] < 5:
|
return True
|
||||||
return False
|
return False
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def is_three_of_a_kind(self):
|
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 = None
|
value_count = self.value_count()
|
||||||
value_count = self.value.count()
|
|
||||||
for value in value_count.values():
|
for value in value_count.values():
|
||||||
if value >= 3:
|
if value >= 3:
|
||||||
return True
|
return True
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
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()
|
dice = FiveDice()
|
||||||
@@ -81,7 +96,28 @@ for trial in tqdm(range(trials)):
|
|||||||
if dice.all_ones():
|
if dice.all_ones():
|
||||||
successes += 1
|
successes += 1
|
||||||
|
|
||||||
print(successes/trials)
|
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))
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class Yahtzee:
|
|||||||
goal.used = False
|
goal.used = False
|
||||||
while self.count_unused_goals() > 0:
|
while self.count_unused_goals() > 0:
|
||||||
self.play_round()
|
self.play_round()
|
||||||
print(f"Your final score was {self.score}"
|
print(f"Your final score was {self.score}")
|
||||||
#says your final score
|
#says your final score
|
||||||
|
|
||||||
def play_round(self):
|
def play_round(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user