generated from mwc/lab_dice
What I did for checkpoint 1 was add two new methods, is_three_of_a_kind and is_four_of_a_kind in FiveDice.
A problem I can solve using classes is simulating a deck of cards for a card game. Each card could be an object with attributes like its suit and its value. A separate class called Deck could hold all the cards, shuffle them, and deal them to players. This setup would make it easier to model real game situations like Blackjack or Poker, since each player could also be a class with their own hand of cards.
This commit is contained in:
@@ -19,6 +19,20 @@ class FiveDice:
|
||||
return False
|
||||
return True
|
||||
|
||||
def is_three_of_a_kind(self):
|
||||
faces = self.faces()
|
||||
for value in range(1,7):
|
||||
if faces.count(value) >= 3:
|
||||
return True
|
||||
return False
|
||||
|
||||
def is_four_of_a_kind(self):
|
||||
faces = self.faces()
|
||||
for value in range(1,7):
|
||||
if faces.count(value) >= 4:
|
||||
return True
|
||||
return False
|
||||
|
||||
dice = FiveDice()
|
||||
successes = 0
|
||||
trials = 1000000
|
||||
@@ -27,7 +41,21 @@ for trial in tqdm(range(trials)):
|
||||
if dice.all_ones():
|
||||
successes += 1
|
||||
|
||||
print(successes/trials)
|
||||
print("All ones probability:",successes/trials)
|
||||
|
||||
successes = 0
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_three_of_a_kind():
|
||||
successes += 1
|
||||
print("Three-of-a-kind probability:", successes/trials)
|
||||
|
||||
successes = 0
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_four_of_a_kind():
|
||||
successes += 1
|
||||
print("Four-of-a-kind probability:", successes/trials)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user