generated from mwc/lab_dice
yahtzee.py
This commit is contained in:
51
yahtzee.py
51
yahtzee.py
@@ -3,6 +3,7 @@ from die import Die
|
||||
class Yahtzee:
|
||||
"""A command-line Yahtzee game.
|
||||
This version of Yahtzee is initialized with a list of goals.
|
||||
5 dice are initially cast.
|
||||
"""
|
||||
def __init__(self, goals):
|
||||
self.score = 0
|
||||
@@ -10,7 +11,12 @@ class Yahtzee:
|
||||
self.dice = [Die() for num in range(5)]
|
||||
|
||||
def play(self):
|
||||
print("Welcome to Yachtzee!")
|
||||
"""Play an entire game.
|
||||
Starts by greeting the user, then plays rounds until all the goals
|
||||
have been used. When the game is over, tells the player their final
|
||||
score.
|
||||
"""
|
||||
print("Welcome to Yahtzee!")
|
||||
self.score = 0
|
||||
for goal in self.goals:
|
||||
goal.used = False
|
||||
@@ -19,6 +25,10 @@ class Yahtzee:
|
||||
print(f"Your final score was {self.score}")
|
||||
|
||||
def play_round(self):
|
||||
"""Initializes the screen display.
|
||||
Sets initial rolls left to 3. Rolls the dice. Displays the status.
|
||||
Sets 2 variables. Increments the final score.
|
||||
"""
|
||||
print("=" * 80)
|
||||
self.rolls_left = 3
|
||||
for die in self.dice:
|
||||
@@ -29,10 +39,16 @@ class Yahtzee:
|
||||
self.score += goal.score(self.dice)
|
||||
|
||||
def show_status(self):
|
||||
"""Assigns a list comprehension to a variable.
|
||||
Displays the first line after the underscore:
|
||||
Score, rolls left, and the face value of the 5 dice.
|
||||
"""
|
||||
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):
|
||||
"""Code for the logic in the Re-roll option.
|
||||
"""
|
||||
options = []
|
||||
unused_goals = self.get_unused_goals()
|
||||
for goal in unused_goals:
|
||||
@@ -49,6 +65,9 @@ class Yahtzee:
|
||||
return unused_goals[choice]
|
||||
|
||||
def get_choice(self, options):
|
||||
"""Displays the menu choices after the status line.
|
||||
Inputs the player's choice after a prompt. Does error-checking.
|
||||
"""
|
||||
print("What would you like to do?")
|
||||
for i, option in enumerate(options):
|
||||
print(f"{i}. {option}")
|
||||
@@ -59,6 +78,10 @@ class Yahtzee:
|
||||
return int(choice)
|
||||
|
||||
def option_choice_is_valid(self, choice, options):
|
||||
"""Error-checking function.
|
||||
Checks if the player's choice is an integer, not less than zero,
|
||||
and not more than one digit.
|
||||
"""
|
||||
if not choice.isdigit():
|
||||
return False
|
||||
if int(choice) < 0:
|
||||
@@ -68,9 +91,14 @@ class Yahtzee:
|
||||
return True
|
||||
|
||||
def count_unused_goals(self):
|
||||
"""Returns the count of the list of unused_goals.
|
||||
"""
|
||||
return len(self.get_unused_goals())
|
||||
|
||||
def get_unused_goals(self):
|
||||
"""
|
||||
Conditional appending of the list of unused_goals.
|
||||
"""
|
||||
unused_goals = []
|
||||
for goal in self.goals:
|
||||
if not goal.used:
|
||||
@@ -78,6 +106,12 @@ class Yahtzee:
|
||||
return unused_goals
|
||||
|
||||
def reroll(self):
|
||||
"""
|
||||
Rerolls chosen dice. Decrements rolls_left by 1.
|
||||
Assigns a variable to the reroll choice.
|
||||
Takes that variable and assigns it to the dice_to_reroll variable,
|
||||
and loops for only the reroll dice, and casts those dice.
|
||||
"""
|
||||
self.rolls_left -= 1
|
||||
choices = self.get_reroll_choices()
|
||||
dice_to_reroll = self.get_dice_to_reroll(choices)
|
||||
@@ -85,6 +119,9 @@ class Yahtzee:
|
||||
die.roll()
|
||||
|
||||
def get_dice_to_reroll(self, choice_ints):
|
||||
"""Conditional logic to append dice to the dice_to_reroll list,
|
||||
and removes dice that weren't chosen from the choice_ints list.
|
||||
"""
|
||||
dice_to_reroll = []
|
||||
for die in self.dice:
|
||||
if die.face in choice_ints:
|
||||
@@ -93,6 +130,12 @@ class Yahtzee:
|
||||
return dice_to_reroll
|
||||
|
||||
def get_reroll_choices(self):
|
||||
"""
|
||||
Displays an input prompt and inputs the choice.
|
||||
Does some error-checking by calling the reroll_choices_are_valid function,
|
||||
clarifying what to enter. Assigns a variable to a list comprehension
|
||||
and returns that variable.
|
||||
"""
|
||||
print("Which dice do you want to re-roll?")
|
||||
choices = input("> ")
|
||||
while not self.reroll_choices_are_valid(choices):
|
||||
@@ -102,6 +145,12 @@ class Yahtzee:
|
||||
return choice_ints
|
||||
|
||||
def reroll_choices_are_valid(self, choices_str):
|
||||
"""
|
||||
Error-checking for the calling function. Assigns a variable to a
|
||||
list comprehension, looking for an integer in a string list.
|
||||
Loops through the cast dice and removes the choice dice
|
||||
from the list comprehension. Returns an empty list for choice_ints.
|
||||
"""
|
||||
if not choices_str.isdigit():
|
||||
return False
|
||||
choice_ints = [int(digit) for digit in choices_str]
|
||||
|
||||
Reference in New Issue
Block a user