I just addded the docstrings

My thinking was more backward in this question, seeing waht we had to write what it was going to give us, when i code its more forward thinkinhg
I might include it, it is easy but also if im sturggling i wont want to, ill just want to code it and be done lol
This commit is contained in:
mollychi
2025-11-09 20:49:15 -05:00
parent b52f578504
commit 90c88b4385

View File

@@ -10,6 +10,10 @@ class Yahtzee:
self.dice = [Die() for num in range(5)] self.dice = [Die() for num in range(5)]
def play(self): 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!") print("Welcome to Yachtzee!")
self.score = 0 self.score = 0
for goal in self.goals: for goal in self.goals:
@@ -19,6 +23,8 @@ 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):
"""This just rolls the dice and figures out what each number was
rolled on each dice"""
print("=" * 80) print("=" * 80)
self.rolls_left = 3 self.rolls_left = 3
for die in self.dice: for die in self.dice:
@@ -29,10 +35,13 @@ class Yahtzee:
self.score += goal.score(self.dice) self.score += goal.score(self.dice)
def show_status(self): def show_status(self):
"""This shows the player their rolls"""
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):
""" shwos them the number of 1's 2's and 3's
and offers them the options of rerolling or keeping their rolls"""
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 +58,7 @@ class Yahtzee:
return unused_goals[choice] return unused_goals[choice]
def get_choice(self, options): def get_choice(self, options):
""" gives feedback to help the player decide what to do next"""
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 +69,7 @@ class Yahtzee:
return int(choice) return int(choice)
def option_choice_is_valid(self, choice, options): def option_choice_is_valid(self, choice, options):
"""Makes sure that what the player puts in is usable """
if not choice.isdigit(): if not choice.isdigit():
return False return False
if int(choice) < 0: if int(choice) < 0:
@@ -78,6 +89,8 @@ class Yahtzee:
return unused_goals return unused_goals
def reroll(self): def reroll(self):
"""If the player decides to reroll, this is what the computer must to
to start that process"""
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 +98,8 @@ class Yahtzee:
die.roll() die.roll()
def get_dice_to_reroll(self, choice_ints): def get_dice_to_reroll(self, choice_ints):
"""If the player decides to reroll, this is what the computer must to
to start that process"""
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 +108,8 @@ class Yahtzee:
return dice_to_reroll return dice_to_reroll
def get_reroll_choices(self): def get_reroll_choices(self):
"""Asks the player what dice number do they want to reroll, all dice of that
number will be rerolled"""
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 +119,7 @@ class Yahtzee:
return choice_ints return choice_ints
def reroll_choices_are_valid(self, choices_str): def reroll_choices_are_valid(self, choices_str):
"""This is the code to adjust for the reroll """
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]