From c07295c253fb447001b9d53865301f47e1adb43f Mon Sep 17 00:00:00 2001 From: root Date: Thu, 12 Dec 2024 00:06:05 -0500 Subject: [PATCH] Checkpoint 3: I feel like OOP was harder to wrap my head around. I had to use and edit the code from the already written parts for many of the new classes we had to write, but I think editing those parts to fit each new goal was helpful in getting to know how classes worked. --- play.py | 20 +++++ yahtzee_goals.py | 225 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+) diff --git a/play.py b/play.py index a8e683c..2386f6b 100644 --- a/play.py +++ b/play.py @@ -3,12 +3,32 @@ from yahtzee_goals import ( GoalOnes, GoalTwos, GoalThrees, + GoalFours, + GoalFives, + GoalSixes, + GoalThreeOfAKind, + GoalFourOfAKind, + GoalFullHouse, + GoalSmallStraight, + GoalLargeStraight, + GoalChance, + GoalYahtzee ) goals = [ GoalOnes(), GoalTwos(), GoalThrees(), + GoalFours(), + GoalFives(), + GoalSixes(), + GoalThreeOfAKind(), + GoalFourOfAKind(), + GoalFullHouse(), + GoalSmallStraight(), + GoalLargeStraight(), + GoalChance(), + GoalYahtzee() ] game = Yachtzee(goals) diff --git a/yahtzee_goals.py b/yahtzee_goals.py index fce4e5a..dbd5589 100644 --- a/yahtzee_goals.py +++ b/yahtzee_goals.py @@ -43,3 +43,228 @@ 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: + "If three of a kind, total of all dice" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Three of a Kind ({potential_score})" + + def score(self, dice): + total = 0 + for number in [1, 2, 3, 4, 5, 6]: + count = 0 + for die in dice: + if die.face == number: + count = count + 1 + if count >= 3: + for die in dice: + total += die.face + return total + +class GoalFourOfAKind: + "If four of a kind, total of all dice" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Four of a Kind ({potential_score})" + + def score(self, dice): + total = 0 + for number in [1, 2, 3, 4, 5, 6]: + count = 0 + for die in dice: + if die.face == number: + count = count + 1 + if count >= 4: + for die in dice: + total += die.face + return total + +class GoalFullHouse: + "If three of one number and two of another, 25 points" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Full House ({potential_score})" + + def score(self, dice): + total = 0 + count1 = 0 + count2 = 0 + count3 = 0 + count4 = 0 + count5 = 0 + count6 = 0 + for die in dice: + if die.face == 1: + count1 = count1 + 1 + if die.face == 2: + count2 = count2 + 1 + if die.face == 3: + count3 = count3 + 1 + if die.face == 4: + count4 = count4 + 1 + if die.face == 5: + count5 = count5 + 1 + if die.face == 6: + count6 = count6 + 1 + if count1 == 3 or count2 == 3 or count3 == 3 or count4 == 3 or count5 == 3 or count6 == 3: + if count1 == 2 or count2 == 2 or count3 == 2 or count4 == 2 or count5 == 2 or count6 == 2: + total = 25 + return total + +class GoalSmallStraight: + "If four in a row (1,2,3,4 or 2,3,4,5 or 3,4,5,6), get 30 points" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Small Straight ({potential_score})" + + def score(self, dice): + total = 0 + count1 = 0 + count2 = 0 + count3 = 0 + count4 = 0 + count5 = 0 + count6 = 0 + for die in dice: + if die.face == 1: + count1 = 1 + if die.face == 2: + count2 = 1 + if die.face == 3: + count3 = 1 + if die.face == 4: + count4 = 1 + if die.face == 5: + count5 = 1 + if die.face == 6: + count6 = 1 + if count1 == 1 and count2 == 1 and count3 == 1 and count4 == 1: + total = 30 + elif count2 == 1 and count3 == 1 and count4 == 1 and count5 == 1: + total = 30 + elif count3 == 1 and count4 == 1 and count5 == 1 and count6 == 1: + total = 30 + return total + +class GoalLargeStraight: + "If five in a row (1,2,3,4,5 or 2,3,4,5,6), get 40 points" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Large Straight ({potential_score})" + + def score(self, dice): + total = 0 + count1 = 0 + count2 = 0 + count3 = 0 + count4 = 0 + count5 = 0 + count6 = 0 + for die in dice: + if die.face == 1: + count1 = 1 + if die.face == 2: + count2 = 1 + if die.face == 3: + count3 = 1 + if die.face == 4: + count4 = 1 + if die.face == 5: + count5 = 1 + if die.face == 6: + count6 = 1 + if count1 == 1 and count2 == 1 and count3 == 1 and count4 == 1 and count5 == 1: + total = 40 + elif count2 == 1 and count3 == 1 and count4 == 1 and count5 == 1 and count6 == 1: + total = 40 + return total + +class GoalYahtzee: + "If five of a kind, get 50 points" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"YAHTZEE ({potential_score})" + + def score(self, dice): + total = 0 + for number in [1, 2, 3, 4, 5, 6]: + count = 0 + for die in dice: + if die.face == number: + count = count + 1 + if count >= 5: + for die in dice: + total = 50 + return total + +class GoalChance: + "Any five dice, total all dice" + 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 \ No newline at end of file