From 492b70fc43492b851dcc3dfb848e222856510d59 Mon Sep 17 00:00:00 2001 From: jbayati Date: Fri, 14 Nov 2025 09:42:28 -0500 Subject: [PATCH] I didn't see much of a difference with working in OOP in my opinion, it felt the same to me. --- play.py | 26 ++++++++- yahtzee_goals.py | 142 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 163 insertions(+), 5 deletions(-) diff --git a/play.py b/play.py index 773f7f3..ec85fda 100644 --- a/play.py +++ b/play.py @@ -1,14 +1,34 @@ from yahtzee import Yahtzee from yahtzee_goals import ( - GoalOnes, - GoalTwos, + GoalOnes, + GoalTwos, GoalThrees, + GoalFours, + GoalFives, + GoalSixes, + GoalThreeOfAKind, + GoalFourOfAKind, + GoalFullHouse, + GoalSmallStraight, + GoalLargeStraight, + GoalYahtzee, + GoalChance, ) goals = [ - GoalOnes(), + GoalOnes(), GoalTwos(), GoalThrees(), + GoalFours(), + GoalFives(), + GoalSixes(), + GoalThreeOfAKind(), + GoalFourOfAKind(), + GoalFullHouse(), + GoalSmallStraight(), + GoalLargeStraight(), + GoalYahtzee(), + GoalChance(), ] game = Yahtzee(goals) diff --git a/yahtzee_goals.py b/yahtzee_goals.py index 10af574..b80b7f9 100644 --- a/yahtzee_goals.py +++ b/yahtzee_goals.py @@ -14,7 +14,6 @@ class GoalOnes: return total class GoalTwos: - "Two points for each two" def prompt(self, dice): potential_score = self.score(dice) @@ -28,7 +27,6 @@ class GoalTwos: return total class GoalThrees: - "Three points for each three" def prompt(self, dice): potential_score = self.score(dice) @@ -40,3 +38,143 @@ class GoalThrees: if die.face == 3: total += 3 return total + +class GoalFours: + + 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: + + 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: + + 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: + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Three of a kind ({potential_score})" + + def score(self, dice): + counts = {} + total = 0 + for die in dice: + total += die.face + counts[die.face] = counts.get(die.face, 0) + 1 + for c in counts.values(): + if c >= 3: + return total + return 0 + +class GoalFourOfAKind: + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Four of a kind ({potential_score})" + + def score(self, dice): + counts = {} + total = 0 + for die in dice: + total += die.face + counts[die.face] = counts.get(die.face, 0) + 1 + for c in counts.values(): + if c >= 4: + return total + return 0 + +class GoalFullHouse: + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Full House ({potential_score})" + + def score(self, dice): + counts = {} + for die in dice: + counts[die.face] = counts.get(die.face, 0) + 1 + values = sorted(counts.values()) + if values == [2, 3]: + return 25 + return 0 + +class GoalSmallStraight: + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Small Straight ({potential_score})" + + def score(self, dice): + faces = sorted({die.face for die in dice}) + # check for sequences of length 4: 1-4, 2-5, 3-6 + sequences = [list(range(start, start + 4)) for start in (1, 2, 3)] + for seq in sequences: + if all(num in faces for num in seq): + return 30 + return 0 + +class GoalLargeStraight: + + 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: + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Yahtzee ({potential_score})" + + def score(self, dice): + first = dice[0].face + for die in dice: + if die.face != first: + return 0 + return 50 + +class GoalChance: + + 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