generated from mwc/lab_dice
I didn't think that differently when writing docstrings versus code. When writing code.
I don't think I will include docstrings in my own projects, I don't find them to be very useful.
This commit is contained in:
24
yahtzee.py
24
yahtzee.py
@@ -5,12 +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):
|
||||||
|
"""Initialize a Yahtzee game.
|
||||||
|
Sets the starting score to zero, stores the list of scoring goals,
|
||||||
|
and creates five Die instances to represent the player's dice.
|
||||||
|
"""
|
||||||
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):
|
||||||
print("Welcome to Yachtzee!")
|
"""Play an entire game of Yahtzee.After all
|
||||||
|
rounds are played, prints the final score."""
|
||||||
|
print("Welcome to Yahtzee!")
|
||||||
self.score = 0
|
self.score = 0
|
||||||
for goal in self.goals:
|
for goal in self.goals:
|
||||||
goal.used = False
|
goal.used = False
|
||||||
@@ -19,6 +25,12 @@ 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):
|
||||||
|
"""Play a single round of Yahtzee.
|
||||||
|
Starts the round by resetting the number of rolls left to three and
|
||||||
|
rolling all five dice once. Shows the current status, lets the player
|
||||||
|
choose a goal or re-roll, marks the chosen goal used, and adds the
|
||||||
|
resulting score for that goal based on the current dice.
|
||||||
|
"""
|
||||||
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,12 @@ class Yahtzee:
|
|||||||
self.score += goal.score(self.dice)
|
self.score += goal.score(self.dice)
|
||||||
|
|
||||||
def show_status(self):
|
def show_status(self):
|
||||||
|
"""Display the player's current game status, Builds a string with the current score"""
|
||||||
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):
|
||||||
|
"""Let the player choose a goal or re-roll during a round."""
|
||||||
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 +63,7 @@ class Yahtzee:
|
|||||||
return unused_goals[choice]
|
return unused_goals[choice]
|
||||||
|
|
||||||
def get_choice(self, options):
|
def get_choice(self, options):
|
||||||
|
"""Prompt the player to pick one of the provided options."""
|
||||||
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 +74,7 @@ class Yahtzee:
|
|||||||
return int(choice)
|
return int(choice)
|
||||||
|
|
||||||
def option_choice_is_valid(self, choice, options):
|
def option_choice_is_valid(self, choice, options):
|
||||||
|
"""Validate that the player's textual choice corresponds to an index."""
|
||||||
if not choice.isdigit():
|
if not choice.isdigit():
|
||||||
return False
|
return False
|
||||||
if int(choice) < 0:
|
if int(choice) < 0:
|
||||||
@@ -68,9 +84,11 @@ class Yahtzee:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def count_unused_goals(self):
|
def count_unused_goals(self):
|
||||||
|
"""Return the number of goals that haven't been used yet."""
|
||||||
return len(self.get_unused_goals())
|
return len(self.get_unused_goals())
|
||||||
|
|
||||||
def get_unused_goals(self):
|
def get_unused_goals(self):
|
||||||
|
"""Return a list of goal objects that are not yet 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 +96,7 @@ class Yahtzee:
|
|||||||
return unused_goals
|
return unused_goals
|
||||||
|
|
||||||
def reroll(self):
|
def reroll(self):
|
||||||
|
"""Perform a re-roll of selected dice."""
|
||||||
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 +104,7 @@ class Yahtzee:
|
|||||||
die.roll()
|
die.roll()
|
||||||
|
|
||||||
def get_dice_to_reroll(self, choice_ints):
|
def get_dice_to_reroll(self, choice_ints):
|
||||||
|
"""Map a list of face-value integers to Die objects to re-roll."""
|
||||||
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 +113,7 @@ class Yahtzee:
|
|||||||
return dice_to_reroll
|
return dice_to_reroll
|
||||||
|
|
||||||
def get_reroll_choices(self):
|
def get_reroll_choices(self):
|
||||||
|
"""Ask the player which dice to re-roll and return a list of ints."""
|
||||||
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 +123,7 @@ class Yahtzee:
|
|||||||
return choice_ints
|
return choice_ints
|
||||||
|
|
||||||
def reroll_choices_are_valid(self, choices_str):
|
def reroll_choices_are_valid(self, choices_str):
|
||||||
|
"""Validate the player's re-roll input string."""
|
||||||
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