generated from mwc/lab_dice
My experience with OOP was interesting, as I had
the opportunity to experience it from the perspective my students do. OOP is helpful in this case because it allows us to break down the steps of yahtzee and compartmentalize them so that, if we want to change one thing, we do not need to change the entire program. If we were to try and write this program in Units 1 and 2, we'd need to use multiple different functions, which each depend on each other. This would be more unwieldy and much more difficult to make updates to.
This commit is contained in:
parent
a272ef3459
commit
2a28d355df
Binary file not shown.
|
@ -0,0 +1,35 @@
|
||||||
|
from die import Die
|
||||||
|
|
||||||
|
from yahtzee_goals import ThreeOfAKind
|
||||||
|
from yahtzee_goals import FourOfAKind
|
||||||
|
from yahtzee_goals import FullHouse
|
||||||
|
from yahtzee_goals import SmallStraight
|
||||||
|
from yahtzee_goals import LargeStraight
|
||||||
|
from yahtzee_goals import Chance
|
||||||
|
from yahtzee_goals import Yahtzee
|
||||||
|
|
||||||
|
def makeDice(faces):
|
||||||
|
dice = []
|
||||||
|
for face in faces:
|
||||||
|
die = Die()
|
||||||
|
die.face = face
|
||||||
|
dice.append(die)
|
||||||
|
return dice
|
||||||
|
|
||||||
|
three = makeDice([1,1,1,3,4])
|
||||||
|
four = makeDice([1,2,4,4,4])
|
||||||
|
full = makeDice([3,3,5,6,6])
|
||||||
|
sstraight = makeDice([1,3,5,5,6])
|
||||||
|
lstraight = makeDice([1,2,3,4,5])
|
||||||
|
chance = makeDice([2,2,3,4,6])
|
||||||
|
yahtzee = makeDice([1,4,4,4,4])
|
||||||
|
|
||||||
|
goalThree = ThreeOfAKind()
|
||||||
|
goalFour = FourOfAKind()
|
||||||
|
goalFull = FullHouse()
|
||||||
|
goalSstraight = SmallStraight()
|
||||||
|
goalLstraight = LargeStraight()
|
||||||
|
goalChance = Chance()
|
||||||
|
goalYahtzee = Yahtzee()
|
||||||
|
|
||||||
|
print(goalYahtzee.score(yahtzee))
|
118
yahtzee_goals.py
118
yahtzee_goals.py
|
@ -93,30 +93,30 @@ class ThreeOfAKind:
|
||||||
"Total of all six dice, if there are at least three of one number"
|
"Total of all six dice, if there are at least three of one number"
|
||||||
used = False
|
used = False
|
||||||
|
|
||||||
|
def diceFaces(self,dice):
|
||||||
|
return [die.face for die in dice]
|
||||||
|
|
||||||
def prompt(self,dice):
|
def prompt(self,dice):
|
||||||
potential_score = self.score(dice)
|
potential_score = self.score(dice)
|
||||||
return f"Three of a Kind ({potential_score})"
|
return f"Three of a Kind ({potential_score})"
|
||||||
|
|
||||||
def is_three_of_a_kind(self,dice):
|
def is_three_of_a_kind(self, faces):
|
||||||
facesList = []
|
faces.sort()
|
||||||
for die in dice:
|
if faces[0]==faces[2]:
|
||||||
facesList += die.face()
|
|
||||||
facesList.sort()
|
|
||||||
if facesList[0]==facesList[2]:
|
|
||||||
return True
|
return True
|
||||||
elif facesList[1]==facesList[3]:
|
elif faces[1]==faces[3]:
|
||||||
return True
|
return True
|
||||||
elif facesList[3]==facesList[5]:
|
elif faces[2]==faces[4]:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def score(self,dice):
|
def score(self,dice):
|
||||||
if self.is_three_of_a_kind(dice):
|
faces = self.diceFaces(dice)
|
||||||
|
if self.is_three_of_a_kind(faces):
|
||||||
total = 0
|
total = 0
|
||||||
for die in dice:
|
for face in faces:
|
||||||
if die.face == 6:
|
total += face
|
||||||
total+= 6
|
|
||||||
return total
|
return total
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
@ -125,27 +125,28 @@ class FourOfAKind:
|
||||||
"Total of all six dice, if there are at least four of one number"
|
"Total of all six dice, if there are at least four of one number"
|
||||||
used = False
|
used = False
|
||||||
|
|
||||||
|
def diceFaces(self,dice):
|
||||||
|
return [die.face for die in dice]
|
||||||
|
|
||||||
def prompt(self,dice):
|
def prompt(self,dice):
|
||||||
potential_score = self.score(dice)
|
potential_score = self.score(dice)
|
||||||
return f"Four of a Kind ({potential_score})"
|
return f"Four of a Kind ({potential_score})"
|
||||||
|
|
||||||
def is_four_of_a_kind(self, dice):
|
def is_four_of_a_kind(self, faces):
|
||||||
facesList = []
|
faces.sort()
|
||||||
for die in dice:
|
if faces[0]==faces[3]:
|
||||||
facesList += die.face()
|
|
||||||
if facesList[0]==facesList[3]:
|
|
||||||
return True
|
return True
|
||||||
elif facesList[1]==facesList[4]:
|
elif faces[1]==faces[4]:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def score(self,dice):
|
def score(self,dice):
|
||||||
if self.is_four_of_a_kind(dice):
|
faces = self.diceFaces(dice)
|
||||||
|
if self.is_four_of_a_kind(faces):
|
||||||
total = 0
|
total = 0
|
||||||
for die in dice:
|
for face in faces:
|
||||||
if die.face == 6:
|
total += face
|
||||||
total+= 6
|
|
||||||
return total
|
return total
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
@ -154,23 +155,25 @@ class FullHouse:
|
||||||
"If there are two of one number and three of another, earn 25 points"
|
"If there are two of one number and three of another, earn 25 points"
|
||||||
used = False
|
used = False
|
||||||
|
|
||||||
|
def diceFaces(self,dice):
|
||||||
|
return [die.face for die in dice]
|
||||||
|
|
||||||
def prompt(self,dice):
|
def prompt(self,dice):
|
||||||
potential_score = self.score(dice)
|
potential_score = self.score(dice)
|
||||||
return f"Full House ({potential_score})"
|
return f"Full House ({potential_score})"
|
||||||
|
|
||||||
def is_full_house(self, dice):
|
def is_full_house(self, faces):
|
||||||
facesList = []
|
faces.sort()
|
||||||
for die in dice:
|
if faces[0] == faces[1] and faces[2] == faces[4]:
|
||||||
facesList += die.face()
|
|
||||||
if facesList[0] == facesList[1] and facesList[2] == facesList[4]:
|
|
||||||
return True
|
return True
|
||||||
elif facesList[0] == facesList[2] and facesList[3] == facesList[4]:
|
elif faces[0] == faces[2] and faces[3] == faces[4]:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def score(self, dice):
|
def score(self, dice):
|
||||||
if self.is_full_house(dice):
|
faces = self.diceFaces(dice)
|
||||||
|
if self.is_full_house(faces):
|
||||||
return 25
|
return 25
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
@ -180,25 +183,27 @@ class SmallStraight:
|
||||||
"If there is any sequence of four numbers, earn 30 points"
|
"If there is any sequence of four numbers, earn 30 points"
|
||||||
used = False
|
used = False
|
||||||
|
|
||||||
|
def diceFaces(self,dice):
|
||||||
|
return [die.face for die in dice]
|
||||||
|
|
||||||
def prompt(self, dice):
|
def prompt(self, dice):
|
||||||
potential_score = self.score(dice)
|
potential_score = self.score(dice)
|
||||||
return f"Small Straight ({potential_score})"
|
return f"Small Straight ({potential_score})"
|
||||||
|
|
||||||
def is_small_straight(self,dice):
|
def is_small_straight(self,faces):
|
||||||
facesList = []
|
faces.sort()
|
||||||
for die in dice:
|
|
||||||
facesList += die.face()
|
|
||||||
count = 0
|
count = 0
|
||||||
for i in range(0,3):
|
|
||||||
if facesList[i] == facesList[i+1] - 1:
|
if faces[0]+1 == faces[1] and faces[1]+1 == faces[2] and faces[2]+1 == faces[3]:
|
||||||
count += 1
|
return True
|
||||||
if count >= 4:
|
elif faces[1]+1 == faces[2] and faces[2]+1 == faces[3] and faces[3]+1 == faces[4]:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def score(self,dice):
|
def score(self,dice):
|
||||||
if self.is_small_straight(dice):
|
faces = self.diceFaces(dice)
|
||||||
|
if self.is_small_straight(faces):
|
||||||
return 30
|
return 30
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
@ -209,25 +214,29 @@ class LargeStraight:
|
||||||
"If there is any sequence of 5 numbers, earn 40 points"
|
"If there is any sequence of 5 numbers, earn 40 points"
|
||||||
used = False
|
used = False
|
||||||
|
|
||||||
|
def diceFaces(self,dice):
|
||||||
|
return [die.face for die in dice]
|
||||||
|
|
||||||
def prompt(self,dice):
|
def prompt(self,dice):
|
||||||
potential_score = self.score(dice)
|
potential_score = self.score(dice)
|
||||||
return f"Large Straignt ({potential_score})"
|
return f"Large Straignt ({potential_score})"
|
||||||
|
|
||||||
def is_Large_Straight(self,dice):
|
def is_Large_Straight(self,faces):
|
||||||
facesList = []
|
faces.sort()
|
||||||
for die in dice:
|
|
||||||
facesList += die.face()
|
|
||||||
count = 0
|
count = 0
|
||||||
for i in range(0,3):
|
for i in range(0,4):
|
||||||
if facesList[i] != facesList[i+1] - 1:
|
if faces[i] == faces[i+1] - 1:
|
||||||
count += 1
|
count += 1
|
||||||
if count == 5:
|
else:
|
||||||
|
count = 0
|
||||||
|
if count == 4:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def score(self,dice):
|
def score(self,dice):
|
||||||
if self.is_Large_straight(dice):
|
faces = self.diceFaces(dice)
|
||||||
|
if self.is_Large_Straight(faces):
|
||||||
return 40
|
return 40
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
@ -251,25 +260,24 @@ class Yahtzee:
|
||||||
"Six of a Kind 50 points, 100 points for yahtzees after the first"
|
"Six of a Kind 50 points, 100 points for yahtzees after the first"
|
||||||
used = False
|
used = False
|
||||||
|
|
||||||
|
def diceFaces(self,dice):
|
||||||
|
return [die.face for die in dice]
|
||||||
|
|
||||||
def prompt(self,dice):
|
def prompt(self,dice):
|
||||||
|
|
||||||
potential_score = self.score(dice)
|
potential_score = self.score(dice)
|
||||||
return f"Yahtzee ({potential_score})"
|
return f"Yahtzee ({potential_score})"
|
||||||
|
|
||||||
def is_yahtzee(self):
|
def is_yahtzee(self, faces):
|
||||||
facesList = []
|
faces.sort()
|
||||||
for die in dice:
|
if faces[0] == faces[4]:
|
||||||
facesList += die.face()
|
|
||||||
|
|
||||||
if facesList[0] == facesList[4]:
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def score(self,dice):
|
def score(self,dice):
|
||||||
if self.is_yahtzee(dice) and used == True:
|
faces = self.diceFaces(dice)
|
||||||
return 100
|
if self.is_yahtzee(faces):
|
||||||
elif self.is_yahtzee(dice) and used != True:
|
|
||||||
return 50
|
return 50
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
Loading…
Reference in New Issue