generated from mwc/lab_dice
What I did for checkpoint 2 was I added docstrings descibing what each function does in yahtzee.py
For checkpoint 2, I have always found it easier to read and describe code rather than actually writing out the code. When writing out the code I think to myself what the process is doing and how it is doing it. This normally takes the longest for me because it is easier to read what the code does rather than write code for the procedure. When writing docstring for my code I always pretend to run a simulation in my mind and picture what is happening which helps me read and understand the code. In future projects I will include writing docstrings because it helps me keep track of what certain codes do and why it does what it does.
This commit is contained in:
38
yahtzee.py
38
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):
|
||||||
|
"""Initialize a new game of Yahtzee.
|
||||||
|
Creates a score of zero, stores the list of goals, and sets up five 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):
|
||||||
|
"""Play an entire game.
|
||||||
|
Greets the player, resets the score and goals, and continues playing rounds
|
||||||
|
until all goals have been used. Prints the final score at the end.
|
||||||
|
"""
|
||||||
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,9 @@ 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.
|
||||||
|
Rolls all dice, lets the player choose a goal or re-roll, and updates the score.
|
||||||
|
"""
|
||||||
print("=" * 80)
|
print("=" * 80)
|
||||||
self.rolls_left = 3
|
self.rolls_left = 3
|
||||||
for die in self.dice:
|
for die in self.dice:
|
||||||
@@ -29,10 +39,17 @@ class Yahtzee:
|
|||||||
self.score += goal.score(self.dice)
|
self.score += goal.score(self.dice)
|
||||||
|
|
||||||
def show_status(self):
|
def show_status(self):
|
||||||
|
"""Display the current game status.
|
||||||
|
Shows the current score, number of rolls left, and the faces of all dice.
|
||||||
|
"""
|
||||||
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):
|
||||||
|
"""Prompt the player to choose a goal or re-roll.
|
||||||
|
Builds a list of unused goals, displays them, and processes the player's choice.
|
||||||
|
If the player chooses to re-roll, performs a re-roll and re-prompts for a goal.
|
||||||
|
"""
|
||||||
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 +66,9 @@ class Yahtzee:
|
|||||||
return unused_goals[choice]
|
return unused_goals[choice]
|
||||||
|
|
||||||
def get_choice(self, options):
|
def get_choice(self, options):
|
||||||
|
"""Display available options and get the player's choice.
|
||||||
|
Validates user input and returns the chosen option as an integer index.
|
||||||
|
"""
|
||||||
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 +79,9 @@ class Yahtzee:
|
|||||||
return int(choice)
|
return int(choice)
|
||||||
|
|
||||||
def option_choice_is_valid(self, choice, options):
|
def option_choice_is_valid(self, choice, options):
|
||||||
|
"""Check whether the player's input is a valid choice.
|
||||||
|
Ensures the input is a number and within the range of available options.
|
||||||
|
"""
|
||||||
if not choice.isdigit():
|
if not choice.isdigit():
|
||||||
return False
|
return False
|
||||||
if int(choice) < 0:
|
if int(choice) < 0:
|
||||||
@@ -68,9 +91,11 @@ class Yahtzee:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def count_unused_goals(self):
|
def count_unused_goals(self):
|
||||||
|
"""Return the number of unused goals left in the game."""
|
||||||
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 goals that have not yet been 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 +103,10 @@ class Yahtzee:
|
|||||||
return unused_goals
|
return unused_goals
|
||||||
|
|
||||||
def reroll(self):
|
def reroll(self):
|
||||||
|
"""Re-roll selected dice.
|
||||||
|
Decreases the number of rolls left, asks the player which dice to re-roll,
|
||||||
|
and rolls the chosen dice again.
|
||||||
|
"""
|
||||||
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 +114,9 @@ class Yahtzee:
|
|||||||
die.roll()
|
die.roll()
|
||||||
|
|
||||||
def get_dice_to_reroll(self, choice_ints):
|
def get_dice_to_reroll(self, choice_ints):
|
||||||
|
"""Identify which dice to re-roll based on the player's choices.
|
||||||
|
Matches chosen face values with current dice faces and returns a list of dice to roll again.
|
||||||
|
"""
|
||||||
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 +125,9 @@ 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.
|
||||||
|
Prompts for input, validates the choices, and returns them as a list of integers.
|
||||||
|
"""
|
||||||
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 +137,9 @@ 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 choices.
|
||||||
|
Checks that all characters are digits and correspond to actual dice values.
|
||||||
|
"""
|
||||||
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