generated from mwc/lab_dice
The changes made here involved providing
docstrings to explain what was occurring in each of the methods. My thinking for this portion came more naturally than some of my coding for this lab as it drew on my English teaching background in order to describe what was happening. I may use this in the future to understand in greater detail what is occurring in my code.
This commit is contained in:
parent
a6b4a1aadd
commit
8a1f6f677e
36
yahtzee.py
36
yahtzee.py
|
@ -8,13 +8,18 @@ class Yachtzee:
|
|||
self.score = 0
|
||||
self.goals = goals
|
||||
self.dice = [Die() for num in range(5)]
|
||||
|
||||
|
||||
"""Allows the user the play a full game of Yahtzee.
|
||||
"""
|
||||
def play(self):
|
||||
print("Welcome to Yachtzee!")
|
||||
while self.count_unused_goals() > 0:
|
||||
self.play_round()
|
||||
print(f"Your final score was {self.score}")
|
||||
|
||||
"""Allows the user to play a round of Yahtzee, during which three rerolls can occur, and finishes the round by
|
||||
selecting a goal based on the acquired rolls.
|
||||
"""
|
||||
def play_round(self):
|
||||
print("=" * 80)
|
||||
self.rolls_left = 3
|
||||
|
@ -25,10 +30,15 @@ class Yachtzee:
|
|||
goal.used = True
|
||||
self.score += goal.score(self.dice)
|
||||
|
||||
"""Allows the user to see their score, number of rolls left, and the dice that have been rolled thus far while
|
||||
still in the current round.
|
||||
"""
|
||||
def show_status(self):
|
||||
dice = ', '.join([str(die) for die in self.dice])
|
||||
print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.")
|
||||
|
||||
|
||||
"""Allows the user to select an unused goal after a round has completed.
|
||||
"""
|
||||
def choose_goal(self):
|
||||
options = []
|
||||
unused_goals = self.get_unused_goals()
|
||||
|
@ -45,6 +55,8 @@ class Yachtzee:
|
|||
else:
|
||||
return unused_goals[choice]
|
||||
|
||||
"""Ensures the unused goal selected is a valid choice.
|
||||
"""
|
||||
def get_choice(self, options):
|
||||
print("What would you like to do?")
|
||||
for i, option in enumerate(options):
|
||||
|
@ -54,7 +66,9 @@ class Yachtzee:
|
|||
print("Sorry, that's not a valid choice.")
|
||||
choice = input("> ")
|
||||
return int(choice)
|
||||
|
||||
|
||||
"""Ensures that an available number has been selected.
|
||||
"""
|
||||
def option_choice_is_valid(self, choice, options):
|
||||
if not choice.isdigit():
|
||||
return False
|
||||
|
@ -64,9 +78,13 @@ class Yachtzee:
|
|||
return False
|
||||
return True
|
||||
|
||||
"""Used to determine when the game is over after all goals have been achieved.
|
||||
"""
|
||||
def count_unused_goals(self):
|
||||
return len(self.get_unused_goals())
|
||||
|
||||
"""Returns the names of any goals not met.
|
||||
"""
|
||||
def get_unused_goals(self):
|
||||
unused_goals = []
|
||||
for goal in self.goals:
|
||||
|
@ -74,6 +92,8 @@ class Yachtzee:
|
|||
unused_goals.append(goal)
|
||||
return unused_goals
|
||||
|
||||
"""Rerolls the dice selected by the user.
|
||||
"""
|
||||
def reroll(self):
|
||||
self.rolls_left -= 1
|
||||
choices = self.get_reroll_choices()
|
||||
|
@ -81,6 +101,8 @@ class Yachtzee:
|
|||
for die in dice_to_reroll:
|
||||
die.roll()
|
||||
|
||||
"""Determining the dice that have been selected by the user to be rerolled and compiles the values together.
|
||||
"""
|
||||
def get_dice_to_reroll(self, choice_ints):
|
||||
dice_to_reroll = []
|
||||
for die in self.dice:
|
||||
|
@ -88,7 +110,9 @@ class Yachtzee:
|
|||
choice_ints.remove(die.face)
|
||||
dice_to_reroll.append(die)
|
||||
return dice_to_reroll
|
||||
|
||||
|
||||
"""Allows the user to reroll specific dice that were rolled by typing the specific values to be rerolled.
|
||||
"""
|
||||
def get_reroll_choices(self):
|
||||
print("Which dice do you want to re-roll?")
|
||||
choices = input("> ")
|
||||
|
@ -97,7 +121,9 @@ class Yachtzee:
|
|||
choices = input("> ")
|
||||
choice_ints = [int(digit) for digit in choices]
|
||||
return choice_ints
|
||||
|
||||
|
||||
"""Validates the dice that are able to be rerolled.
|
||||
"""
|
||||
def reroll_choices_are_valid(self, choices_str):
|
||||
if not choices_str.isdigit():
|
||||
return False
|
||||
|
|
Loading…
Reference in New Issue