Checkpoimt 2

While writing the docstrings I needed to go through all the code to understand what it was doing and then run the simulation to experience wgat was written before I could descrbe it.
I also had to go back and forth between methods. I think writing docstrings are super helpful in determining how the code works or what we want our code to do. It is similar to writing out objectives.
This commit is contained in:
ilmabura
2025-11-18 23:42:01 -05:00
parent b1d374d1d5
commit 203a47a44d

View File

@@ -5,11 +5,16 @@ class Yahtzee:
This version of Yahtzee is initialized with a list of goals. This version of Yahtzee is initialized with a list of goals.
""" """
def __init__(self, goals): def __init__(self, goals):
"""Initializes the scores to 0, a list of goals, and creates 5 dice for the game to roll
"""
self.score = 0 self.score = 0
self.goals = goals self.goals = goals
self.dice = [Die() for num in range(5)] self.dice = [Die() for num in range(5)]
def play(self): def play(self):
"""Runs the full Yahtzee game until all goals are used.
Resets the score and marks all goals as unused. Then plays the rounds until no more goals can be used and displays the final score.
"""
print("Welcome to Yachtzee!") print("Welcome to Yachtzee!")
self.score = 0 self.score = 0
for goal in self.goals: for goal in self.goals:
@@ -19,6 +24,9 @@ class Yahtzee:
print(f"Your final score was {self.score}") print(f"Your final score was {self.score}")
def play_round(self): def play_round(self):
"""Runs one round of Yahtzee.
Rolls all five dice and displays the status of the round, allows player to choose a goal and marks it as used, and tallies up score.
"""
print("=" * 80) print("=" * 80)
self.rolls_left = 3 self.rolls_left = 3
for die in self.dice: for die in self.dice:
@@ -29,10 +37,16 @@ class Yahtzee:
self.score += goal.score(self.dice) self.score += goal.score(self.dice)
def show_status(self): def show_status(self):
"""Displays the current score, rolls left, and dice faces.
"""
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}.")
def choose_goal(self): def choose_goal(self):
"""Asks the player to choose a goal or roll the dice.
Displays the unused goals and the potential score.
Keeps track of re-rolls and unused goals until player only has one option left.
"""
options = [] options = []
unused_goals = self.get_unused_goals() unused_goals = self.get_unused_goals()
for goal in unused_goals: for goal in unused_goals:
@@ -49,6 +63,9 @@ class Yahtzee:
return unused_goals[choice] return unused_goals[choice]
def get_choice(self, options): def get_choice(self, options):
"""Prompts the player to select an option from the list of goals and re-rolls.
Displays all available choices with indices. Validates user input and returns the index of the option selected.
"""
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):
print(f"{i}. {option}") print(f"{i}. {option}")
@@ -59,6 +76,9 @@ class Yahtzee:
return int(choice) return int(choice)
def option_choice_is_valid(self, choice, options): def option_choice_is_valid(self, choice, options):
"""Ensures that the player's option is valid.
Checks if the input is within the provided options and returns true or false accordingly.
"""
if not choice.isdigit(): if not choice.isdigit():
return False return False
if int(choice) < 0: if int(choice) < 0:
@@ -68,9 +88,13 @@ class Yahtzee:
return True return True
def count_unused_goals(self): def count_unused_goals(self):
"""Returns the number of unused goals
"""
return len(self.get_unused_goals()) return len(self.get_unused_goals())
def get_unused_goals(self): def get_unused_goals(self):
"""Returns a list of unused gials by iterating through all goals and collecting False occurences
"""
unused_goals = [] unused_goals = []
for goal in self.goals: for goal in self.goals:
if not goal.used: if not goal.used:
@@ -78,6 +102,8 @@ class Yahtzee:
return unused_goals return unused_goals
def reroll(self): def reroll(self):
"""Re-rolls the selected dice
"""
self.rolls_left -= 1 self.rolls_left -= 1
choices = self.get_reroll_choices() choices = self.get_reroll_choices()
dice_to_reroll = self.get_dice_to_reroll(choices) dice_to_reroll = self.get_dice_to_reroll(choices)
@@ -85,6 +111,8 @@ class Yahtzee:
die.roll() die.roll()
def get_dice_to_reroll(self, choice_ints): def get_dice_to_reroll(self, choice_ints):
"""Determines which dice needs to be re-rolled based on player input.
"""
dice_to_reroll = [] dice_to_reroll = []
for die in self.dice: for die in self.dice:
if die.face in choice_ints: if die.face in choice_ints:
@@ -93,6 +121,8 @@ class Yahtzee:
return dice_to_reroll return dice_to_reroll
def get_reroll_choices(self): def get_reroll_choices(self):
"""Prompts player to enter which dice values to re-roll.
"""
print("Which dice do you want to re-roll?") print("Which dice do you want to re-roll?")
choices = input("> ") choices = input("> ")
while not self.reroll_choices_are_valid(choices): while not self.reroll_choices_are_valid(choices):
@@ -102,6 +132,8 @@ class Yahtzee:
return choice_ints return choice_ints
def reroll_choices_are_valid(self, choices_str): def reroll_choices_are_valid(self, choices_str):
"""Validates player input for dice to re-roll.
"""
if not choices_str.isdigit(): if not choices_str.isdigit():
return False return False
choice_ints = [int(digit) for digit in choices_str] choice_ints = [int(digit) for digit in choices_str]