generated from mwc/lab_dice
Edited the docstrings on the Yathzee.
Checkpoint 2: I think writing the docstrings GREATLY helped me understand the code. I would love to get in the habit of utilizing this for any new code that I might encounter, if time permitting. It helped me analyze the step=by=step process of the running code. It was different in a sense that I thought it was more intentional than writing the code. When I write code I start with a very rough draft of it then fixing it as I go. I think if I start thinking of code in the way I wrote these docstrings, my understanding and skills of coding would improve.
This commit is contained in:
48
yahtzee.py
48
yahtzee.py
@@ -5,11 +5,18 @@ class Yahtzee:
|
|||||||
This version of Yahtzee is initialized with a list of goals.
|
This version of Yahtzee is initialized with a list of goals.
|
||||||
"""
|
"""
|
||||||
def __init__(self, goals):
|
def __init__(self, goals):
|
||||||
|
"""Determines the starting conditions.
|
||||||
|
Sets the score to 0 to begin with, defines the goals and prepares 5 dices for the game.
|
||||||
|
"""
|
||||||
self.score = 0
|
self.score = 0
|
||||||
self.goals = goals
|
self.goals = goals
|
||||||
self.dice = [Die() for num in range(5)]
|
self.dice = [Die() for num in range(5)]
|
||||||
|
|
||||||
def play(self):
|
def play(self):
|
||||||
|
"""Play an entire game
|
||||||
|
Starts by greeting the user, then plays a round until all the goals have been used.
|
||||||
|
When the game is over, tells the player their final score.
|
||||||
|
"""
|
||||||
print("Welcome to Yachtzee!")
|
print("Welcome to Yachtzee!")
|
||||||
self.score = 0
|
self.score = 0
|
||||||
for goal in self.goals:
|
for goal in self.goals:
|
||||||
@@ -19,6 +26,11 @@ class Yahtzee:
|
|||||||
print(f"Your final score was {self.score}")
|
print(f"Your final score was {self.score}")
|
||||||
|
|
||||||
def play_round(self):
|
def play_round(self):
|
||||||
|
"""Sets the screen and rolls 5 dice.
|
||||||
|
After setting up the screen with 80 = signs, 5 dice are rolled and the results are shown
|
||||||
|
to the player. The player then is left to choose a goal from the list provided and their
|
||||||
|
score is updated.
|
||||||
|
"""
|
||||||
print("=" * 80)
|
print("=" * 80)
|
||||||
self.rolls_left = 3
|
self.rolls_left = 3
|
||||||
for die in self.dice:
|
for die in self.dice:
|
||||||
@@ -29,10 +41,17 @@ class Yahtzee:
|
|||||||
self.score += goal.score(self.dice)
|
self.score += goal.score(self.dice)
|
||||||
|
|
||||||
def show_status(self):
|
def show_status(self):
|
||||||
|
"""Print the status line
|
||||||
|
Provides the player important info such as score, remaining number of rolls and
|
||||||
|
their dice upon rolling.
|
||||||
|
"""
|
||||||
dice = ', '.join([str(die) for die in self.dice])
|
dice = ', '.join([str(die) for die in self.dice])
|
||||||
print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.")
|
print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.")
|
||||||
|
|
||||||
def choose_goal(self):
|
def choose_goal(self):
|
||||||
|
"""Allows the user to choose the goal option.
|
||||||
|
Appends the unused goal options. Depending on the player input, re-rolls the dice.
|
||||||
|
"""
|
||||||
options = []
|
options = []
|
||||||
unused_goals = self.get_unused_goals()
|
unused_goals = self.get_unused_goals()
|
||||||
for goal in unused_goals:
|
for goal in unused_goals:
|
||||||
@@ -49,6 +68,9 @@ class Yahtzee:
|
|||||||
return unused_goals[choice]
|
return unused_goals[choice]
|
||||||
|
|
||||||
def get_choice(self, options):
|
def get_choice(self, options):
|
||||||
|
"""Provides the player set of options.
|
||||||
|
Enumerates the options as an integer list and present it to the player.
|
||||||
|
"""
|
||||||
print("What would you like to do?")
|
print("What would you like to do?")
|
||||||
for i, option in enumerate(options):
|
for i, option in enumerate(options):
|
||||||
print(f"{i}. {option}")
|
print(f"{i}. {option}")
|
||||||
@@ -59,6 +81,10 @@ class Yahtzee:
|
|||||||
return int(choice)
|
return int(choice)
|
||||||
|
|
||||||
def option_choice_is_valid(self, choice, options):
|
def option_choice_is_valid(self, choice, options):
|
||||||
|
"""Determines the validity of player choice.
|
||||||
|
Based on the integer list of choices, determines the validity of input such as
|
||||||
|
non-positive, non-integer and longer than allowed inputs.
|
||||||
|
"""
|
||||||
if not choice.isdigit():
|
if not choice.isdigit():
|
||||||
return False
|
return False
|
||||||
if int(choice) < 0:
|
if int(choice) < 0:
|
||||||
@@ -68,9 +94,14 @@ class Yahtzee:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def count_unused_goals(self):
|
def count_unused_goals(self):
|
||||||
|
"""Counts the number of unused goals.
|
||||||
|
"""
|
||||||
return len(self.get_unused_goals())
|
return len(self.get_unused_goals())
|
||||||
|
|
||||||
def get_unused_goals(self):
|
def get_unused_goals(self):
|
||||||
|
"""Determines the unused goals.
|
||||||
|
If a goal isn't used, appends that goal to the list of remaining goals to be used.
|
||||||
|
"""
|
||||||
unused_goals = []
|
unused_goals = []
|
||||||
for goal in self.goals:
|
for goal in self.goals:
|
||||||
if not goal.used:
|
if not goal.used:
|
||||||
@@ -78,6 +109,10 @@ class Yahtzee:
|
|||||||
return unused_goals
|
return unused_goals
|
||||||
|
|
||||||
def reroll(self):
|
def reroll(self):
|
||||||
|
"""Rerolls the dice
|
||||||
|
Decreases the number of rerolls left by one and for the selected dice,
|
||||||
|
performs the reroll action.
|
||||||
|
"""
|
||||||
self.rolls_left -= 1
|
self.rolls_left -= 1
|
||||||
choices = self.get_reroll_choices()
|
choices = self.get_reroll_choices()
|
||||||
dice_to_reroll = self.get_dice_to_reroll(choices)
|
dice_to_reroll = self.get_dice_to_reroll(choices)
|
||||||
@@ -85,6 +120,10 @@ class Yahtzee:
|
|||||||
die.roll()
|
die.roll()
|
||||||
|
|
||||||
def get_dice_to_reroll(self, choice_ints):
|
def get_dice_to_reroll(self, choice_ints):
|
||||||
|
"""Allows the user to choose which dice to reroll.
|
||||||
|
Creates an empty array for the player input of dice to reroll. Removes the chosen
|
||||||
|
dice from the faces and sends the chosen list of dice for the reroll action.
|
||||||
|
"""
|
||||||
dice_to_reroll = []
|
dice_to_reroll = []
|
||||||
for die in self.dice:
|
for die in self.dice:
|
||||||
if die.face in choice_ints:
|
if die.face in choice_ints:
|
||||||
@@ -93,6 +132,11 @@ class Yahtzee:
|
|||||||
return dice_to_reroll
|
return dice_to_reroll
|
||||||
|
|
||||||
def get_reroll_choices(self):
|
def get_reroll_choices(self):
|
||||||
|
"""Prompts the user to choose their dice to reroll.
|
||||||
|
Asks the player which dice they want to reroll. If their input isn't valid,
|
||||||
|
asks the player to enter numbers of the dice to reroll. The digit input is stored in
|
||||||
|
the choice integers list.
|
||||||
|
"""
|
||||||
print("Which dice do you want to re-roll?")
|
print("Which dice do you want to re-roll?")
|
||||||
choices = input("> ")
|
choices = input("> ")
|
||||||
while not self.reroll_choices_are_valid(choices):
|
while not self.reroll_choices_are_valid(choices):
|
||||||
@@ -102,6 +146,10 @@ class Yahtzee:
|
|||||||
return choice_ints
|
return choice_ints
|
||||||
|
|
||||||
def reroll_choices_are_valid(self, choices_str):
|
def reroll_choices_are_valid(self, choices_str):
|
||||||
|
"""Removes the reroll dice choice from the faces.
|
||||||
|
Checks if the choices string element is a digit or not. If the digit is a dice face,
|
||||||
|
removes the face as it is to be rerolled.
|
||||||
|
"""
|
||||||
if not choices_str.isdigit():
|
if not choices_str.isdigit():
|
||||||
return False
|
return False
|
||||||
choice_ints = [int(digit) for digit in choices_str]
|
choice_ints = [int(digit) for digit in choices_str]
|
||||||
|
|||||||
Reference in New Issue
Block a user