generated from mwc/lab_dice
I added the goals to the Yahtzee game.
Checkpoint 3: My experience working with OOP is that I understand most of it but is still a little confusing. I wasn't able to figure out all of the goals for the game. This is different than how I would have written the program in units 1 and 2 as I would have just made a bunch of functions and called them throughout my code. Now we can use classes.
This commit is contained in:
10
play.py
10
play.py
@@ -3,12 +3,22 @@ from yahtzee_goals import (
|
||||
GoalOnes,
|
||||
GoalTwos,
|
||||
GoalThrees,
|
||||
GoalFours,
|
||||
GoalFives,
|
||||
GoalSixes,
|
||||
GoalThreeofaKind,
|
||||
GoalFourofaKind
|
||||
)
|
||||
|
||||
goals = [
|
||||
GoalOnes(),
|
||||
GoalTwos(),
|
||||
GoalThrees(),
|
||||
GoalFours(),
|
||||
GoalFives(),
|
||||
GoalSixes(),
|
||||
GoalThreeofaKind(),
|
||||
GoalFourofaKind()
|
||||
]
|
||||
|
||||
game = Yahtzee(goals)
|
||||
|
||||
@@ -40,3 +40,93 @@ class GoalThrees:
|
||||
if die.face == 3:
|
||||
total += 3
|
||||
return total
|
||||
|
||||
class GoalFours:
|
||||
"Four points for each four"
|
||||
|
||||
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"
|
||||
|
||||
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"
|
||||
|
||||
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:
|
||||
"3*number points for each number"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"ThreeofaKind ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
total = 0
|
||||
for die in dice:
|
||||
if die.face == 1:
|
||||
total += 1*3
|
||||
if die.face == 2:
|
||||
total += 2*3
|
||||
if die.face == 3:
|
||||
total += 3*3
|
||||
if die.face == 4:
|
||||
total += 4*3
|
||||
if die.face == 5:
|
||||
total += 5*3
|
||||
if die.face == 6:
|
||||
total += 6*3
|
||||
return total
|
||||
|
||||
class GoalFourofaKind:
|
||||
"4*number points for each number"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"FourofaKind ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
total = 0
|
||||
for die in dice:
|
||||
if die.face == 1:
|
||||
total += 1*4
|
||||
if die.face == 2:
|
||||
total += 2*4
|
||||
if die.face == 3:
|
||||
total += 3*4
|
||||
if die.face == 4:
|
||||
total += 4*4
|
||||
if die.face == 5:
|
||||
total += 5*4
|
||||
if die.face == 6:
|
||||
total += 6*4
|
||||
return total
|
||||
Reference in New Issue
Block a user