generated from mwc/lab_dice
Updated the logic to have a three and four of a kind logic.
I set the list to a variable because I kept getting an error saying the object was not iterable.
This commit is contained in:
parent
c46657de4f
commit
3d5c2e1d8c
|
@ -19,6 +19,41 @@ class FiveDice:
|
|||
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
|
||||
|
@ -27,7 +62,27 @@ for trial in tqdm(range(trials)):
|
|||
if dice.all_ones():
|
||||
successes += 1
|
||||
|
||||
print(successes/trials)
|
||||
print(f"All Ones: {successes/trials}")
|
||||
|
||||
dice = FiveDice()
|
||||
successes = 0
|
||||
trials = 1000000
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_three_of_a_kind():
|
||||
successes += 1
|
||||
|
||||
print(f"Three of a Kind: {successes/trials}")
|
||||
|
||||
dice = FiveDice()
|
||||
successes = 0
|
||||
trials = 1000000
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_four_of_a_kind():
|
||||
successes += 1
|
||||
|
||||
print(f"Four of a Kind: {successes/trials}")
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue