lab_dice/yahtzee_goals.py

189 lines
5.9 KiB
Python

class GoalOnes:
"One point for each one"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Ones ({potential_score})"
def score(self, dice):
total = 0
for die in dice:
if die.face == 1:
total += 1
return total
class GoalTwos:
"Two points for each two"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Twos ({potential_score})"
def score(self, dice):
total = 0
for die in dice:
if die.face == 2:
total += 2
return total
class GoalThrees:
"Three points for each three"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Threes ({potential_score})"
def score(self, dice):
total = 0
for die in dice:
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:
"Total of all dice if there is three of a kind"
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
if self.threek(dice):
for die in dice:
total=total + die.face
return total
def threek(self,dice):
nmuch = {} # create an empty dictionary to associate each face rolled with its frequency
for die in dice: # go through each dice rolled
if die.face in nmuch: # check if the dictionary already has the face included, and if so increase its frequency by 1
nmuch[die.face] += 1
else:
nmuch[die.face] = 1 # add the face to the dictionary with frequency 1 if it has not already been included
for die,face in nmuch.items(): # check if there is three of a kind for a given face, and if so, return True
if face==3:
return True
return False # otherwise return False
class GoalFourOfAKind:
"Total of all dice if there is four of a kind"
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
if self.fourk(dice):
for die in dice:
total=total + die.face
return total
def fourk(self,dice):
nmuch = {} # create an empty dictionary to associate each face rolled with its frequency
for die in dice: # go through each dice rolled
if die.face in nmuch: # check if the dictionary already has the face included, and if so increase its frequency by 1
nmuch[die.face] += 1
else:
nmuch[die.face] = 1 # add the face to the dictionary with frequency 1 if it has not already been included
for die,face in nmuch.items(): # check if there is three of a kind for a given face, and if so, return True
if face==4:
return True
return False # otherwise return False
class GoalFullHouse:
"25 points if there is full house"
used = False
def prompt(self, dice):
potential_score=self.score(dice)
return f"Full house ({potential_score})"
def score(self, dice):
if self.full(dice):
return 25
return 0
def nofakind(self,dice,n):
nmuch = {} # create an empty dictionary to associate each face rolled with its frequency
for die in dice: # go through each dice rolled
if die.face in nmuch: # check if the dictionary already has the face included, and if so increase its frequency by 1
nmuch[die.face] += 1
else:
nmuch[die.face] = 1 # add the face to the dictionary with frequency 1 if it has not already been included
for die,face in nmuch.items(): # check if there is n of a kind for a given face, and if so, return True
if face==n:
return True
return False # otherwise return False
def full(self,dice):
if self.nofakind(dice,3) and self.nofakind(dice,2): # check if there is a three of a kind and a two of a kind (both exact)
return True
else:
return False
class GoalChance:
"Total of all dice by chance"
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=total + die.face
return total