Question

Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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()

Add a comment
Know the answer?
Add Answer to:
Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputOrGenerateDiceRolls (which is...

    Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputOrGenerateDiceRolls (which is as follows: InputOrGenerateDiceRolls program should ask the user whether the user prefers 1) to roll his/her own dice, 2) use computer-generated dice rolls, or 3) quit the program. If the user prefers to roll his/her own dice, the program should prompt the user to enter 5 digits in the range from 1-6 separated by spaces in any order with duplicates allowed. However, if the...

  • Develop a C++ program which will play a simplified version of the dice game Yahtzee For...

    Develop a C++ program which will play a simplified version of the dice game Yahtzee For simplicity, this version of the game only uses four dice No prompting of the user for input values is required Simply use four variables called A, B, C, D to maintain dice roll values int A = 2; No input validation is required as well From the input you will determine if the player rolled 4 of a kind, 3 of a kind, 2...

  • I need java code for this . The assignment requires using objects Exercise 2 of 2:...

    I need java code for this . The assignment requires using objects Exercise 2 of 2: Create a program that lets the player play a simplified game of Yahtzee. The game starts with the user throwing five 6-sided dice. The user can choose which dice to keep and which dice to re-roll. The user can re-roll a maximum of 2 times. After two times, the user has 5 dice with a certain value. The player then has to choose where...

  • Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will...

    Write a C PROGRAM that will simulate rolling a die. When rolling a die, one will get a die face value of 1-6. We can use rand function and math ceiling function to get a random number of 1-6 generated. The program will simulating rolling 5 dice simultaneously and store their face values into an array of size 5. The program will keep rolling the dice and print the following: 1. All face values in one line 2. If any...

  • please do it in C++ Write a program that simulates the rolling of two dice. The...

    please do it in C++ Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2and 12...

  • Modular program mathlab: Write a program in Matlab that would continuously ask the user for an...

    Modular program mathlab: Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “You got it” If the user’s die is smaller, it should display, “Nice try” If the results are the...

  • Can anyone help me with this java program? We are still very early with lessons so...

    Can anyone help me with this java program? We are still very early with lessons so no advanced code please. And if you comment throughout the code that would be incredibly helpful. Thank you Create 2 Java files for this assignment: Die.java and TestDie.java Part 1 - The Die Class             The program Design a Die class that contains the following attributes: sides: represents how many sides a dice has. A dice usually has 6 sides but sometimes could have...

  • Write a java program that simulates the rolling of four dice. The program should use random...

    Write a java program that simulates the rolling of four dice. The program should use random number generator to roll dice. The sum of the four values should then be calculated. Note that each die can show an integer value from 1 to 6, so the sum of the four values will vary from 4 to 24, with 14 being the most frequent sum and 4 and 24 being the least frequent sums. Your program should roll the dice 12,960...

  • Complete each problem separately and perform in python. 1. Create a script that will roll five...

    Complete each problem separately and perform in python. 1. Create a script that will roll five dice and print out their values. Allow the user to roll the dice three times. Prompt them to hit enter to roll. Ex: Hit enter to roll 1,4,3,6,6 Hit enter to roll 3,5,1,2,1 Hit enter to roll 4,3,4,2,6 2. Write a script that will roll five dice (just one time). The user's score will be the sum of the values on the dice. Print...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT