checkpoint 3

This is different because I feel the code is a lot shorter than it would
be for units 1 and 2. It also keeps things very organized.
This commit is contained in:
Lauren Dawnkaski 2024-11-30 13:30:51 -05:00
parent 2967e780b3
commit 1be376713f
3 changed files with 186 additions and 0 deletions

26
play.py
View File

@ -3,12 +3,38 @@ from yahtzee_goals import (
GoalOnes,
GoalTwos,
GoalThrees,
GoalFours,
GoalFives,
GoalSixes,
GoalThreeofakind,
GoalFourofakind,
GoalFullHouse,
GoalSmallStraight,
GoalLargeStraight,
GoalYahtzee,
GoalChance
)
goals = [
GoalOnes(),
GoalTwos(),
GoalThrees(),
GoalFours(),
GoalFives(),
GoalSixes(),
GoalThreeofakind(),
GoalFourofakind(),
GoalFullHouse(),
GoalSmallStraight(),
GoalLargeStraight(),
GoalYahtzee(),
GoalChance()
]
game = Yachtzee(goals)

View File

@ -153,3 +153,4 @@ class Yachtzee:
if die.face in choice_ints:
choice_ints.remove(die.face)
return len(choice_ints) == 0

View File

@ -43,3 +43,162 @@ class GoalThrees:
if die.face == 3:
total += 3
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 if at least three dice have the same face"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Three of a kind ({potential_score})"
def score(self, dice):
face_counts={}
for die in dice:
face_counts[die.face] = face_counts.get(die.face, 0)+1
if any(count >=3 for count in face_counts.values()):
return sum(die.face for die in dice)
return 0
class GoalFourofakind:
"sum of all dice if at least four dice have the same face"
used=False
def prompt(self, dice):
potential_score=self.score(dice)
return f"Four of a kind({potential_score})"
def score(self, dice):
face_counts={}
for die in dice:
face_counts[die.face]=face_counts.get(die.face, 0)+1
if any(count >= 4 for count in face_counts.values()):
return sum(die.face for die in dice)
return 0
class GoalFullHouse:
"25 points for a full house"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Full House ({potential_score})"
def score(self, dice):
face_counts = {}
for die in dice:
face_counts[die.face] = face_counts.get(die.face, 0) + 1
if sorted(face_counts.values()) == [2, 3]:
return 25
return 0
class GoalSmallStraight:
"30 points for a small straight"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Small Straight ({potential_score})"
def score(self, dice):
face_counts={}
for die in dice:
face_counts[die.face]=face_counts.get(die.face,0)+1
small_straights = [[1, 2, 3, 4],[2, 3, 4, 5],[3, 4, 5, 6]]
for straight in small_straights:
if all(face in face_counts for face in straight):
return 30
return 0
class GoalLargeStraight:
"40 points for a large straight"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Large Straight ({potential_score})"
def score(self, dice):
face_counts = {}
for die in dice:
face_counts[die.face] = face_counts.get(die.face, 0) + 1
large_straights = [[1, 2, 3, 4, 5],[2, 3, 4, 5, 6]]
for straight in large_straights:
if all(face in face_counts for face in straight):
return 40
return 0
class GoalYahtzee:
"50 points for a Yahtzee"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Yahtzee ({potential_score})"
def score(self, dice):
face_counts = {}
for die in dice:
face_counts[die.face] = face_counts.get(die.face, 0) + 1
if any(count == 5 for count in face_counts.values()):
return 50
return 0
class GoalChance:
"Sum of all dice"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Chance ({potential_score})"
def score(self, dice):
total = sum(die.face for die in dice)
return total