generated from mwc/lab_dice
153 lines
6.6 KiB
Python
153 lines
6.6 KiB
Python
from die import Die
|
|
|
|
class Yachtzee:
|
|
"""A command-line Yahtzee game.
|
|
This version of Yahtzee is initialized with a list of goals.
|
|
"""
|
|
def __init__(self, goals):
|
|
'''Initializes a game of Yahtzee. The initial score is 0, and all goals are available.
|
|
Also creates the five dice that will be used for this game.'''
|
|
self.score = 0
|
|
self.goals = goals
|
|
self.dice = [Die() for num in range(5)]
|
|
|
|
def play(self):
|
|
'''Plays a game of Yahtzee.
|
|
Starts by welcoming the player and will continue playing rounds until all goals have been used.
|
|
Once all goals have been used, the game ends and the final score is presented to the player.'''
|
|
print("Welcome to Yahtzee!")
|
|
while self.count_unused_goals() > 0:
|
|
self.play_round()
|
|
print(f"Your final score was {self.score}")
|
|
|
|
def play_round(self):
|
|
'''Plays a single round of Yahtzee.
|
|
Prints a line to divide this round from the previous round.
|
|
The five dice are rolled to start, and the player is shown the current game status.
|
|
The player can choose to re-roll up to three times with the ultimate end choosing a goal.
|
|
The round concludes when a player chooses a goal to apply their dice toward.
|
|
This goal is marked as used, and the appropriate score from that round is added to the total.'''
|
|
print("=" * 80)
|
|
self.rolls_left = 3
|
|
for die in self.dice:
|
|
die.roll()
|
|
self.show_status()
|
|
goal = self.choose_goal()
|
|
goal.used = True
|
|
self.score += goal.score(self.dice)
|
|
|
|
def show_status(self):
|
|
'''Shows the player their current status.
|
|
Displays the currrent values on the faces of the five dice, the current score and
|
|
the number of re-rolls remaining for this round.'''
|
|
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):
|
|
'''Finds which goals the player can choose from and whether or not the player can re-roll.
|
|
If the player has re-rolls remaining and chooses to re-roll, they can.
|
|
Otherwise, they choose a goal and this function returns the goal chosen.'''
|
|
options = []
|
|
unused_goals = self.get_unused_goals()
|
|
for goal in unused_goals:
|
|
option = goal.prompt(self.dice)
|
|
options.append(option)
|
|
if self.rolls_left > 0:
|
|
options.append("Re-roll")
|
|
choice = self.get_choice(options)
|
|
if options[choice] == "Re-roll":
|
|
self.reroll()
|
|
self.show_status()
|
|
return self.choose_goal()
|
|
else:
|
|
return unused_goals[choice]
|
|
|
|
def get_choice(self, options):
|
|
'''Prompts the player to choose an option, either a goal or to re-roll
|
|
depending on whether the player has re-rolls remaining.
|
|
Each option is assigned a number. Checks to see if the player's response is
|
|
valid. Informs the player if their choice is invalid. When the player chooses
|
|
a valid option, the number corresponding to that option is returned'''
|
|
print("What would you like to do?")
|
|
for i, option in enumerate(options):
|
|
print(f"{i}. {option}")
|
|
choice = input("> ")
|
|
while not self.option_choice_is_valid(choice, options):
|
|
print("Sorry, that's not a valid choice.")
|
|
choice = input("> ")
|
|
return int(choice)
|
|
|
|
def option_choice_is_valid(self, choice, options):
|
|
'''Checks to see if the option a player chose was valid.
|
|
First checks to see if the option chosen was presented in the form of a number.
|
|
If the choice was a number, then checks to see if that number was one of the
|
|
possible options presented to the player. If it was then the choice is valid.
|
|
If the choice was not a number or if the number was not a possible option,
|
|
then the choice is not valid.'''
|
|
if not choice.isdigit():
|
|
return False
|
|
if int(choice) < 0:
|
|
return False
|
|
if int(choice) >= len(options):
|
|
return False
|
|
return True
|
|
|
|
def count_unused_goals(self):
|
|
'''Returns the number of goals the player has not yet used.'''
|
|
return len(self.get_unused_goals())
|
|
|
|
def get_unused_goals(self):
|
|
'''Creates a list of goals the player has not yet used.'''
|
|
unused_goals = []
|
|
for goal in self.goals:
|
|
if not goal.used:
|
|
unused_goals.append(goal)
|
|
return unused_goals
|
|
|
|
def reroll(self):
|
|
'''Re-rolls the dice chosen by the player.
|
|
Decreases the number of re-rolls remaining for this round by 1 and
|
|
and rolls the dice that need to be re-rolled'''
|
|
self.rolls_left -= 1
|
|
choices = self.get_reroll_choices()
|
|
dice_to_reroll = self.get_dice_to_reroll(choices)
|
|
for die in dice_to_reroll:
|
|
die.roll()
|
|
|
|
def get_dice_to_reroll(self, choice_ints):
|
|
'''This function returns the dice that need to be re-rolled.
|
|
The dice that the player wants to re-roll are removed and placed into a list
|
|
and this list of dice to re-roll is returned'''
|
|
dice_to_reroll = []
|
|
for die in self.dice:
|
|
if die.face in choice_ints:
|
|
choice_ints.remove(die.face)
|
|
dice_to_reroll.append(die)
|
|
return dice_to_reroll
|
|
|
|
def get_reroll_choices(self):
|
|
'''Prompts the player to select which dice they would like to re-roll.
|
|
Checks to see that the input is valid. If it is not valid, the player
|
|
is prompted to enter number values of dice they want to re-roll.
|
|
Returns a list of the dice values to re-roll.'''
|
|
print("Which dice do you want to re-roll?")
|
|
choices = input("> ")
|
|
while not self.reroll_choices_are_valid(choices):
|
|
print("Please enter the numbers on dice you want to re-roll.")
|
|
choices = input("> ")
|
|
choice_ints = [int(digit) for digit in choices]
|
|
return choice_ints
|
|
|
|
def reroll_choices_are_valid(self, choices_str):
|
|
'''This function checks to see if choices for re-rolls are valid.
|
|
First checks if the values the player wants to re-roll are numbers.
|
|
Then checks that each value the player wants to re-roll corresponds
|
|
to a unique dice-value the player currently has.'''
|
|
if not choices_str.isdigit():
|
|
return False
|
|
choice_ints = [int(digit) for digit in choices_str]
|
|
for die in self.dice:
|
|
if die.face in choice_ints:
|
|
choice_ints.remove(die.face)
|
|
return len(choice_ints) == 0
|