generated from mwc/lab_dice
Updateed checkpoint 2:
Writing comments is about describing what should happen vs. just writing what the code does technically. I will use docstrings and comments, they are great for learning.
This commit is contained in:
parent
80535dd986
commit
a00d27af7e
Binary file not shown.
Binary file not shown.
|
@ -20,7 +20,6 @@ class FiveDice:
|
|||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_three_of_a_kind(self):
|
||||
count = Counter(self.faces())
|
||||
for face_count in count.values():
|
||||
|
|
39
yahtzee.py
39
yahtzee.py
|
@ -4,17 +4,26 @@ class Yachtzee:
|
|||
"""A command-line Yahtzee game.
|
||||
This version of Yahtzee is initialized with a list of goals.
|
||||
"""
|
||||
|
||||
"""runs when the object is instantiated, sets attributes for score, goals and dice. Dice is creating a dice roll 5 times
|
||||
"""
|
||||
def __init__(self, goals):
|
||||
self.score = 0
|
||||
self.goals = goals
|
||||
self.dice = [Die() for num in range(5)]
|
||||
|
||||
|
||||
"""prints a welcome message and uses a while loop to say if the goal is 0, then run the play_round() method. then print your final score out.
|
||||
"""
|
||||
def play(self):
|
||||
print("Welcome to Yachtzee!")
|
||||
while self.count_unused_goals() > 0:
|
||||
self.play_round()
|
||||
print(f"Your final score was {self.score}")
|
||||
|
||||
|
||||
"""its prints =*80 then it sets rolls to 3. Then it shows how many rolls you have left. it shows how many rolls you ahve left after thdice roles. adds to the score
|
||||
"""
|
||||
def play_round(self):
|
||||
print("=" * 80)
|
||||
self.rolls_left = 3
|
||||
|
@ -25,10 +34,14 @@ class Yachtzee:
|
|||
goal.used = True
|
||||
self.score += goal.score(self.dice)
|
||||
|
||||
"""Used to show score, rolls left and dice in the game.
|
||||
"""
|
||||
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}.")
|
||||
|
||||
"""Used to show score, rolls left and dice in the game.
|
||||
"""
|
||||
def choose_goal(self):
|
||||
options = []
|
||||
unused_goals = self.get_unused_goals()
|
||||
|
@ -44,7 +57,10 @@ class Yachtzee:
|
|||
return self.choose_goal()
|
||||
else:
|
||||
return unused_goals[choice]
|
||||
|
||||
|
||||
"""
|
||||
Displays options amd prompts user to make choice. If input is valid return the choice.
|
||||
"""
|
||||
def get_choice(self, options):
|
||||
print("What would you like to do?")
|
||||
for i, option in enumerate(options):
|
||||
|
@ -55,6 +71,9 @@ class Yachtzee:
|
|||
choice = input("> ")
|
||||
return int(choice)
|
||||
|
||||
"""
|
||||
Chekcs if the users chouice is valid, returns true or false.
|
||||
"""
|
||||
def option_choice_is_valid(self, choice, options):
|
||||
if not choice.isdigit():
|
||||
return False
|
||||
|
@ -64,9 +83,15 @@ class Yachtzee:
|
|||
return False
|
||||
return True
|
||||
|
||||
"""
|
||||
counts unused goals
|
||||
"""
|
||||
def count_unused_goals(self):
|
||||
return len(self.get_unused_goals())
|
||||
|
||||
"""
|
||||
iterates through sel.goals and ammends unused goals list with unused goals.
|
||||
"""
|
||||
def get_unused_goals(self):
|
||||
unused_goals = []
|
||||
for goal in self.goals:
|
||||
|
@ -74,6 +99,9 @@ class Yachtzee:
|
|||
unused_goals.append(goal)
|
||||
return unused_goals
|
||||
|
||||
"""
|
||||
rerolls dice, decreased amount of rolls
|
||||
"""
|
||||
def reroll(self):
|
||||
self.rolls_left -= 1
|
||||
choices = self.get_reroll_choices()
|
||||
|
@ -81,6 +109,9 @@ class Yachtzee:
|
|||
for die in dice_to_reroll:
|
||||
die.roll()
|
||||
|
||||
"""
|
||||
Determiens whcih dice should be rerolled
|
||||
"""
|
||||
def get_dice_to_reroll(self, choice_ints):
|
||||
dice_to_reroll = []
|
||||
for die in self.dice:
|
||||
|
@ -89,6 +120,9 @@ class Yachtzee:
|
|||
dice_to_reroll.append(die)
|
||||
return dice_to_reroll
|
||||
|
||||
"""
|
||||
Prompts to enter which dice to re-roll
|
||||
"""
|
||||
def get_reroll_choices(self):
|
||||
print("Which dice do you want to re-roll?")
|
||||
choices = input("> ")
|
||||
|
@ -98,6 +132,9 @@ class Yachtzee:
|
|||
choice_ints = [int(digit) for digit in choices]
|
||||
return choice_ints
|
||||
|
||||
"""
|
||||
Validates choices for reroll
|
||||
"""
|
||||
def reroll_choices_are_valid(self, choices_str):
|
||||
if not choices_str.isdigit():
|
||||
return False
|
||||
|
|
Loading…
Reference in New Issue