I didn't see much of a difference with working in OOP in my opinion, it felt the same to me.

This commit is contained in:
jbayati
2025-11-14 09:42:28 -05:00
parent 4f0ec4d5ce
commit 492b70fc43
2 changed files with 163 additions and 5 deletions

26
play.py
View File

@@ -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)

View File

@@ -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