Checkpoint 3

I enjoyed this style of problem solving, but it took me a while to figure
out the small straight. I kept thinking that I had it but then I realized
with testing and playing the game that I had forgotten about if the die
had duplicates. Originally 1,1,2,3,4 was not detecting a straight. Then
I thought checking in reverse would help this, but I forgot about
duplicates in the middle (1,2,3,3,4). Ultimately I had to add some code
to remove any duplicates from the list and then it finally worked.

I did find this problem solving to be easier to code once I found the
problem, but sometimes the problem (like with the example above) was not
always apparent. I spent more time thinking about how to solve the problem
in this topic compared to other topics, but less time debugging the actual
code than in previous units. I think I am becoming more familiar with the
coding and am starting to be able to use it to solve new problems.
This commit is contained in:
Chris Mekelburg 2024-11-15 21:33:51 -05:00
parent a05e5c3630
commit 57b45dbc3a
3 changed files with 277 additions and 5 deletions

16
play.py
View File

@ -1,14 +1,20 @@
from yahtzee import Yachtzee
from yahtzee_goals import (
GoalOnes,
GoalTwos,
GoalThrees,
)
from yahtzee_goals import *
goals = [
GoalOnes(),
GoalTwos(),
GoalThrees(),
GoalFours(),
GoalFives(),
GoalSixes(),
ThreeofaKind(),
FourofaKind(),
FullHouse(),
SmallStraight(),
LargeStraight(),
YAHTZEE(),
Chance()
]
game = Yachtzee(goals)

12
testing.py Normal file
View File

@ -0,0 +1,12 @@
from yahtzee_goals import SmallStraight
from die import Die
goal = SmallStraight()
dice= [Die(),Die(),Die(),Die(),Die()]
dice[0]. face = 6
dice[1]. face = 4
dice[2]. face = 1
dice[3]. face = 3
dice[4]. face = 4
print(dice, goal.score(dice))

View File

@ -43,3 +43,257 @@ class GoalThrees:
if die.face == 3:
total += 3
return total
class GoalFours:
"Four points for each four"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Fours ({potential_score})"
def score(self, dice):
total = 0
for die in dice:
if die.face == 4:
total += 4
return total
class GoalFives:
"Five points for each five"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Fives ({potential_score})"
def score(self, dice):
total = 0
for die in dice:
if die.face == 5:
total += 5
return total
class GoalSixes:
"Six points for each six"
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Sixes ({potential_score})"
def score(self, dice):
total = 0
for die in dice:
if die.face == 6:
total += 6
return total
class ThreeofaKind:
"""If 3 or more dice are the same, returns the value of all 5 dice,
otherwise returns a score of 0"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Three of a Kind ({potential_score})"
def score(self, dice):
if self.check_3_of_a_kind(dice):
return self.dice_total(dice)
else:
return 0
def check_3_of_a_kind(self,dice):
for value in range(7):
instances = 0
for die in dice:
if die.face == value:
instances +=1
if instances >= 3:
return True
return False
def dice_total(self,dice):
total = 0
for die in dice:
total += die.face
return total
class FourofaKind:
"""If 4 or more dice are the same, returns the value of all 5 dice,
otherwise returns a score of 0"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Four of a Kind ({potential_score})"
def score(self, dice):
if self.check_4_of_a_kind(dice):
return self.dice_total(dice)
else:
return 0
def check_4_of_a_kind(self,dice):
for value in range(7):
instances = 0
for die in dice:
if die.face == value:
instances +=1
if instances >= 4:
return True
return False
def dice_total(self,dice):
total = 0
for die in dice:
total += die.face
return total
class FullHouse:
"""If there are 3 of the same dice and 2 of the same dice, returns 25,
otherwise returns a score of 0"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Full House ({potential_score})"
def score(self, dice):
if self.check_full_house(dice):
return 25
else:
return 0
def check_full_house(self,dice):
for value in range(7):
instances = 0
for die in dice:
if die.face == value:
instances +=1
if instances == 3:
triple_dice = value
list_die = [1,2,3,4,5,6]
list_die.remove(value)
for value in list_die:
instances_2 = 0
for die in dice:
if die.face == value:
instances_2 +=1
if instances_2 ==2:
return True
return False
class SmallStraight:
"""If there are 4 dice that can be put in consecutive order,
return 30, otherwise return a score of 0"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Small Straight ({potential_score})"
def score(self, dice):
if self.check_small_straight(dice):
return 30
else:
return 0
def check_small_straight(self,dice):
dice_faces =[]
for die in dice:
dice_faces.append(die.face)
dice_faces.sort()
'''need to check for duplicates first, do not need to check index 0 or 4'''
if dice_faces[1]==dice_faces[2]:
dice_faces.remove(dice_faces[1])
if dice_faces[2]==dice_faces[3]:
dice_faces.remove(dice_faces[2])
'''now check if ordered list increases by 1, do not need to check last index'''
if dice_faces[0] + 1 == dice_faces[1]:
if dice_faces[1] + 1 == dice_faces[2]:
if dice_faces[2] + 1 == dice_faces[3]:
return True
else:
'''now reverse the list and check if they decrease by 1, do not need to check last index'''
dice_faces.sort(reverse=True)
if dice_faces[0] - 1 == dice_faces[1]:
if dice_faces[1] - 1 == dice_faces[2]:
if dice_faces[2] - 1 == dice_faces[3]:
return True
else:
return False
class LargeStraight:
"""If there are 5 dice that can be put in consecutive order,
return 40, otherwise return a score of 0"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Large Straight ({potential_score})"
def score(self, dice):
if self.check_large_straight(dice):
return 40
else:
return 0
def check_large_straight(self,dice):
dice_faces =[]
for die in dice:
dice_faces.append(die.face)
dice_faces.sort()
if dice_faces[0] + 1 == dice_faces[1]:
if dice_faces[1] + 1 == dice_faces[2]:
if dice_faces[2] + 1 == dice_faces[3]:
if dice_faces[3] + 1 ==dice_faces[4]:
return True
else:
return False
class YAHTZEE:
"""If there are 5 dice that are all the same,
return 50, otherwise return a score of 0"""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"YAHTZEE ({potential_score})"
def score(self, dice):
if self.check_YAHTZEE(dice):
return 50
else:
return 0
def check_YAHTZEE(self,dice):
dice_faces =[]
for die in dice:
dice_faces.append(die.face)
if dice_faces[0]==dice_faces[1]==dice_faces[2]==dice_faces[3]==dice_faces[4]:
return True
else:
return False
class Chance:
"""Returns the total of all 5 dice."""
used = False
def prompt(self, dice):
potential_score = self.score(dice)
return f"Chance ({potential_score})"
def score(self, dice):
total = 0
for die in dice:
total += die.face
return total
def dice_total(self,dice):
total = 0
for die in dice:
total += die.face
return total