I dont know, it was just playing yahtzee. More then likely I will use docstrings for my projects.

This commit is contained in:
cdonahue
2025-11-14 09:28:28 -05:00
parent d09183c61c
commit 7661d6dc54

View File

@@ -1,15 +1,15 @@
from die import Die
class Yahtzee:
"""A command-line Yahtzee game.
This version of Yahtzee is initialized with a list of goals.
"""
"""A command-line Yahtzee game."""
def __init__(self, goals):
"""Initialize the game with score 0, goals, and five dice."""
self.score = 0
self.goals = goals
self.dice = [Die() for num in range(5)]
def play(self):
"""Run the main game loop until all goals are used, then print final score."""
print("Welcome to Yachtzee!")
self.score = 0
for goal in self.goals:
@@ -19,6 +19,7 @@ class Yahtzee:
print(f"Your final score was {self.score}")
def play_round(self):
"""Play one round: roll dice, choose a goal, and add its score."""
print("=" * 80)
self.rolls_left = 3
for die in self.dice:
@@ -29,10 +30,12 @@ class Yahtzee:
self.score += goal.score(self.dice)
def show_status(self):
"""Display current score, rolls left, and dice faces."""
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):
"""Prompt player to choose a goal or reroll, return the chosen goal."""
options = []
unused_goals = self.get_unused_goals()
for goal in unused_goals:
@@ -49,6 +52,7 @@ class Yahtzee:
return unused_goals[choice]
def get_choice(self, options):
"""Prompt user to select from options, validate input, and return index."""
print("What would you like to do?")
for i, option in enumerate(options):
print(f"{i}. {option}")
@@ -59,6 +63,7 @@ class Yahtzee:
return int(choice)
def option_choice_is_valid(self, choice, options):
"""Return True if choice is a valid digit index within options range."""
if not choice.isdigit():
return False
if int(choice) < 0:
@@ -68,9 +73,11 @@ class Yahtzee:
return True
def count_unused_goals(self):
"""Return the number of unused goals."""
return len(self.get_unused_goals())
def get_unused_goals(self):
"""Return a list of all unused Goal objects."""
unused_goals = []
for goal in self.goals:
if not goal.used:
@@ -78,6 +85,7 @@ class Yahtzee:
return unused_goals
def reroll(self):
"""Decrement rolls_left, get player's choices, and reroll selected dice."""
self.rolls_left -= 1
choices = self.get_reroll_choices()
dice_to_reroll = self.get_dice_to_reroll(choices)
@@ -85,6 +93,7 @@ class Yahtzee:
die.roll()
def get_dice_to_reroll(self, choice_ints):
"""Return Die objects whose faces match the provided choice_ints."""
dice_to_reroll = []
for die in self.dice:
if die.face in choice_ints:
@@ -93,6 +102,7 @@ class Yahtzee:
return dice_to_reroll
def get_reroll_choices(self):
"""Prompt user for dice to reroll, validate, and return list of face values."""
print("Which dice do you want to re-roll?")
choices = input("> ")
while not self.reroll_choices_are_valid(choices):
@@ -102,6 +112,7 @@ class Yahtzee:
return choice_ints
def reroll_choices_are_valid(self, choices_str):
"""Return True if choices_str contains only digits matching current dice faces."""
if not choices_str.isdigit():
return False
choice_ints = [int(digit) for digit in choices_str]
@@ -109,3 +120,4 @@ class Yahtzee:
if die.face in choice_ints:
choice_ints.remove(die.face)
return len(choice_ints) == 0