diff --git a/play.py b/play.py index a8e683c..6565bc8 100644 --- a/play.py +++ b/play.py @@ -1,14 +1,20 @@ from yahtzee import Yachtzee -from yahtzee_goals import ( - GoalOnes, - GoalTwos, - GoalThrees, -) +from yahtzee_goals import * goals = [ GoalOnes(), GoalTwos(), GoalThrees(), + GoalFours(), + GoalFives(), + GoalSixes(), + ThreeofaKind(), + FourofaKind(), + FullHouse(), + SmallStraight(), + LargeStraight(), + YAHTZEE(), + Chance() ] game = Yachtzee(goals) diff --git a/testing.py b/testing.py new file mode 100644 index 0000000..5226615 --- /dev/null +++ b/testing.py @@ -0,0 +1,12 @@ +from yahtzee_goals import SmallStraight +from die import Die + +goal = SmallStraight() +dice= [Die(),Die(),Die(),Die(),Die()] +dice[0]. face = 6 +dice[1]. face = 4 +dice[2]. face = 1 +dice[3]. face = 3 +dice[4]. face = 4 + +print(dice, goal.score(dice)) \ No newline at end of file diff --git a/yahtzee_goals.py b/yahtzee_goals.py index fce4e5a..c75035c 100644 --- a/yahtzee_goals.py +++ b/yahtzee_goals.py @@ -43,3 +43,257 @@ 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 ThreeofaKind: + """If 3 or more dice are the same, returns the value of all 5 dice, + otherwise returns a score of 0""" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Three of a Kind ({potential_score})" + + def score(self, dice): + if self.check_3_of_a_kind(dice): + return self.dice_total(dice) + else: + return 0 + + def check_3_of_a_kind(self,dice): + for value in range(7): + instances = 0 + for die in dice: + if die.face == value: + instances +=1 + if instances >= 3: + return True + return False + + def dice_total(self,dice): + total = 0 + for die in dice: + total += die.face + return total + +class FourofaKind: + """If 4 or more dice are the same, returns the value of all 5 dice, + otherwise returns a score of 0""" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Four of a Kind ({potential_score})" + + def score(self, dice): + if self.check_4_of_a_kind(dice): + return self.dice_total(dice) + else: + return 0 + + def check_4_of_a_kind(self,dice): + for value in range(7): + instances = 0 + for die in dice: + if die.face == value: + instances +=1 + if instances >= 4: + return True + return False + + def dice_total(self,dice): + total = 0 + for die in dice: + total += die.face + return total + +class FullHouse: + """If there are 3 of the same dice and 2 of the same dice, returns 25, + otherwise returns a score of 0""" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Full House ({potential_score})" + + def score(self, dice): + if self.check_full_house(dice): + return 25 + else: + return 0 + + def check_full_house(self,dice): + for value in range(7): + instances = 0 + for die in dice: + if die.face == value: + instances +=1 + if instances == 3: + triple_dice = value + list_die = [1,2,3,4,5,6] + list_die.remove(value) + for value in list_die: + instances_2 = 0 + for die in dice: + if die.face == value: + instances_2 +=1 + if instances_2 ==2: + return True + return False + +class SmallStraight: + """If there are 4 dice that can be put in consecutive order, + return 30, otherwise return a score of 0""" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Small Straight ({potential_score})" + + def score(self, dice): + if self.check_small_straight(dice): + return 30 + else: + return 0 + + def check_small_straight(self,dice): + dice_faces =[] + for die in dice: + dice_faces.append(die.face) + dice_faces.sort() + '''need to check for duplicates first, do not need to check index 0 or 4''' + if dice_faces[1]==dice_faces[2]: + dice_faces.remove(dice_faces[1]) + if dice_faces[2]==dice_faces[3]: + dice_faces.remove(dice_faces[2]) + '''now check if ordered list increases by 1, do not need to check last index''' + if dice_faces[0] + 1 == dice_faces[1]: + if dice_faces[1] + 1 == dice_faces[2]: + if dice_faces[2] + 1 == dice_faces[3]: + return True + else: + '''now reverse the list and check if they decrease by 1, do not need to check last index''' + dice_faces.sort(reverse=True) + if dice_faces[0] - 1 == dice_faces[1]: + if dice_faces[1] - 1 == dice_faces[2]: + if dice_faces[2] - 1 == dice_faces[3]: + return True + else: + return False + +class LargeStraight: + """If there are 5 dice that can be put in consecutive order, + return 40, otherwise return a score of 0""" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"Large Straight ({potential_score})" + + def score(self, dice): + if self.check_large_straight(dice): + return 40 + else: + return 0 + + def check_large_straight(self,dice): + dice_faces =[] + for die in dice: + dice_faces.append(die.face) + dice_faces.sort() + if dice_faces[0] + 1 == dice_faces[1]: + if dice_faces[1] + 1 == dice_faces[2]: + if dice_faces[2] + 1 == dice_faces[3]: + if dice_faces[3] + 1 ==dice_faces[4]: + return True + else: + return False + +class YAHTZEE: + """If there are 5 dice that are all the same, + return 50, otherwise return a score of 0""" + used = False + + def prompt(self, dice): + potential_score = self.score(dice) + return f"YAHTZEE ({potential_score})" + + def score(self, dice): + if self.check_YAHTZEE(dice): + return 50 + else: + return 0 + + def check_YAHTZEE(self,dice): + dice_faces =[] + for die in dice: + dice_faces.append(die.face) + if dice_faces[0]==dice_faces[1]==dice_faces[2]==dice_faces[3]==dice_faces[4]: + return True + else: + return False + +class Chance: + """Returns the total of all 5 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 + + + def dice_total(self,dice): + total = 0 + for die in dice: + total += die.face + return total \ No newline at end of file