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): self.score = 0 self.goals = goals self.dice = [Die() for num in range(5)] """Plays a game of Yahtzee, round by round until all goals have been used. """ def play(self): print("Welcome to Yachtzee!") while self.count_unused_goals() > 0: self.play_round() print(f"Your final score was {self.score}") """Plays a single round of Yahtzee, allowing up to 3 re-rolls and ending when the player chooses a goal to use to score. """ def play_round(self): 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) """Reports stats between rolls, including current score, re-rolls remaining and the current values of the dice. """ def show_status(self): dice = ', '.join([str(die) for die in self.dice]) print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.") """Allows the user to choose the goal to use on the scoresheet from the list of remaining unused goals. """ def choose_goal(self): 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] """Processes the user's choice for validity, moving on after receiving a valid goal choice. """ def get_choice(self, options): 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) """Only returns true when the choice from the user is one of the numbered list items available to them. """ def option_choice_is_valid(self, choice, options): if not choice.isdigit(): return False if int(choice) < 0: return False if int(choice) >= len(options): return False return True """Used to determine when the game is over because all goals have been used. """ def count_unused_goals(self): return len(self.get_unused_goals()) """Generates the list of unused goals at that point in the game. """ def get_unused_goals(self): unused_goals = [] for goal in self.goals: if not goal.used: unused_goals.append(goal) return unused_goals """Removes a re-roll available to the player and re-rolls selected dice. """ def reroll(self): 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() """Collects the individual dice to be rerolled after validation. """ def get_dice_to_reroll(self, choice_ints): 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 """Collects which dice to be rerolled from the user as a single, multi-place number, like "6442". """ def get_reroll_choices(self): 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 """Checks that the dice to be rerolled are available to be rerolled. """ def reroll_choices_are_valid(self, choices_str): 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