generated from mwc/lab_dice
it was an okay experience. It didnt feel much different in units 1 or 2
This commit is contained in:
14
play.py
14
play.py
@@ -1,15 +1,15 @@
|
||||
from yahtzee import Yahtzee
|
||||
from yahtzee_goals import (
|
||||
GoalOnes,
|
||||
GoalTwos,
|
||||
GoalThrees,
|
||||
GoalOnes, GoalTwos, GoalThrees, GoalFours, GoalFives, GoalSixes,
|
||||
GoalThreeOfAKind, GoalFourOfAKind, GoalFullHouse,
|
||||
GoalSmallStraight, GoalLargeStraight, GoalYahtzee, GoalChance
|
||||
)
|
||||
|
||||
goals = [
|
||||
GoalOnes(),
|
||||
GoalTwos(),
|
||||
GoalThrees(),
|
||||
GoalOnes(), GoalTwos(), GoalThrees(), GoalFours(), GoalFives(), GoalSixes(),
|
||||
GoalThreeOfAKind(), GoalFourOfAKind(), GoalFullHouse(),
|
||||
GoalSmallStraight(), GoalLargeStraight(), GoalYahtzee(), GoalChance()
|
||||
]
|
||||
|
||||
game = Yahtzee(goals)
|
||||
game.play()
|
||||
game.play()
|
||||
138
yahtzee_goals.py
138
yahtzee_goals.py
@@ -1,4 +1,3 @@
|
||||
|
||||
class GoalOnes:
|
||||
"One point for each one"
|
||||
|
||||
@@ -40,3 +39,140 @@ class GoalThrees:
|
||||
if die.face == 3:
|
||||
total += 3
|
||||
return total
|
||||
|
||||
class GoalFours:
|
||||
"Four points for each four"
|
||||
|
||||
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"
|
||||
|
||||
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"
|
||||
|
||||
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 if three or more are the same"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Three of a Kind ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
faces = [die.face for die in dice]
|
||||
for face in set(faces):
|
||||
if faces.count(face) >= 3:
|
||||
return sum(faces)
|
||||
return 0
|
||||
|
||||
class GoalFourOfAKind:
|
||||
"Sum of all dice if four or more are the same"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Four of a Kind ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
faces = [die.face for die in dice]
|
||||
for face in set(faces):
|
||||
if faces.count(face) >= 4:
|
||||
return sum(faces)
|
||||
return 0
|
||||
|
||||
class GoalFullHouse:
|
||||
"25 points if three of one kind and two of another"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Full House ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
faces = [die.face for die in dice]
|
||||
counts = {}
|
||||
for face in faces:
|
||||
counts[face] = counts.get(face, 0) + 1
|
||||
if sorted(counts.values()) == [2, 3]:
|
||||
return 25
|
||||
return 0
|
||||
|
||||
class GoalSmallStraight:
|
||||
"30 points for four consecutive numbers"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Small Straight ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
faces = sorted(set([die.face for die in dice]))
|
||||
straights = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
|
||||
for straight in straights:
|
||||
if all(num in faces for num in straight):
|
||||
return 30
|
||||
return 0
|
||||
|
||||
class GoalLargeStraight:
|
||||
"40 points for five consecutive numbers"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Large Straight ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
faces = sorted([die.face for die in dice])
|
||||
if faces == [1, 2, 3, 4, 5] or faces == [2, 3, 4, 5, 6]:
|
||||
return 40
|
||||
return 0
|
||||
|
||||
class GoalYahtzee:
|
||||
"50 points if all five dice are the same"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Yahtzee ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
faces = [die.face for die in dice]
|
||||
if len(set(faces)) == 1:
|
||||
return 50
|
||||
return 0
|
||||
|
||||
class GoalChance:
|
||||
"Sum of all dice"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Chance ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
return sum([die.face for die in dice])
|
||||
Reference in New Issue
Block a user