generated from mwc/lab_dice
docstring is like a manual or descrption, I find it can be very helpful.
Espeically, if I work with someone else, i might not be able to understand his or her code, but with docstring, I would have a better understanding of his or her intention. even if I work alone, for the beginner level of coder, it would be helpful to write down the docstring to organize the plans or orders.
This commit is contained in:
parent
b7f1ec19bf
commit
0e0ffd5cb2
36
yahtzee.py
36
yahtzee.py
|
@ -10,12 +10,21 @@ class Yachtzee:
|
|||
self.dice = [Die() for num in range(5)]
|
||||
|
||||
def play(self):
|
||||
"""While there is still avaiable goals, keep playing the game.
|
||||
When the game is over, print the final score"""
|
||||
print("Welcome to Yachtzee!")
|
||||
while self.count_unused_goals() > 0:
|
||||
self.play_round()
|
||||
print(f"Your final score was {self.score}")
|
||||
|
||||
def play_round(self):
|
||||
"""Show the image of an equal sign 80 times in a row.
|
||||
Define the remaining rolls is three.
|
||||
Roll all five dice.
|
||||
Show the result of the row.
|
||||
Ask the user to choose the goal.
|
||||
Mark the goal as used.
|
||||
Add score from this round to the total score"""
|
||||
print("=" * 80)
|
||||
self.rolls_left = 3
|
||||
for die in self.dice:
|
||||
|
@ -26,10 +35,18 @@ class Yachtzee:
|
|||
self.score += goal.score(self.dice)
|
||||
|
||||
def show_status(self):
|
||||
"""show the list numbers on the dice with comma and a space.
|
||||
Show the score and the remaining rolls."""
|
||||
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):
|
||||
"""Build the selection menu for the user, and ask the user to select
|
||||
one option.
|
||||
If there is no avaiable roll, then the option for 'reroll' will not be shown.
|
||||
If the user's choice is 're-roll', then the user roll the dice again, and
|
||||
this fuction gets called again.
|
||||
If the user choose the goal, it will return the selected goal."""
|
||||
options = []
|
||||
unused_goals = self.get_unused_goals()
|
||||
for goal in unused_goals:
|
||||
|
@ -46,6 +63,9 @@ class Yachtzee:
|
|||
return unused_goals[choice]
|
||||
|
||||
def get_choice(self, options):
|
||||
"""Ask the user the input. And print the avaiable choices.
|
||||
Check if the choice is valid, if not, ask again. if valid, then return
|
||||
the choice"""
|
||||
print("What would you like to do?")
|
||||
for i, option in enumerate(options):
|
||||
print(f"{i}. {option}")
|
||||
|
@ -56,6 +76,9 @@ class Yachtzee:
|
|||
return int(choice)
|
||||
|
||||
def option_choice_is_valid(self, choice, options):
|
||||
"""if the users provide non-digit or a number that is less than zero
|
||||
or a number that are greater than the maxium option number, then return
|
||||
false. If valid, return true."""
|
||||
if not choice.isdigit():
|
||||
return False
|
||||
if int(choice) < 0:
|
||||
|
@ -65,9 +88,12 @@ class Yachtzee:
|
|||
return True
|
||||
|
||||
def count_unused_goals(self):
|
||||
"""get list of unused goals, and returen its length. """
|
||||
return len(self.get_unused_goals())
|
||||
|
||||
def get_unused_goals(self):
|
||||
"""Iterate through all possible goals, and return a list containing
|
||||
only the unused goals."""
|
||||
unused_goals = []
|
||||
for goal in self.goals:
|
||||
if not goal.used:
|
||||
|
@ -75,6 +101,9 @@ class Yachtzee:
|
|||
return unused_goals
|
||||
|
||||
def reroll(self):
|
||||
"""Subtract 1 from the avaiable rolls, then ask the user which dice he
|
||||
or she wants to roll
|
||||
And then, roll them"""
|
||||
self.rolls_left -= 1
|
||||
choices = self.get_reroll_choices()
|
||||
dice_to_reroll = self.get_dice_to_reroll(choices)
|
||||
|
@ -82,6 +111,8 @@ class Yachtzee:
|
|||
die.roll()
|
||||
|
||||
def get_dice_to_reroll(self, choice_ints):
|
||||
""" Loop through all dies, if the die face is in the list of the number
|
||||
the user wants to reroll, then add it to the reroll """
|
||||
dice_to_reroll = []
|
||||
for die in self.dice:
|
||||
if die.face in choice_ints:
|
||||
|
@ -90,6 +121,8 @@ class Yachtzee:
|
|||
return dice_to_reroll
|
||||
|
||||
def get_reroll_choices(self):
|
||||
"""Asks the user to input the numbers to reroll, check the selection is
|
||||
valid, and if it is, return it. """
|
||||
print("Which dice do you want to re-roll?")
|
||||
choices = input("> ")
|
||||
while not self.reroll_choices_are_valid(choices):
|
||||
|
@ -99,6 +132,9 @@ class Yachtzee:
|
|||
return choice_ints
|
||||
|
||||
def reroll_choices_are_valid(self, choices_str):
|
||||
"""Check the user input is valid choice for the die number, for example,
|
||||
if the input is not digit, return false. Then, check the input number
|
||||
appears on the die face."""
|
||||
if not choices_str.isdigit():
|
||||
return False
|
||||
choice_ints = [int(digit) for digit in choices_str]
|
||||
|
|
Loading…
Reference in New Issue