From 637a5e5191038aaf567b2ad1270867554d97e3f9 Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 12 Feb 2024 12:09:14 -0500 Subject: [PATCH] Completed checkpoint 2. Writing docstrings was different from writing code. One reason is that I was trying to understand code I hadn't written, so it may not have been written or presented in the way I would have conceived it. Similarly, I didn't necessarily know how each method related to the other, so instead of breaking down a problem into smaller ones, I was trying to understand how all of the pieces came together to create a version of Yahtzee. Another reason is that I don't write a lot of comments or docstrings in my own practice, so it's something I need to build a better habit of. I wasn't always sure if what I was writing captured what I needed to in a docstring. I do think in the future, I should write more docstrings, if not for myself than at least to model it for my students. I think partially this is because I usually write my projects in short bursts so the meaning is clear to me. I can see how it might have value if it's something others might look at or something I have to revisit later on after having not looked at it for a while. --- dice.py | 1 - yahtzee.py | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 6 deletions(-) delete mode 100644 dice.py diff --git a/dice.py b/dice.py deleted file mode 100644 index 8d75686..0000000 --- a/dice.py +++ /dev/null @@ -1 +0,0 @@ -dskkjkldsfj \ No newline at end of file diff --git a/yahtzee.py b/yahtzee.py index e5fc4fd..2380e09 100644 --- a/yahtzee.py +++ b/yahtzee.py @@ -6,7 +6,7 @@ class Yachtzee: """ def __init__(self, goals): """ This method specifies the default properties of a Yachtzee object. - The object has a score set to 0, a list of goals that is specified when the object is created(?), and a list of five dice. + The object has a score set to 0, a list of goals that is specified when the object is created, and a list of five dice. """ self.score = 0 self.goals = goals @@ -14,7 +14,7 @@ class Yachtzee: def play(self): """ This method allows for a game of Yachtzee to be played. - While none of the goals are met, the game continues. Once a goal is met, the score is printed. + While at least one goal remains unused, the game continues. Once all goals are used, the score is printed. """ print("Welcome to Yachtzee!") while self.count_unused_goals() > 0: @@ -23,9 +23,8 @@ class Yachtzee: def play_round(self): """ This method allows for a round to be played. - At the start of the round, a divider is printed consisting of 80 = signs. A property called rolls_left is set equal to 3. - Then, all dice are rolled. The method then checks the state (status and goals fulfilled) of the game and updates the score - accordingly. + The player starts with 3 rolls of the dice. Then, all dice are rolled. The method then checks the state (status and goals fulfilled) + of the game and updates the score accordingly based on the goal selected. The method also records that the goal has been used. """ print("=" * 80) self.rolls_left = 3 @@ -37,10 +36,18 @@ class Yachtzee: self.score += goal.score(self.dice) def show_status(self): + """ This method prints the status of the game. + This prints the score of the player, the number of rolls left, what was rolled. + """ 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): + """ This method allows one to choose which goal to score in a given round. + This method lists all the goals one can score. If any rolls are left, the player is also given the option to re-roll. The player + selects the option they want. If they choose to re-roll, the re-roll method is called and then players can see what their dice + are and choose a goal to be scored (or they can choose to re-roll if any rolls are left). + """ options = [] unused_goals = self.get_unused_goals() for goal in unused_goals: @@ -57,6 +64,9 @@ class Yachtzee: return unused_goals[choice] def get_choice(self, options): + """ This method returns a list of possible goals to be scored. + This method creates a numbered list of goals. The player's choice is solicited and returned as an integer. If the choice is not + valid, the player is prompted to select again a goal again.""" print("What would you like to do?") for i, option in enumerate(options): print(f"{i}. {option}") @@ -67,6 +77,10 @@ class Yachtzee: return int(choice) def option_choice_is_valid(self, choice, options): + """ This method checks if a player's choice is valid. + This method checks that the choice is a non-negative number strictly less than the length of the length. This is because the first + option is numbered 0. + """ if not choice.isdigit(): return False if int(choice) < 0: @@ -76,9 +90,15 @@ class Yachtzee: return True def count_unused_goals(self): + """ This method reports the number of unscored goals. + The method generates the list of unused goals and then calculates and reports the list's length. + """ return len(self.get_unused_goals()) def get_unused_goals(self): + """ This method reports the unused goals. + This method checks each goal to see if it has been used. If it has not, it is added to the list of unused goals, which is reported. + """ unused_goals = [] for goal in self.goals: if not goal.used: @@ -86,6 +106,10 @@ class Yachtzee: return unused_goals def reroll(self): + """ This method rerolls selected dice. + The method first reduces the number of re-rolls available left by one. Then the method captures the choice of dice to re-roll and + re-rolls only those dice. + """ self.rolls_left -= 1 choices = self.get_reroll_choices() dice_to_reroll = self.get_dice_to_reroll(choices) @@ -93,6 +117,10 @@ class Yachtzee: die.roll() def get_dice_to_reroll(self, choice_ints): + """ This method creates a list of the dice to be re-rolled. + This method goes through each dice. If it is one of the dice that was chosen to be re-rolled, then it is added to the list of + dice to be re-rolled. Said list is returned. + """ dice_to_reroll = [] for die in self.dice: if die.face in choice_ints: @@ -101,6 +129,10 @@ class Yachtzee: return dice_to_reroll def get_reroll_choices(self): + """ This method captures which dice the player would like to re-roll. + First, the user is prompted to specify the faces of the dice they would like to re-roll. The method checks that these choices are valid. + Then, a list is created where each choice the player made is recorded as an integer, and the list is returned. + """ print("Which dice do you want to re-roll?") choices = input("> ") while not self.reroll_choices_are_valid(choices): @@ -110,6 +142,13 @@ class Yachtzee: return choice_ints def reroll_choices_are_valid(self, choices_str): + """ This method checks that the choice of dice to re-roll is valid. + The method checks that the choices are in a string of only numbers. Then, a list is formed converting all the characters in the + string to integers. This list of integers is compared to what was rolled on the dice by checking to see that each die + that should be re-rolled is included in the list of desired re-rolls and that nothing else is in the list. If a die is in the list + of dice to be re-rolled, that die is removed from the list. Once all dice have been gone through, there should be nothing remaining + in the list of dice to be re-rolled. + """ if not choices_str.isdigit(): return False choice_ints = [int(digit) for digit in choices_str]