Question: Gain experience using NumPy arrays to solve problems. In a modified game of Yahtzee, five eight - sided dice are rolled once. For this assignment,

Gain experience using NumPy arrays to solve problems. In a modified game of Yahtzee, five eight-sided dice are rolled once.
For this assignment, you will simulate this modified game of Yahtzee to determine how likely certain outcomes are.
In this modified version of Yahtzee, a Low Roll occurs when each of the five dice is either a 1 or an 2. For example, 1-1-1-1-1 or 2-2-1-2-1.
In this modified version of Yahtzee, a Three of a Kind occurs when three of the dice show the same number. The other two dice must not show this number and must also be different from one another. For example, 4-7-4-4-2 but not 4-7-4-4-7.
In this modified version of Yahtzee, a Large Straight occurs when the five numbers can be arranged consecutively (for example, 1-3-4-2-5 or 5-7-4-6-3). Hint: the numpy library contains a sort function. In the section called Class and function definitions, add the missing methods such that when the main is called, it might produce the following output. You will need to write new code, but do not change any of the existing code.
Number of Rolls: 20000
---------------------
Number of Low Rolls: 18
Percent: 0.09%
Number of Three of a Kinds: 1995
Percent: 9.97%
Number of Large Straights: 261
Percent: 1.30%
Yahtzee code:
import numpy as np
import random
class Die:
def __init__(self, sides):
"""A constructor method to create a die"""
self.sides = sides
def roll(self):
"""A general method to roll the die"""
return random.randint(1, self.sides)
class Yahtzee:
def __init__(self, sides):
"""A constructor method that can record 5 dice rolls"""
self.rolls = np.zeros(5, dtype=np.int16)
self.sides = sides
def roll_dice(self):
"""A general method that rolls 5 dice"""
for i in range(len(self.rolls)):
self.rolls[i]= Die(self.sides).roll()
def count_outcomes(self):
"""A helper method that determines how many 1s,2s, etc. were rolled"""
counts = np.zeros(self.sides +1, dtype=np.int16)
for roll in self.rolls:
counts[roll]+=1
return counts
def main(how_many):
low_rolls =0
three_of_a_kinds =0
large_straights =0
game = Yahtzee(8) # 8-sided dice
for i in range(how_many):
game.roll_dice()
if game.is_it_low_roll():
low_rolls +=1
elif game.is_it_three_of_a_kind():
three_of_a_kinds +=1
elif game.is_it_large_straight():
large_straights +=1
print("Number of Rolls:", how_many)
print("---------------------")
print("Number of Low Rolls:", low_rolls)
print("Percent:","{:.2f}%
".format(low_rolls *100/ how_many))
print("Number of Three of a Kinds:", three_of_a_kinds)
print("Percent:","{:.2f}%
".format(three_of_a_kinds *100/ how_many))
print("Number of Large Straights:", large_straights)
print("Percent:","{:.2f}%".format(large_straights *100/ how_many))
n =10000
main(n)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!