I made several changes to the yahtzee_goals python

file in order to provide the various tasks needed to win the
game of Yahtzee.

This style of problem solving has some similarities to our earlier
assignments from the summer, but had a significant number of layers to
it not seen in prior labs. While I certainly understand this method of
thinking and workflow, I do not believe this is how my thinking process
best understands coding. During much of the process, I needed a
significant amount of help to work through and understand even the
smallest of details.
This commit is contained in:
Justin Toombs 2024-02-04 22:31:56 -05:00
parent 8a1f6f677e
commit 1d8878f10a
4 changed files with 279 additions and 0 deletions

Binary file not shown.

Binary file not shown.

20
play.py
View File

@ -3,12 +3,32 @@ 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

@ -14,6 +14,7 @@ class GoalOnes:
total += 1
return total
class GoalTwos:
"Two points for each two"
used = False
@ -29,6 +30,7 @@ class GoalTwos:
total += 2
return total
class GoalThrees:
"Three points for each three"
used = False
@ -43,3 +45,260 @@ 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:
"""Add total of all dice for getting three identical rolls"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Three of a kind ({potential_score})"
def faces(self, dice):
return[die.face for die in dice]
def is_three_of_a_kind(self, dice):
listOfFaces = self.faces(dice)
listOfFaces.sort()
if listOfFaces[0] == listOfFaces[1] and listOfFaces[0] == listOfFaces[2]:
return True
elif listOfFaces[1] == listOfFaces[2] and listOfFaces[1] == listOfFaces[3]:
return True
elif listOfFaces[2] == listOfFaces[3] and listOfFaces[2] == listOfFaces[4]:
return True
else:
return False
def score(self, dice):
if self.is_three_of_a_kind(dice):
total = 0
for die in dice:
total += die.face
return total
else:
return 0
class GoalFourOfAKind:
"""Add total of all dice for getting four identical rolls"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Four of a kind ({potential_score})"
def faces(self, dice):
return[die.face for die in dice]
def is_four_of_a_kind(self, dice):
listOfFaces = self.faces(dice)
listOfFaces.sort()
if listOfFaces[0] == listOfFaces[3]:
return True
elif listOfFaces[1] == listOfFaces[4]:
return True
else:
return False
def score(self, dice):
if self.is_four_of_a_kind(dice):
total = 0
for die in dice:
total += die.face
return total
else:
return 0
class GoalFullHouse:
"""Add 25 points for getting both two and three of a kind at once"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Full house ({potential_score})"
def faces(self, dice):
return[die.face for die in dice]
def is_full_house(self, dice):
listOfFaces = self.faces(dice)
listOfFaces.sort()
if listOfFaces[0] == listOfFaces[1] and listOfFaces[0] == listOfFaces[2] and listOfFaces[3] == listOfFaces[4]:
return True
elif listOfFaces[2] == listOfFaces[3] and listOfFaces[2] == listOfFaces[4] and listOfFaces[0] == listOfFaces[1]:
return True
else:
return False
def score(self, dice):
if self.is_full_house(dice):
return 25
else:
return 0
class GoalSmallStraight:
"""Add 30 points for getting four numbers in order"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Small straight ({potential_score})"
def faces(self, dice):
return[die.face for die in dice]
def is_small_straight(self, dice):
listOfFaces = self.faces(dice)
listOfFaces.sort()
foundSmallStraight = False
for i in range (2):
for j in range(3):
if i == 0:
if j != 2:
if listOfFaces[j] == listOfFaces[j+1] - 1:
continue
else:
break
else:
if listOfFaces[j] == listOfFaces[j+1] - 1:
foundSmallStraight = True
else:
if j != 2:
if listOfFaces[j+1] == listOfFaces[j+2] - 1:
continue
else:
break
else:
if listOfFaces[j+1] == listOfFaces[j+2] - 1:
foundSmallStraight = True
return foundSmallStraight
def score(self, dice):
if self.is_small_straight(dice):
return 30
else:
return 0
class GoalLargeStraight:
"""Add 40 points for getting five numbers in order"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Large straight ({potential_score})"
def faces(self, dice):
return[die.face for die in dice]
def is_large_straight(self, dice):
listOfFaces = self.faces(dice)
listOfFaces.sort()
for j in range(4):
if j != 3:
if listOfFaces[j] == listOfFaces[j+1] - 1:
continue
else:
return False
else:
if listOfFaces[j] == listOfFaces[j+1] - 1:
return True
def score(self, dice):
if self.is_large_straight(dice):
return 40
else:
return 0
class GoalYahtzee:
"""Add 50 points for getting five identical rolls"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Five of a kind ({potential_score})"
def faces(self, dice):
return[die.face for die in dice]
def is_five_of_a_kind(self, dice):
listOfFaces = self.faces(dice)
listOfFaces.sort()
if listOfFaces[0] == listOfFaces[4]:
return True
else:
return False
def score(self, dice):
if self.is_five_of_a_kind(dice):
return 50
else:
return 0
class GoalChance:
"""Add total of 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