Using Python 3.5.2 and IDLE, Write a program that plays a modified Yahtzee game.
ID: 3794671 • Letter: U
Question
Using Python 3.5.2 and IDLE, Write a program that plays a modified Yahtzee game. You will allow a single player to play with 5 dice. For those of you who know how to play the game, you will only play the top half of the game. You will allow the user to have exactly 6 turns. On each turn the player rolls all 5 dice. For the basic game, you will roll for ones on the first turn, twos on the second turn, and so on until you roll for sixes on the last turn. In a single turn you get 3 tries. Each try you may roll all of the dice, some of the dice, or none of the dice. When you have rolled on try one of turn one, you will "keep" the ones that you rolled, and will try for more ones on the next two rolls. The total number of ones rolled on the first turn will be added to your overall score. Then you will start with turn two when you count the total number of twos rolled in all of 3 tries. When you have completed your six turns, a final score is printed out with a congratulatory message.
Explanation / Answer
import random
dice = 0
dice2=[]
threeofkind = 0
fourofkind = 0
fullhouse = 0
yahtzee = 0
sstraight = 0
lstraight = 0
misc = 0
def roll_dice():
'''Returns 5 randomly rolled dice.'''
dice = []
for i in range(5):
dice += [random.randint(1,6)]
dice = sorted(dice)
dice2 = set(dice)
return dice
def three_of_a_kind(dice):
'''Returns True or False if the dice are three of a kind.'''
if len(dice2) == 3:
threeofkind += 1
else:
pass
def four_of_a_kind(dice):
''' Returns True or False if the dice are four of a kind. '''
if dice[0] == dice[-1]:
pass
elif len(dice2) == 2:
if dice[0] == dice[1]:
fourofkind += 1
else:
pass
def full_house(dice):
'''Returns True of False if the dice are a full house.'''
if len(dice2) == 2:
if dice[0] == dice[1]:
fullhouse += 1
elif dice[-1] == dice[-2]:
fullhouse += 1
else:
pass
def small_straight(dice):
if dice == (1,2,3,4,5):
sstraight += 1
else:
pass
def large_straight(dice):
''' Returns True or False if the dice represent a large straight. '''
if dice == (2,3,4,5,6):
lstraight += 1
else:
pass
def yahtzee(dice):
''' Returns True or False if the dice represent Yahtzee'''
if len(dice2) == 1:
yahtzee += 1
else:
pass
def misc(dice):
if len(dice2) == 5 or 4:
misc += 1
else:
pass
def main():
total_rolls = 0.0
num_experiments=int(raw_input("Dice rolls: "))
for i in range(num_experiments):
roll_dice()
three_of_a_kind(dice)
four_of_a_kind(dice)
full_house(dice)
small_straight(dice)
large_straight(dice)
yahtzee(dice)
misc(dice)
yahtzeepre = yahtzee/total_rolls * 100
fourofkindpre = fourofkind/total_rolls * 100
lstraightpre = lstraight/total_rolls * 100
sstraightpre = sstraight/total_rolls * 100
fullhousepre = fullhouse/total_rolls * 100
threeofkindpre = threeofkind/total_rolls * 100
miscpre = misc/total_rolls *100
print "Dice rolls: ", + total_rolls
print "Yahtzee: ", + yahtzeepre, + "%"
print "Four of a kind (but not Yahtzee): ", + fourofkindpre,+ "%"
print "Large straight: ", + lstraightpre , + "%"
print "Small straight (but not large straight): ", + sstraightpre , + "%"
print "Full house (but not three of a kind): ", + fullhousepre, + "%"
print "Three of a kind (but not four of a kind, full house, or Yahtzee): ", + threeofkindpre, + "%"
print "Misc. (not any of the above): ", + miscpre,+ "%"
return
main()
threeofkind = 0
fourofkind = 0
fullhouse = 0
yahtzee = 0
sstraight = 0
lstraight = 0
misc = 0
def roll_dice():
'''Returns 5 randomly rolled dice.'''
dice = []
for i in range(5):
dice += [random.randint(1,6)]
dice = sorted(dice)
dice2 = set(dice)
return dice
def three_of_a_kind(dice):
'''Returns True or False if the dice are three of a kind.'''
if len(dice2) == 3:
threeofkind += 1
else:
pass
def four_of_a_kind(dice):
''' Returns True or False if the dice are four of a kind. '''
if dice[0] == dice[-1]:
pass
elif len(dice2) == 2:
if dice[0] == dice[1]:
fourofkind += 1
else:
pass
def full_house(dice):
'''Returns True of False if the dice are a full house.'''
if len(dice2) == 2:
if dice[0] == dice[1]:
fullhouse += 1
elif dice[-1] == dice[-2]:
fullhouse += 1
else:
pass
def small_straight(dice):
if dice == (1,2,3,4,5):
sstraight += 1
else:
pass
def large_straight(dice):
''' Returns True or False if the dice represent a large straight. '''
if dice == (2,3,4,5,6):
lstraight += 1
else:
pass
def yahtzee(dice):
''' Returns True or False if the dice represent Yahtzee'''
if len(dice2) == 1:
yahtzee += 1
else:
pass
def misc(dice):
if len(dice2) == 5 or 4:
misc += 1
else:
pass
def main():
total_rolls = 0.0
num_experiments=int(raw_input("Dice rolls: "))
for i in range(num_experiments):
roll_dice()
three_of_a_kind(dice)
four_of_a_kind(dice)
full_house(dice)
small_straight(dice)
large_straight(dice)
yahtzee(dice)
misc(dice)
yahtzeepre = yahtzee/total_rolls * 100
fourofkindpre = fourofkind/total_rolls * 100
lstraightpre = lstraight/total_rolls * 100
sstraightpre = sstraight/total_rolls * 100
fullhousepre = fullhouse/total_rolls * 100
threeofkindpre = threeofkind/total_rolls * 100
miscpre = misc/total_rolls *100
print "Dice rolls: ", + total_rolls
print "Yahtzee: ", + yahtzeepre, + "%"
print "Four of a kind (but not Yahtzee): ", + fourofkindpre,+ "%"
print "Large straight: ", + lstraightpre , + "%"
print "Small straight (but not large straight): ", + sstraightpre , + "%"
print "Full house (but not three of a kind): ", + fullhousepre, + "%"
print "Three of a kind (but not four of a kind, full house, or Yahtzee): ", + threeofkindpre, + "%"
print "Misc. (not any of the above): ", + miscpre,+ "%"
return
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.