generated from mwc/lab_dice
I added the docstrings.
My thinking was different in that I had to try to figure out what all the pieces were doing as opposed to creating the logic myself. Including comments helps remind me as well as helps other future users of my code.
This commit is contained in:
parent
a034fda385
commit
c46657de4f
39
yahtzee.py
39
yahtzee.py
|
@ -5,12 +5,20 @@ class Yahtzee:
|
|||
This version of Yahtzee is initialized with a list of goals.
|
||||
"""
|
||||
def __init__(self, goals):
|
||||
"""Initiates the instance of a Yahtzee game.
|
||||
sets the score to 0.
|
||||
provides a list of the goals for the game.
|
||||
Creates a list of 5 instances of Die."""
|
||||
|
||||
self.score = 0
|
||||
self.goals = goals
|
||||
self.dice = [Die() for num in range(5)]
|
||||
|
||||
def play(self):
|
||||
print("Welcome to Yachtzee!")
|
||||
"""Welcomes the user to Yahtzee.
|
||||
Prints the remaining goals.
|
||||
If all goals are met, then the final score is printed """
|
||||
print("Welcome to Yahtzee!")
|
||||
self.score = 0
|
||||
for goal in self.goals:
|
||||
goal.used = False
|
||||
|
@ -19,6 +27,12 @@ class Yahtzee:
|
|||
print(f"Your final score was {self.score}")
|
||||
|
||||
def play_round(self):
|
||||
"""prints a row of = to separate the rounds visually.
|
||||
set the number or rolls left to 3.
|
||||
Rolls 5 dice and shows your current score, how many rolls you have left
|
||||
and the list of dice values. Then list the options for action
|
||||
(goals left and re-roll if they have any rolls left). Then sets the goal
|
||||
they used's used status to True and updates the score based on the option they chose. """
|
||||
print("=" * 80)
|
||||
self.rolls_left = 3
|
||||
for die in self.dice:
|
||||
|
@ -29,10 +43,14 @@ class Yahtzee:
|
|||
self.score += goal.score(self.dice)
|
||||
|
||||
def show_status(self):
|
||||
"""called from play_round to show the current stus to include the
|
||||
current score, how many rolls they have left and what the values for each of the 5
|
||||
dice are after the roll."""
|
||||
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):
|
||||
"""" create a list of options using unused goals and re-roll if there are rolls left."""
|
||||
options = []
|
||||
unused_goals = self.get_unused_goals()
|
||||
for goal in unused_goals:
|
||||
|
@ -49,6 +67,10 @@ class Yahtzee:
|
|||
return unused_goals[choice]
|
||||
|
||||
def get_choice(self, options):
|
||||
""" present the options for the user in a numbered list.
|
||||
If the user makes a valid choice, the choice is returned.
|
||||
If the user makes an invalid chioice, an error of Sorry, that's not a valid choice is a returned.
|
||||
Allow for additional input."""
|
||||
print("What would you like to do?")
|
||||
for i, option in enumerate(options):
|
||||
print(f"{i}. {option}")
|
||||
|
@ -59,6 +81,7 @@ class Yahtzee:
|
|||
return int(choice)
|
||||
|
||||
def option_choice_is_valid(self, choice, options):
|
||||
""" Choice must be a digit that is between 0 and the length of the option list."""
|
||||
if not choice.isdigit():
|
||||
return False
|
||||
if int(choice) < 0:
|
||||
|
@ -68,9 +91,11 @@ class Yahtzee:
|
|||
return True
|
||||
|
||||
def count_unused_goals(self):
|
||||
""" return the length of the list of unused goals"""
|
||||
return len(self.get_unused_goals())
|
||||
|
||||
def get_unused_goals(self):
|
||||
""" create and return a list of goals that have not been used."""
|
||||
unused_goals = []
|
||||
for goal in self.goals:
|
||||
if not goal.used:
|
||||
|
@ -78,6 +103,9 @@ class Yahtzee:
|
|||
return unused_goals
|
||||
|
||||
def reroll(self):
|
||||
""" Decrease the rolls left by 1. Ask which dice they would like to re-roll.
|
||||
Die to reroll must be in the die face list. Re-roll the die selected.
|
||||
"""
|
||||
self.rolls_left -= 1
|
||||
choices = self.get_reroll_choices()
|
||||
dice_to_reroll = self.get_dice_to_reroll(choices)
|
||||
|
@ -85,6 +113,8 @@ class Yahtzee:
|
|||
die.roll()
|
||||
|
||||
def get_dice_to_reroll(self, choice_ints):
|
||||
""" Receives the dice that need to be re-rolled and replaces then with new rolls.
|
||||
Returns a list of die that can be re-rolled."""
|
||||
dice_to_reroll = []
|
||||
for die in self.dice:
|
||||
if die.face in choice_ints:
|
||||
|
@ -93,15 +123,18 @@ class Yahtzee:
|
|||
return dice_to_reroll
|
||||
|
||||
def get_reroll_choices(self):
|
||||
print("Which dice do you want to re-roll?")
|
||||
""" Allows user to select the dice to re-roll and ensures the options are valid within the list."""
|
||||
print("Which dice do you want to re-roll? - enter all dice values with no spaces between them.")
|
||||
choices = input("> ")
|
||||
while not self.reroll_choices_are_valid(choices):
|
||||
print("Please enter the numbers on dice you want to re-roll.")
|
||||
print("Please enter the numbers on dice you want to re-roll. No spaces.")
|
||||
choices = input("> ")
|
||||
choice_ints = [int(digit) for digit in choices]
|
||||
return choice_ints
|
||||
|
||||
def reroll_choices_are_valid(self, choices_str):
|
||||
""" Converts the choices provided by the user to integers and
|
||||
removes all the choice from the list of 5 dice that were rolled."""
|
||||
if not choices_str.isdigit():
|
||||
return False
|
||||
choice_ints = [int(digit) for digit in choices_str]
|
||||
|
|
Loading…
Reference in New Issue