Added docstrings to yahtzee to outline methods

When I'm writing code, I'm trying to solve a specific problem,
but when I write documentation, I'm trying to clearly convey what
is actually going through my head. The shift is sort of subtle but
it's awkward because I know what I'm thinking and what I'm trying
to do, but as I write documentation there's a constant uncertainty
of "will someone else be able to understand this, even if it's me
in a week?"

I certainly should start using docstrings, that's for sure. Writing
documentation in general is something I should put more resources into
because generally I only do it when I have already written some
pseudocode planning out my process and that sort of naturally flows
into becoming comments or docstrings. In reality, a lot of times
I'm trying to code as fast as I can think and so as things develop
quickly, I'm not taking the time to actually explain what's going on
and to go back later and add those kinds of docstrings feels awkward,
even though that exercise is still worthwhile in the grand scheme
of the design.
This commit is contained in:
Pat Wick 2024-02-03 20:09:02 -05:00
parent 35a8bd78ae
commit 33cfbc0284
3 changed files with 30 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -9,12 +9,17 @@ class Yachtzee:
self.goals = goals self.goals = goals
self.dice = [Die() for num in range(5)] self.dice = [Die() for num in range(5)]
"""Plays a game of Yahtzee, round by round until all goals have been used.
"""
def play(self): def play(self):
print("Welcome to Yachtzee!") print("Welcome to Yachtzee!")
while self.count_unused_goals() > 0: while self.count_unused_goals() > 0:
self.play_round() self.play_round()
print(f"Your final score was {self.score}") print(f"Your final score was {self.score}")
"""Plays a single round of Yahtzee, allowing up to 3 re-rolls and ending when
the player chooses a goal to use to score.
"""
def play_round(self): def play_round(self):
print("=" * 80) print("=" * 80)
self.rolls_left = 3 self.rolls_left = 3
@ -25,10 +30,16 @@ class Yachtzee:
goal.used = True goal.used = True
self.score += goal.score(self.dice) self.score += goal.score(self.dice)
"""Reports stats between rolls, including current score, re-rolls remaining
and the current values of the dice.
"""
def show_status(self): def show_status(self):
dice = ', '.join([str(die) for die in self.dice]) dice = ', '.join([str(die) for die in self.dice])
print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.") print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.")
"""Allows the user to choose the goal to use on the scoresheet from the list of
remaining unused goals.
"""
def choose_goal(self): def choose_goal(self):
options = [] options = []
unused_goals = self.get_unused_goals() unused_goals = self.get_unused_goals()
@ -45,6 +56,9 @@ class Yachtzee:
else: else:
return unused_goals[choice] return unused_goals[choice]
"""Processes the user's choice for validity, moving on after receiving a valid
goal choice.
"""
def get_choice(self, options): def get_choice(self, options):
print("What would you like to do?") print("What would you like to do?")
for i, option in enumerate(options): for i, option in enumerate(options):
@ -55,6 +69,9 @@ class Yachtzee:
choice = input("> ") choice = input("> ")
return int(choice) return int(choice)
"""Only returns true when the choice from the user is one of the numbered
list items available to them.
"""
def option_choice_is_valid(self, choice, options): def option_choice_is_valid(self, choice, options):
if not choice.isdigit(): if not choice.isdigit():
return False return False
@ -64,9 +81,13 @@ class Yachtzee:
return False return False
return True return True
"""Used to determine when the game is over because all goals have been used.
"""
def count_unused_goals(self): def count_unused_goals(self):
return len(self.get_unused_goals()) return len(self.get_unused_goals())
"""Generates the list of unused goals at that point in the game.
"""
def get_unused_goals(self): def get_unused_goals(self):
unused_goals = [] unused_goals = []
for goal in self.goals: for goal in self.goals:
@ -74,6 +95,8 @@ class Yachtzee:
unused_goals.append(goal) unused_goals.append(goal)
return unused_goals return unused_goals
"""Removes a re-roll available to the player and re-rolls selected dice.
"""
def reroll(self): def reroll(self):
self.rolls_left -= 1 self.rolls_left -= 1
choices = self.get_reroll_choices() choices = self.get_reroll_choices()
@ -81,6 +104,8 @@ class Yachtzee:
for die in dice_to_reroll: for die in dice_to_reroll:
die.roll() die.roll()
"""Collects the individual dice to be rerolled after validation.
"""
def get_dice_to_reroll(self, choice_ints): def get_dice_to_reroll(self, choice_ints):
dice_to_reroll = [] dice_to_reroll = []
for die in self.dice: for die in self.dice:
@ -89,6 +114,9 @@ class Yachtzee:
dice_to_reroll.append(die) dice_to_reroll.append(die)
return dice_to_reroll return dice_to_reroll
"""Collects which dice to be rerolled from the user as a single,
multi-place number, like "6442".
"""
def get_reroll_choices(self): def get_reroll_choices(self):
print("Which dice do you want to re-roll?") print("Which dice do you want to re-roll?")
choices = input("> ") choices = input("> ")
@ -98,6 +126,8 @@ class Yachtzee:
choice_ints = [int(digit) for digit in choices] choice_ints = [int(digit) for digit in choices]
return choice_ints return choice_ints
"""Checks that the dice to be rerolled are available to be rerolled.
"""
def reroll_choices_are_valid(self, choices_str): def reroll_choices_are_valid(self, choices_str):
if not choices_str.isdigit(): if not choices_str.isdigit():
return False return False