generated from mwc/lab_dice
made comments on what i think the code does
This commit is contained in:
@@ -18,6 +18,60 @@ class FiveDice:
|
|||||||
if face != 1:
|
if face != 1:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def value_count(self):
|
||||||
|
|
||||||
|
count_ones = 0
|
||||||
|
count_twos = 0
|
||||||
|
count_threes = 0
|
||||||
|
count_fours = 0
|
||||||
|
count_fives = 0
|
||||||
|
count_sixes = 0
|
||||||
|
|
||||||
|
for face in self.faces():
|
||||||
|
count_ones+=1
|
||||||
|
if face == 2:
|
||||||
|
count_twos+=1
|
||||||
|
if face == 3:
|
||||||
|
count_threes+=1
|
||||||
|
if face == 4:
|
||||||
|
count_fours+=1
|
||||||
|
if face == 5:
|
||||||
|
count_fives+=1
|
||||||
|
if face == 6:
|
||||||
|
count_sixes+=1
|
||||||
|
counts=
|
||||||
|
'ones':count_ones,
|
||||||
|
'twos':count_twos,
|
||||||
|
'threes':count_threes,
|
||||||
|
'fours':count_fours,
|
||||||
|
'fives':count_fives,
|
||||||
|
'sixes':count_sixes,
|
||||||
|
return counts
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def all_ones(self):
|
||||||
|
value_count = self.value_count()
|
||||||
|
|
||||||
|
if value_count['ones'] < 5:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def is_three_of_a_kind(self):
|
||||||
|
|
||||||
|
value_count = None
|
||||||
|
value_count = self.value.count()
|
||||||
|
for value in value_count.values():
|
||||||
|
if value >= 3:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
dice = FiveDice()
|
dice = FiveDice()
|
||||||
successes = 0
|
successes = 0
|
||||||
|
|||||||
1
die.py
1
die.py
@@ -15,3 +15,4 @@ class Die:
|
|||||||
def roll(self):
|
def roll(self):
|
||||||
self.face = randint(1, 6)
|
self.face = randint(1, 6)
|
||||||
return self.face
|
return self.face
|
||||||
|
|
||||||
15
yahtzee.py
15
yahtzee.py
@@ -10,13 +10,15 @@ class Yahtzee:
|
|||||||
self.dice = [Die() for num in range(5)]
|
self.dice = [Die() for num in range(5)]
|
||||||
|
|
||||||
def play(self):
|
def play(self):
|
||||||
|
#sets the game up
|
||||||
print("Welcome to Yachtzee!")
|
print("Welcome to Yachtzee!")
|
||||||
self.score = 0
|
self.score = 0
|
||||||
for goal in self.goals:
|
for goal in self.goals:
|
||||||
goal.used = False
|
goal.used = False
|
||||||
while self.count_unused_goals() > 0:
|
while self.count_unused_goals() > 0:
|
||||||
self.play_round()
|
self.play_round()
|
||||||
print(f"Your final score was {self.score}")
|
print(f"Your final score was {self.score}"
|
||||||
|
#says your final score
|
||||||
|
|
||||||
def play_round(self):
|
def play_round(self):
|
||||||
print("=" * 80)
|
print("=" * 80)
|
||||||
@@ -27,10 +29,12 @@ class Yahtzee:
|
|||||||
goal = self.choose_goal()
|
goal = self.choose_goal()
|
||||||
goal.used = True
|
goal.used = True
|
||||||
self.score += goal.score(self.dice)
|
self.score += goal.score(self.dice)
|
||||||
|
#says the amount of rolls you have left
|
||||||
|
|
||||||
def show_status(self):
|
def show_status(self):
|
||||||
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}.")
|
||||||
|
#prints the score and rolls left
|
||||||
|
|
||||||
def choose_goal(self):
|
def choose_goal(self):
|
||||||
options = []
|
options = []
|
||||||
@@ -47,6 +51,7 @@ class Yahtzee:
|
|||||||
return self.choose_goal()
|
return self.choose_goal()
|
||||||
else:
|
else:
|
||||||
return unused_goals[choice]
|
return unused_goals[choice]
|
||||||
|
#says the unused goals
|
||||||
|
|
||||||
def get_choice(self, options):
|
def get_choice(self, options):
|
||||||
print("What would you like to do?")
|
print("What would you like to do?")
|
||||||
@@ -57,6 +62,7 @@ class Yahtzee:
|
|||||||
print("Sorry, that's not a valid choice.")
|
print("Sorry, that's not a valid choice.")
|
||||||
choice = input("> ")
|
choice = input("> ")
|
||||||
return int(choice)
|
return int(choice)
|
||||||
|
#gives you the choices
|
||||||
|
|
||||||
def option_choice_is_valid(self, choice, options):
|
def option_choice_is_valid(self, choice, options):
|
||||||
if not choice.isdigit():
|
if not choice.isdigit():
|
||||||
@@ -66,9 +72,11 @@ class Yahtzee:
|
|||||||
if int(choice) >= len(options):
|
if int(choice) >= len(options):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
#if the choice you said isnt there it says you cant do that
|
||||||
|
|
||||||
def count_unused_goals(self):
|
def count_unused_goals(self):
|
||||||
return len(self.get_unused_goals())
|
return len(self.get_unused_goals())
|
||||||
|
#counts the unused goals
|
||||||
|
|
||||||
def get_unused_goals(self):
|
def get_unused_goals(self):
|
||||||
unused_goals = []
|
unused_goals = []
|
||||||
@@ -76,6 +84,7 @@ class Yahtzee:
|
|||||||
if not goal.used:
|
if not goal.used:
|
||||||
unused_goals.append(goal)
|
unused_goals.append(goal)
|
||||||
return unused_goals
|
return unused_goals
|
||||||
|
#gets the unused goals
|
||||||
|
|
||||||
def reroll(self):
|
def reroll(self):
|
||||||
self.rolls_left -= 1
|
self.rolls_left -= 1
|
||||||
@@ -83,6 +92,7 @@ class Yahtzee:
|
|||||||
dice_to_reroll = self.get_dice_to_reroll(choices)
|
dice_to_reroll = self.get_dice_to_reroll(choices)
|
||||||
for die in dice_to_reroll:
|
for die in dice_to_reroll:
|
||||||
die.roll()
|
die.roll()
|
||||||
|
#rerolls
|
||||||
|
|
||||||
def get_dice_to_reroll(self, choice_ints):
|
def get_dice_to_reroll(self, choice_ints):
|
||||||
dice_to_reroll = []
|
dice_to_reroll = []
|
||||||
@@ -91,6 +101,7 @@ class Yahtzee:
|
|||||||
choice_ints.remove(die.face)
|
choice_ints.remove(die.face)
|
||||||
dice_to_reroll.append(die)
|
dice_to_reroll.append(die)
|
||||||
return dice_to_reroll
|
return dice_to_reroll
|
||||||
|
#makes the dice able to reroll
|
||||||
|
|
||||||
def get_reroll_choices(self):
|
def get_reroll_choices(self):
|
||||||
print("Which dice do you want to re-roll?")
|
print("Which dice do you want to re-roll?")
|
||||||
@@ -100,6 +111,7 @@ class Yahtzee:
|
|||||||
choices = input("> ")
|
choices = input("> ")
|
||||||
choice_ints = [int(digit) for digit in choices]
|
choice_ints = [int(digit) for digit in choices]
|
||||||
return choice_ints
|
return choice_ints
|
||||||
|
#gets the reroll choices
|
||||||
|
|
||||||
def reroll_choices_are_valid(self, choices_str):
|
def reroll_choices_are_valid(self, choices_str):
|
||||||
if not choices_str.isdigit():
|
if not choices_str.isdigit():
|
||||||
@@ -109,3 +121,4 @@ class Yahtzee:
|
|||||||
if die.face in choice_ints:
|
if die.face in choice_ints:
|
||||||
choice_ints.remove(die.face)
|
choice_ints.remove(die.face)
|
||||||
return len(choice_ints) == 0
|
return len(choice_ints) == 0
|
||||||
|
#sees if the reroll choices are valid
|
||||||
|
|||||||
Reference in New Issue
Block a user