generated from mwc/lab_dice
completed checkpoint 3 by adding the yahtzee goals
At first, I had lots of difficulty trying to get used to OOP. However, I do believe I am starting to understand it. It was not difficult to expand the code since OOP was used. This method of problem solving is more organized than how I would have written a Yahtzee program using my past experience. I would have had to use many more functions and it would be tougher to stay organized and expand as we had to do in this assignment. I like how it was structured in the different files. This made it easy to debug. This method of problem solving might be tougher to get started due to its more complex nature, but the coder will reap the benefits when trying to debug or expand the code.
This commit is contained in:
parent
97e1448a66
commit
055f163409
Binary file not shown.
Binary file not shown.
|
@ -36,7 +36,7 @@ class FiveDice:
|
||||||
|
|
||||||
dice = FiveDice()
|
dice = FiveDice()
|
||||||
|
|
||||||
|
'''
|
||||||
successes = 0
|
successes = 0
|
||||||
trials = 1000000
|
trials = 1000000
|
||||||
for trial in tqdm(range(trials)):
|
for trial in tqdm(range(trials)):
|
||||||
|
@ -66,5 +66,5 @@ for trial in tqdm(range(trials)):
|
||||||
print('four of a kind odds')
|
print('four of a kind odds')
|
||||||
print(successes/trials)
|
print(successes/trials)
|
||||||
print('_'*80)
|
print('_'*80)
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
20
play.py
20
play.py
|
@ -3,12 +3,32 @@ from yahtzee_goals import (
|
||||||
GoalOnes,
|
GoalOnes,
|
||||||
GoalTwos,
|
GoalTwos,
|
||||||
GoalThrees,
|
GoalThrees,
|
||||||
|
GoalFours,
|
||||||
|
GoalFives,
|
||||||
|
GoalSixes,
|
||||||
|
GoalThreeOfAKind,
|
||||||
|
GoalFourOfAKind,
|
||||||
|
GoalFullHouse,
|
||||||
|
GoalSmallStraight,
|
||||||
|
GoalLargeStraight,
|
||||||
|
GoalYahtzee,
|
||||||
|
GoalChance
|
||||||
)
|
)
|
||||||
|
|
||||||
goals = [
|
goals = [
|
||||||
GoalOnes(),
|
GoalOnes(),
|
||||||
GoalTwos(),
|
GoalTwos(),
|
||||||
GoalThrees(),
|
GoalThrees(),
|
||||||
|
GoalFours(),
|
||||||
|
GoalFives(),
|
||||||
|
GoalSixes(),
|
||||||
|
GoalThreeOfAKind(),
|
||||||
|
GoalFourOfAKind(),
|
||||||
|
GoalFullHouse(),
|
||||||
|
GoalSmallStraight(),
|
||||||
|
GoalLargeStraight(),
|
||||||
|
GoalYahtzee(),
|
||||||
|
GoalChance()
|
||||||
]
|
]
|
||||||
|
|
||||||
game = Yachtzee(goals)
|
game = Yachtzee(goals)
|
||||||
|
|
187
yahtzee_goals.py
187
yahtzee_goals.py
|
@ -43,3 +43,190 @@ class GoalThrees:
|
||||||
if die.face == 3:
|
if die.face == 3:
|
||||||
total += 3
|
total += 3
|
||||||
return total
|
return total
|
||||||
|
|
||||||
|
class GoalFours:
|
||||||
|
"Four points for each four"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"Fours ({potential_score})"
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
for die in dice:
|
||||||
|
if die.face == 4:
|
||||||
|
total += 4
|
||||||
|
return total
|
||||||
|
|
||||||
|
class GoalFives:
|
||||||
|
"Five points for each five"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"Fives ({potential_score})"
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
for die in dice:
|
||||||
|
if die.face == 5:
|
||||||
|
total += 5
|
||||||
|
return total
|
||||||
|
|
||||||
|
class GoalSixes:
|
||||||
|
"Six points for each six"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"Sixes ({potential_score})"
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
for die in dice:
|
||||||
|
if die.face == 6:
|
||||||
|
total += 6
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class GoalThreeOfAKind:
|
||||||
|
"Sum of all dice for 3 of a kind"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"3 of a kind ({potential_score})"
|
||||||
|
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
flag = False
|
||||||
|
of_each_counter={1:0, 2:0, 3:0, 4:0, 5:0, 6:0}
|
||||||
|
for die in dice:
|
||||||
|
|
||||||
|
of_each_counter[die.face]=of_each_counter[die.face]+1
|
||||||
|
for num_of in of_each_counter.values():
|
||||||
|
if num_of>=3:
|
||||||
|
flag = True
|
||||||
|
if flag:
|
||||||
|
for die in dice:
|
||||||
|
total += die.face
|
||||||
|
return total
|
||||||
|
|
||||||
|
class GoalFourOfAKind:
|
||||||
|
"Sum of all dice for 4 of a kind"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"4 of a kind ({potential_score})"
|
||||||
|
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
flag = False
|
||||||
|
of_each_counter={1:0, 2:0, 3:0, 4:0, 5:0, 6:0}
|
||||||
|
for die in dice:
|
||||||
|
of_each_counter[die.face]=of_each_counter[die.face]+1
|
||||||
|
for num_of in of_each_counter.values():
|
||||||
|
if num_of>=4:
|
||||||
|
flag = True
|
||||||
|
if flag:
|
||||||
|
for die in dice:
|
||||||
|
total += die.face
|
||||||
|
return total
|
||||||
|
|
||||||
|
class GoalFullHouse:
|
||||||
|
"25 points for full house"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"Full House ({potential_score})"
|
||||||
|
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
of_each_counter={1:0, 2:0, 3:0, 4:0, 5:0, 6:0}
|
||||||
|
for die in dice:
|
||||||
|
of_each_counter[die.face]=of_each_counter[die.face]+1
|
||||||
|
if 3 in of_each_counter.values() and 2 in of_each_counter.values():
|
||||||
|
total=25
|
||||||
|
return total
|
||||||
|
|
||||||
|
class GoalSmallStraight:
|
||||||
|
"30 points for small straight"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"Small Straight ({potential_score})"
|
||||||
|
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
nums=[]
|
||||||
|
for die in dice:
|
||||||
|
nums.append(die.face)
|
||||||
|
op1=(1 in nums and 2 in nums and 3 in nums and 4 in nums)
|
||||||
|
op2=(2 in nums and 3 in nums and 4 in nums and 5 in nums)
|
||||||
|
op3=(3 in nums and 4 in nums and 5 in nums and 6 in nums)
|
||||||
|
if op1 or op2 or op3:
|
||||||
|
total=30
|
||||||
|
return total
|
||||||
|
|
||||||
|
class GoalLargeStraight:
|
||||||
|
"40 points for Large straight"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"Large Straight ({potential_score})"
|
||||||
|
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
nums=[]
|
||||||
|
for die in dice:
|
||||||
|
nums.append(die.face)
|
||||||
|
op1=(1 in nums and 2 in nums and 3 in nums and 4 in nums and 5 in nums)
|
||||||
|
op2=(2 in nums and 3 in nums and 4 in nums and 5 in nums and 6 in nums)
|
||||||
|
if op1 or op2:
|
||||||
|
total=40
|
||||||
|
return total
|
||||||
|
|
||||||
|
class GoalYahtzee:
|
||||||
|
"50 points for Yahtzee"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"Yahtzee ({potential_score})"
|
||||||
|
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
flag=True
|
||||||
|
for die in dice:
|
||||||
|
if die!=dice[0].face:
|
||||||
|
flag=False
|
||||||
|
if flag:
|
||||||
|
total=50
|
||||||
|
return total
|
||||||
|
|
||||||
|
class GoalChance:
|
||||||
|
"Sum of all dice for Chance"
|
||||||
|
used = False
|
||||||
|
|
||||||
|
def prompt(self, dice):
|
||||||
|
potential_score = self.score(dice)
|
||||||
|
return f"Chance ({potential_score})"
|
||||||
|
|
||||||
|
|
||||||
|
def score(self, dice):
|
||||||
|
total = 0
|
||||||
|
for die in dice:
|
||||||
|
total += die.face
|
||||||
|
return total
|
||||||
|
|
Loading…
Reference in New Issue