generated from mwc/lab_dice
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.
This commit is contained in:
parent
9e7cde5211
commit
97e1448a66
Binary file not shown.
Binary file not shown.
71
yahtzee.py
71
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
|
||||
|
|
Loading…
Reference in New Issue