I added the comments to each line of yahtzee.py

Checkpoint 2:
My thinking was different in that I was trying to see what the code was doing instead of actually figuring out the correct syntax.  It was definitely easier to think this way as I already knew the code worked.  I will add docstrings in my future code for portions of the code that is confusing.
This commit is contained in:
jwberent
2025-11-04 22:19:25 -05:00
parent a72b6338f2
commit fdcd5d5ae9

View File

@@ -8,6 +8,9 @@ class Yahtzee:
self.score = 0
self.goals = goals
self.dice = [Die() for num in range(5)]
"""
Just sets up the class. __init__ is always the first thing needed.
"""
def play(self):
print("Welcome to Yachtzee!")
@@ -17,6 +20,9 @@ class Yahtzee:
while self.count_unused_goals() > 0:
self.play_round()
print(f"Your final score was {self.score}")
"""
Runs the game. It tells you the final score of the game.
"""
def play_round(self):
print("=" * 80)
@@ -27,10 +33,16 @@ class Yahtzee:
goal = self.choose_goal()
goal.used = True
self.score += goal.score(self.dice)
"""
Uses the rolls left, rolls the dice, and shows the score. Used to further the game.
"""
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}.")
"""
Shows the score and how many rolls of the dice you have left. Uses the score and rolls left.
"""
def choose_goal(self):
options = []
@@ -47,6 +59,9 @@ class Yahtzee:
return self.choose_goal()
else:
return unused_goals[choice]
"""
Allows the player to choose how many dice to reroll. If there are more than 0 dice left to reroll, it gives the player the option to.
"""
def get_choice(self, options):
print("What would you like to do?")
@@ -57,6 +72,9 @@ class Yahtzee:
print("Sorry, that's not a valid choice.")
choice = input("> ")
return int(choice)
"""
It tells if you don't choose a possible choice. It is there so it is not just a blank screen.
"""
def option_choice_is_valid(self, choice, options):
if not choice.isdigit():
@@ -66,9 +84,15 @@ class Yahtzee:
if int(choice) >= len(options):
return False
return True
"""
It allows the program to work as it makes sure that the choice is valid. It will make sure the choice is a number and is inside the number in the list.
"""
def count_unused_goals(self):
return len(self.get_unused_goals())
"""
It counts the number of unused goals to further the game. It takes the length of the unused goals.
"""
def get_unused_goals(self):
unused_goals = []
@@ -76,6 +100,9 @@ class Yahtzee:
if not goal.used:
unused_goals.append(goal)
return unused_goals
"""
Finds the rest of the unused goals not chosen so far. It will make a list of unused goals and add the unused ones to it.
"""
def reroll(self):
self.rolls_left -= 1
@@ -83,6 +110,9 @@ class Yahtzee:
dice_to_reroll = self.get_dice_to_reroll(choices)
for die in dice_to_reroll:
die.roll()
"""
Allows the player to reroll a dice if they choose to. It will remove 1 from rolls left and find the choices the player has and then will reroll.
"""
def get_dice_to_reroll(self, choice_ints):
dice_to_reroll = []
@@ -91,6 +121,9 @@ class Yahtzee:
choice_ints.remove(die.face)
dice_to_reroll.append(die)
return dice_to_reroll
"""
It will remove dies when necessary. It will use the empty list then remove some dies then append what is left and then reroll from there.
"""
def get_reroll_choices(self):
print("Which dice do you want to re-roll?")
@@ -100,6 +133,9 @@ class Yahtzee:
choices = input("> ")
choice_ints = [int(digit) for digit in choices]
return choice_ints
"""
Asks the player which dice they want to reroll and if they do not give a possible choice, it will ask again. It uses the valid reroll choices and returns a number called choice ints.
"""
def reroll_choices_are_valid(self, choices_str):
if not choices_str.isdigit():
@@ -109,3 +145,6 @@ class Yahtzee:
if die.face in choice_ints:
choice_ints.remove(die.face)
return len(choice_ints) == 0
"""
Is used when the reroll choice is valid and will go until there are no more choices left. It will remove the unneeded dice faces and stop when there are no choices left.
"""