From 97e1448a66877f4d7f9bfc7fe0471fe0dd3aba8a Mon Sep 17 00:00:00 2001 From: root Date: Wed, 7 Feb 2024 17:37:57 -0500 Subject: [PATCH] Completed checkpoint 2 by adding the docstrings. Instead of trying to solve a problem, I was walking myself through an already given solution to the problem. However, this is by no means easy as you need to be able to figure out how the different functions are connected. Also, the solution used isn't necessarily how I would do it so I cannot make any assumptions. I would use docstrings if I planned for someone else to use and try to understand my code or if it was a complicated project. If not, I would probably stick to a few comments here and there. --- __pycache__/yahtzee.cpython-310.pyc | Bin 0 -> 4514 bytes __pycache__/yahtzee_goals.cpython-310.pyc | Bin 0 -> 1391 bytes yahtzee.py | 71 ++++++++++++++++++---- 3 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 __pycache__/yahtzee.cpython-310.pyc create mode 100644 __pycache__/yahtzee_goals.cpython-310.pyc diff --git a/__pycache__/yahtzee.cpython-310.pyc b/__pycache__/yahtzee.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c330e696e43be6e0f3b3522644f1164246ac67ad GIT binary patch literal 4514 zcmb_f&u<&Y6`q-0E>{%w!?JA4a@?*{rwLn!N}IY)T}N?Y!%Z$kU>P+m)Yz^$D`_Q) zE6pq|Xh8u1JkdFF4`d92VC!cf6A?f#KNQ$Ibw1+M+!?&}uGxNUp-uK?7 zGcy&2D=7Ud{`>}G-_p(S<>2Nan*Iqo$s}*FUB1pa`y-P=I&YYCj>Wo=?i#C=-eL|r z;f^-6&2*;b;1*Rj$`+78lNh z?IcU0R+7guI7qUsAPQQE&S=(V8nv`tphh3<~FJ8U;UNVFrXLEZL%(VSvukwJ7#|q`(llqF-5a#q7e0u3a8!KRVCb; z?)$qdDowMM-DoFiZ-#XIu$jtuW%r=@tCbEk`*@`ly$WU0j8_gVD)qg?qVj-L1ZI1y zbF?#Uu)y;7`iR!UkYEYJ`h!;51PH4T`ft3Y?Q3pP(#=%GMaiI4lvLE-j0=~x!dYD% zw>DI@e^7g5AGd|yUz;9k51q$7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644 --- a/yahtzee.py +++ b/yahtzee.py @@ -4,17 +4,29 @@ class Yachtzee: """A command-line Yahtzee game. This version of Yahtzee is initialized with a list of goals. """ + """Sets up a new game. + Sets the users score to zero, sets the goals for this game + to the inputted goals and creates the five dice that will + be used in the game (5 ints in a list). + """ def __init__(self, goals): self.score = 0 self.goals = goals self.dice = [Die() for num in range(5)] - + """Controls the entire game. + Welcomes the user to the game with a print statement, then + keeps starting additional rounds until all of the goals have + been used. Then, prints out the users final score.""" def play(self): print("Welcome to Yachtzee!") while self.count_unused_goals() > 0: self.play_round() print(f"Your final score was {self.score}") - + """Controls each round. + Prints characters to seperate the lines of text, then will roll + the dice and show the user the outcome. Will let the user choose + their goal. Once the user is done rolling, marks the goal as used + and adds to their score the necessary amount""" def play_round(self): print("=" * 80) self.rolls_left = 3 @@ -24,11 +36,19 @@ class Yachtzee: goal = self.choose_goal() goal.used = True self.score += goal.score(self.dice) - + """Shows the user their status in round. + Prints a statement telling them their score, the rolls they have + left in that round and the current dice values""" 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}.") - + """Allows the user to chose what they want to do next. + Creates list of possible actions by appending unsued goals and the + Re-roll option if they have rolls left to to use. Then, gets the + users choice. If they chose to re-roll, the dice will reroll and the + dice will be shown and the process restarts until the user choses a non + Re-roll option. Then that option is removed from the list of options and + the function ends.""" def choose_goal(self): options = [] unused_goals = self.get_unused_goals() @@ -44,7 +64,12 @@ class Yachtzee: return self.choose_goal() else: return unused_goals[choice] - + """Obtains the users decision of what to do next. + Prints a statement asking the user what they would like to do and prints + every individual optioned all numbered. If the choice is valid, the choice + is outputted as an integer. If the choice is not valid, the user is told + this by a print statement and the user is asked to make a choice again + """ def get_choice(self, options): print("What would you like to do?") for i, option in enumerate(options): @@ -54,7 +79,10 @@ class Yachtzee: print("Sorry, that's not a valid choice.") choice = input("> ") return int(choice) - + """Ensures that the users choice is valid. + Ensures that the inputted value is a number, is not less than zero and + is not greater than the total number of options. If all of these tests + pass, True will be returned. Otherwise, False will be returned.""" def option_choice_is_valid(self, choice, options): if not choice.isdigit(): return False @@ -63,24 +91,33 @@ class Yachtzee: if int(choice) >= len(options): return False return True - + """Determines how many goals have not been used yet. + Returns the length of the list of the unused goals.""" def count_unused_goals(self): return len(self.get_unused_goals()) - + """Makes a list of all unused goals. + Creates an empty list then appends any goal that has not + been mark as used.""" def get_unused_goals(self): unused_goals = [] for goal in self.goals: if not goal.used: unused_goals.append(goal) return unused_goals - + """Manages the reroll process. + First it reduces the number of rolls left by one. Then, it obtains + any choices leftover, checks what dice the user wants to reroll, + then it rerolls them.""" def reroll(self): self.rolls_left -= 1 choices = self.get_reroll_choices() dice_to_reroll = self.get_dice_to_reroll(choices) for die in dice_to_reroll: die.roll() - + """Obtains the dice to be rerolled. + Creates an empty list, then checks each dice, seeing if the user + wants to reroll it. If so, it appends it to the aformentioned list. Then + it returns the dice to be rerolled.""" def get_dice_to_reroll(self, choice_ints): dice_to_reroll = [] for die in self.dice: @@ -88,7 +125,10 @@ class Yachtzee: choice_ints.remove(die.face) dice_to_reroll.append(die) return dice_to_reroll - + """Figures out which dice the user wants to reroll. + Prints a statement asking the user which dice they want to reroll. If the + requested dice are valid choices, then a list of the chosen dice as integers + is returned.""" def get_reroll_choices(self): print("Which dice do you want to re-roll?") choices = input("> ") @@ -97,7 +137,14 @@ class Yachtzee: choices = input("> ") choice_ints = [int(digit) for digit in choices] return choice_ints - + """Ensures the dice the user requested to reroll are valid. + Checks that the requested dice all are numbers. If so, a list of them as integers + is made. This list is iterated through ensuring that each requested dice is there. + It accomplishes this by checking the dice we have one by one and removing them + from the choice list created before if they are in the choice list. We then return + false if in the end, the choice list is not empty. Meaning, if dice + we dont have are requested to be rerolled, such as too many dice (ex. 6) more of a + face than we have, False will be returned. Otherwise, True will be returned.""" def reroll_choices_are_valid(self, choices_str): if not choices_str.isdigit(): return False