generated from mwc/lab_dice
You could simulate possible outcomes with simulations
I was thinking about something readable for other people rather than something that does something
This commit is contained in:
16
yahtzee.py
16
yahtzee.py
@@ -10,6 +10,11 @@ class Yahtzee:
|
||||
self.dice = [Die() for num in range(5)]
|
||||
|
||||
def play(self):
|
||||
"""Play an entire game.
|
||||
Starts by greeting the user, then plays rounds until all the goals
|
||||
have been used. When the game is over, tells the player their final
|
||||
score.
|
||||
"""
|
||||
print("Welcome to Yachtzee!")
|
||||
self.score = 0
|
||||
for goal in self.goals:
|
||||
@@ -19,6 +24,7 @@ class Yahtzee:
|
||||
print(f"Your final score was {self.score}")
|
||||
|
||||
def play_round(self):
|
||||
"""Plays a single round of Yahtzee"""
|
||||
print("=" * 80)
|
||||
self.rolls_left = 3
|
||||
for die in self.dice:
|
||||
@@ -29,10 +35,12 @@ class Yahtzee:
|
||||
self.score += goal.score(self.dice)
|
||||
|
||||
def show_status(self):
|
||||
"""Shows your current status in Yahtzee"""
|
||||
dice = ', '.join([str(die) for die in self.dice])
|
||||
print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.")
|
||||
|
||||
def choose_goal(self):
|
||||
"""Chooses a goal for the player"""
|
||||
options = []
|
||||
unused_goals = self.get_unused_goals()
|
||||
for goal in unused_goals:
|
||||
@@ -49,6 +57,7 @@ class Yahtzee:
|
||||
return unused_goals[choice]
|
||||
|
||||
def get_choice(self, options):
|
||||
"""Shows the player the choices they are allowed to do"""
|
||||
print("What would you like to do?")
|
||||
for i, option in enumerate(options):
|
||||
print(f"{i}. {option}")
|
||||
@@ -59,6 +68,7 @@ class Yahtzee:
|
||||
return int(choice)
|
||||
|
||||
def option_choice_is_valid(self, choice, options):
|
||||
"""Allows player to do the option choice"""
|
||||
if not choice.isdigit():
|
||||
return False
|
||||
if int(choice) < 0:
|
||||
@@ -68,9 +78,11 @@ class Yahtzee:
|
||||
return True
|
||||
|
||||
def count_unused_goals(self):
|
||||
"""Counts the unused goals for player"""
|
||||
return len(self.get_unused_goals())
|
||||
|
||||
def get_unused_goals(self):
|
||||
"""Shows the player the unused goals"""
|
||||
unused_goals = []
|
||||
for goal in self.goals:
|
||||
if not goal.used:
|
||||
@@ -78,6 +90,7 @@ class Yahtzee:
|
||||
return unused_goals
|
||||
|
||||
def reroll(self):
|
||||
"""Rerolls the dice"""
|
||||
self.rolls_left -= 1
|
||||
choices = self.get_reroll_choices()
|
||||
dice_to_reroll = self.get_dice_to_reroll(choices)
|
||||
@@ -85,6 +98,7 @@ class Yahtzee:
|
||||
die.roll()
|
||||
|
||||
def get_dice_to_reroll(self, choice_ints):
|
||||
"""Finds the dice that can be rerolled"""
|
||||
dice_to_reroll = []
|
||||
for die in self.dice:
|
||||
if die.face in choice_ints:
|
||||
@@ -93,6 +107,7 @@ class Yahtzee:
|
||||
return dice_to_reroll
|
||||
|
||||
def get_reroll_choices(self):
|
||||
"""Shows the player what dice can be rerolled"""
|
||||
print("Which dice do you want to re-roll?")
|
||||
choices = input("> ")
|
||||
while not self.reroll_choices_are_valid(choices):
|
||||
@@ -102,6 +117,7 @@ class Yahtzee:
|
||||
return choice_ints
|
||||
|
||||
def reroll_choices_are_valid(self, choices_str):
|
||||
"""Gets what reroll choices are valid"""
|
||||
if not choices_str.isdigit():
|
||||
return False
|
||||
choice_ints = [int(digit) for digit in choices_str]
|
||||
|
||||
Reference in New Issue
Block a user