Checkpoint 3: Added 7 goals!

The most immediate thing that came to my mind with my experience with
OOP is how unused to thinking in that way and working with objects I am.
In yahtzee_goals.py, I don't know that it would have occurred to me to
create a class for each goal. I think I was thinking of objects pretty
literally as "things" one can do stuff to or with, e.g. dice that can be
rolled. Goals don't immediately jump out as me in the same way, which
perhaps highlights how my understanding of OOP probably still could be
further developed. I thought a bit more about the difference between
methods and functions as I worked through Yahtzee. They seem similar in
the sense that either can do things or return values, but methods are
more restrictive in that they're tied to particular objects/classes. I
had some difficulty when writing the GoalThreeOfAKind class since I kept
getting an error that the number of arguments being passed was wrong. I
"realized," in the sense that I eventually figured out through trial and
error, that self was being passed even if I didn't specify it should be.

I imagine if I were to try doing this in unit 1 or 2, instead of
treating the game as an object interacting with other objects (the dice
and goals), it'd probably be some sort of loop with functions being
called to mimic the methods and a declaration of a lot of global
variables to keep track of things like score, which goals have been
used, the faces of the dice, etc. I feel like with all these functions
and variables I might need to be more mindful with making sure to only
edit the things I don't need to keep track of if I were to go that
route. I think one thing that I'm realizing generally is that I need to
consider how best to use multiple files to write things in so that I can
focus on particular elements, e.g. goals vs. how the game plays. I hope
with more experience I'll get better at planning out how my projects can
be written/organized.
This commit is contained in:
Cory 2024-02-12 13:52:53 -05:00
parent 637a5e5191
commit a3bc21bee7
2 changed files with 158 additions and 0 deletions

14
play.py
View File

@ -3,12 +3,26 @@ from yahtzee_goals import (
GoalOnes,
GoalTwos,
GoalThrees,
GoalFours,
GoalFives,
GoalSixes,
GoalThreeOfAKind,
GoalFourOfAKind,
GoalFullHouse,
GoalChance
)
goals = [
GoalOnes(),
GoalTwos(),
GoalThrees(),
GoalFours(),
GoalFives(),
GoalSixes(),
GoalThreeOfAKind(),
GoalFourOfAKind(),
GoalFullHouse(),
GoalChance()
]
game = Yachtzee(goals)

View File

@ -43,3 +43,147 @@ 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:
"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