generated from mwc/lab_dice
Finished yahtzee_goals.py with all but the bonuses
Python OOP is just different enough from Java's syntax that it's driving me crazy, but I'm still having fun. Honestly I always looked at OOP as having a different set of "scopes" as functional Programming but the more I've been using OOP the more I see it's just the same scope and access paradigm. You need to either receive or create data, do something with it, and possibly pass data back to something else. This unit is going to be an exercise in patience for me because I see a lot of framework building before actually getting to the result that I want.
This commit is contained in:
parent
33cfbc0284
commit
19cd2472c8
Binary file not shown.
Binary file not shown.
20
play.py
20
play.py
|
@ -3,12 +3,32 @@ from yahtzee_goals import (
|
|||
GoalOnes,
|
||||
GoalTwos,
|
||||
GoalThrees,
|
||||
GoalFours,
|
||||
GoalFives,
|
||||
GoalSixes,
|
||||
ThreeOfAKind,
|
||||
FourOfAKind,
|
||||
FullHouse,
|
||||
SmallStraight,
|
||||
LargeStraight,
|
||||
Yahtzee,
|
||||
Chance
|
||||
)
|
||||
|
||||
goals = [
|
||||
GoalOnes(),
|
||||
GoalTwos(),
|
||||
GoalThrees(),
|
||||
GoalFours(),
|
||||
GoalFives(),
|
||||
GoalSixes(),
|
||||
ThreeOfAKind(),
|
||||
FourOfAKind(),
|
||||
FullHouse(),
|
||||
SmallStraight(),
|
||||
LargeStraight(),
|
||||
Yahtzee(),
|
||||
Chance()
|
||||
]
|
||||
|
||||
game = Yachtzee(goals)
|
||||
|
|
235
yahtzee_goals.py
235
yahtzee_goals.py
|
@ -43,3 +43,238 @@ 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"Six ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
total = 0
|
||||
for die in dice:
|
||||
if die.face == 6:
|
||||
total += 6
|
||||
return total
|
||||
|
||||
class ThreeOfAKind:
|
||||
"Total of all 5 dice"
|
||||
used = False
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Three of a kind ({potential_score})"
|
||||
|
||||
def faces(self, dice):
|
||||
#print([die.face for die in dice])
|
||||
return [die.face for die in dice]
|
||||
|
||||
def is_three_of_a_kind(self,dice):
|
||||
sorted = self.faces(dice)
|
||||
sorted.sort()
|
||||
#print(sorted)
|
||||
if sorted[0] == sorted[1] and sorted[0] == sorted[2] and sorted[0] != sorted[3]:
|
||||
return True
|
||||
elif sorted[1] == sorted[2] and sorted[1] == sorted[3] and sorted[1] != sorted[4]:
|
||||
return True
|
||||
elif sorted[2] == sorted[3] and sorted[2] == sorted[4]:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
# if self.faces(dice)[0] == self.faces(dice)[1] and self.faces(dice)[0] == self.faces(dice)[2] and self.faces(dice)[0] != self.faces(dice)[3]:
|
||||
# return True
|
||||
# elif self.faces(dice)[1] == self.faces(dice)[2] and self.faces(dice)[1] == self.faces(dice)[3] and self.faces(dice)[1] != self.faces(dice)[4]:
|
||||
# return True
|
||||
# elif self.faces(dice)[2] == self.faces(dice)[3] and self.faces(dice)[2] == self.faces(dice)[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 FourOfAKind:
|
||||
"Total of all 5 dice"
|
||||
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):
|
||||
sorted = self.faces(dice)
|
||||
sorted.sort()
|
||||
if sorted[0] != sorted[4] and sorted[0] == sorted[1] and sorted[0] == sorted[2] and sorted[0] == sorted[3]:
|
||||
return True
|
||||
elif sorted[0] != sorted[1] and sorted[1] == sorted[2] and sorted[1] == sorted[3] and sorted[1] == sorted[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 FullHouse:
|
||||
"25 points for three of one number and two of another"
|
||||
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):
|
||||
counts = []
|
||||
for i in range(1,7):
|
||||
counts.append(self.faces(dice).count(i))
|
||||
if 2 in counts and 3 in counts:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def score(self,dice):
|
||||
if self.is_full_house(dice):
|
||||
return 25
|
||||
else:
|
||||
return 0
|
||||
|
||||
class SmallStraight:
|
||||
"Score 30 points if 4 of 5 dice show a sequence"
|
||||
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):
|
||||
small_straights = [[1,2,3,4], [2,3,4,5], [3,4,5,6], [1,2,3,4,5], [2,3,4,5,6]]
|
||||
ss_list = self.faces(dice)
|
||||
check = list(set(ss_list))
|
||||
check.sort()
|
||||
if check in small_straights:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def score(self,dice):
|
||||
if self.is_small_straight(dice):
|
||||
return 30
|
||||
else:
|
||||
return 0
|
||||
|
||||
class LargeStraight:
|
||||
"Score 40 points if all 5 dice show a sequence"
|
||||
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):
|
||||
lorge_straights = [[1,2,3,4,5], [2,3,4,5,6]]
|
||||
ls_list = self.faces(dice)
|
||||
check = list(set(ls_list))
|
||||
check.sort()
|
||||
if check in lorge_straights:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def score(self,dice):
|
||||
if self.is_large_straight(dice):
|
||||
return 40
|
||||
else:
|
||||
return 0
|
||||
|
||||
class Yahtzee:
|
||||
"Score 50 points if all 5 dice are the same value"
|
||||
used = False
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Yahtzee ({potential_score})"
|
||||
|
||||
def faces(self,dice):
|
||||
return [die.face for die in dice]
|
||||
|
||||
def is_yahtzee(self,dice):
|
||||
check = set(self.faces(dice))
|
||||
if len(check) == 1:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def score(self,dice):
|
||||
if self.is_yahtzee(dice):
|
||||
return 50
|
||||
else:
|
||||
return 0
|
||||
|
||||
class Chance:
|
||||
"Scores total of all 5 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
|
Loading…
Reference in New Issue