Write a Python version 352 program that simulates how often

Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make certain combinations. For example, the Yahtzee combination is formed by all five dice having the same value. A large straight occurs when all of the dice are in consecutive order (e.g., 1,2,3,4,5 or 2,3,4,5,6). If you haven’t played Yahtzee, you can find out more from various online sources such as Wikipedia.

Once the five dice are rolled, the following 7 hands in Yahtzee are of interest.

Directions for writing your program.

a) q2.py has been provided, ( https://repl.it/EWvm/0 ) where roll_dice() function has been written for you. roll_dice() returns a list of 5 random integers between 1 and 6. You are respon- sible for writing the main() function and the functions representing the different Yahtzee hands. You can add any additional user-defined functions you may need to complete your program.

b) The user will enter the number of dice rolls at the > prompt (see example output). Suppose the user enters 10. Then, your simulation will show the number (as a percent) of rolls that were Yahtzee, Four of a kind, Three of a kind, etc.

c) The five dice are represented as a list. For example, the list [2, 3, 6, 6, 1], says that the first die has a face value of 2, the second die has a face value of 3, the third die has a face value of 6, etc.

d) The parameter dice to each function is a local variable that is a list that contains the result of rolling 5 dice.

e) In the program provided, there is a function for each of the dice hand combinations in Yahtzee. When writing these functions, return the Boolean values True or False if the dice contains the combination of interest. For example, if the dice input to the yahtzee function is [1,1,1,1,1] (e.g., yahtzee([1,1,1,1,1])), then the function should return True. If the input to the yahtzee function is [2,1,6,4,6], then the function should return False.

f) You will use the results of your functions to help you calculate the probability of the combinations of interest.

g) You might find sets and the sorted() built-in function to be especially useful for your program.

h) When testing your program, don’t be afraid to try a large number of rolls like 100,000 or 1,000,000.

Solution

Code:

# Computes the probability of the various hands in Yahtzee.

import random
count_three=0
count_four=0
count_full=0
count_small=0
count_large=0
count_yah=0

def roll_dice():
\'\'\'Returns 5 randomly rolled dice.\'\'\'
dice = []
for i in range(5):
dice += [random.randint(1,6)]
dice2=dice.sort()
print(dice)
return dice

def three_of_a_kind(dice):
\'\'\'Returns True or False if the dice are three of a kind.\'\'\'
global count_three
dice=set(dice)
if (len(dice)==3):
   count_three+=1
else:
   pass

def four_of_a_kind(dice):
\'\'\' Returns True or False if the dice are four of a kind. \'\'\'
global count_four
dice.sort()
dice2=set(dice)
if (len(dice2)==2):
   if (dice[2]==dice[3]):
       count_four+=1
   else:
       pass
else:
   pass

def full_house(dice):
\'\'\'Returns True of False if the dice are a full house.\'\'\'
global count_full
dice.sort()
dice2=set(dice)
if (len(dice2)==2):
   if (dice[2]!=dice[3]):
       count_full+=1
   else:
       pass
else:
   pass

def small_straight(dice):
\'\'\' Returns True or False if the dice represent a small straight. \'\'\'
global count_small
dice.sort()
dice1=dice[:4]
if (dice1 == (1,2,3,4) and dice[4]!=5):
count_small+=1
elif(dice1 == (2,3,4,5) and dice[4]!=6):
count_small+=1
else:
pass
       
def large_straight(dice):
\'\'\' Returns True or False if the dice represent a large straight. \'\'\'
global count_large
dice.sort()
if (dice == (1,2,3,4,5)):
count_large+=1
elif(dice == (2,3,4,5,6)):
count_large+=1
else:
pass
       
def yahtzee(dice):
\'\'\' Returns True or False if the dice represent Yahtzee\'\'\'
global count_yah
dice=set(dice)
if(len(dice)==1):
   count_yah+=1

def main():
\'\'\'The main block of your code.\'\'\'
total_rolls = 0.0
num_rolls=int(input(\"Number of Dice rolls: \"))
for i in range( num_rolls):
dice=roll_dice()
three_of_a_kind(dice)
four_of_a_kind(dice)
full_house(dice)
small_straight(dice)
large_straight(dice)
yahtzee(dice)
percent_yahtzee = count_yah/num_rolls * 100
percent_fourofkind = count_four/num_rolls * 100
percent_largestraight = count_large/num_rolls * 100
percent_smallstraight = count_small/num_rolls * 100
percent_fullhouse = count_full/num_rolls * 100
percent_threeofkind = count_three/num_rolls * 100
  
print(\"Number of Dice rolls:\" , + num_rolls)
print(\"Yahtzee: \", + percent_yahtzee)
print(\"Four of a kind: \", + percent_fourofkind)
print(\"Large straight: \", + percent_largestraight )
print(\"Small straight : \", + percent_smallstraight)
print(\"Full house: \", + percent_fullhouse)
print(\"Three of a kind: \", + percent_threeofkind)
return
  

main()

Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make
Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make
Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site