Python: Craps is a dice-based game played in many casinos. Like blackjack, a pla
ID: 3666205 • Letter: P
Question
Python:
Craps is a dice-based game played in many casinos. Like blackjack, a player plays against the house. The game starts with the player throwing a pair of standard, six-sided dice. If the player rolls a total of 7 or 11, the player wins. If the player rolls a total of 2, 3, or 12, the player loses. For all other roll values, the player will repeatedly roll the pair of dice until either she/he rolls the initial value again (in which case she/he wins) or 7 (in which case she/he loses). Develop a classCrapsthat uses the Die class developed above to simulate a game of craps. The class should be a subclass of the object class, not Die. The class supports two methods:
__init__() which takes no parameters. It creates two six-sided Die objects and stores them into two variables in the object. It then stores the sum of the initial rolls of the two dice into a variable in the class representing the initial roll of the player. If the initial roll is 7 or 11, the constructor reports that the player wins. If the initial roll is 2, 3, or 12, the constructor reports that the player loses. If the initial roll is any other value the constructor suggests that the player throw for point.
forPoint() which takes no parameters and rolls the next round of the craps game. If the player rolls a 7, the method reports that the player lost. If the player rolls the same value as the initial roll, the method reports that the player wins. Otherwise the method suggests that the player again roll for point.
The following shows how the Craps class and methods could be used. Note that the situation where the user immediately loses is not represented although you have to handle that case in your implementation
Python 3.41Shell Eile Edit gel Debug Optors indows telp Craps(I Throv total: 11. Tou von! > Craps) Throv total: 5. Thro tor Point c.torPoint) Throv total: 8. Thro for Point Throv total: 8. Throo tor Point total: S. You on >>c.forPoine >c.for Point Throv total: . Tou von Craps Throv total: 11. You oD! Craps| Throw total: 6. Throo tor Point >c.forPoint 0 hrov total: S. Thro for Poin. c.forPoint 0 Throv total: S. Thro tor Point >c.torPoine 0 Throw total: S. Throu tor Point >c.torPoint1 Throv total: 7. You lost! Craps Throv total: 6.Thro tor Point >c.forPoine Throv total: Throw for Poiat. c.torPoint 0 Throv total: 3. Thro tor Point ·Throutor Point. e.forPoint Throv total: 9. Throu tor Point. >c.forPoine0 Tazov total: 7. Tou lostExplanation / Answer
from random import randrange
def main():
printIntro()
n = getInput()
x = crapsCasino(n)
printSummary(x,n)
def printIntro():
print('Program to simulate Craps Casino.')
print('Probability of winning the ber will show in result ')
def getInput():
n = eval(input('How many games to simulate: '))
return n
def crapsCasino(rounds):
win = 0
for i in range(rounds):
init = firstRoll()
if init == 'win': win = win+1
elif (type(init)) == int:
final = pointRoll(init)
if final == 'win': win = win+1
return win
def firstRoll():
x = dice()
if x == 7 or x == 11:
status = 'win'
elif x ==2 or x ==3 or x ==12:
status = 'lose'
else:
status = x
return status
def pointRoll(point):
x = 0
while not pointCheck(x,point):
x = dice()
if x == point:
status = 'win'
elif x == 7:
status = 'lose'
return status
def pointCheck(x,point):
return x == 7 or x == point
def dice():
return randrange(1,7)+randrange(1,7)
def printSummary(win,rounds):
print('==========')
print(' Games simulated:',rounds)
print('Total winning:',win)
print(' Probability of winning Craps is: {:0.2}'.format(win/rounds))
if __name__ == '__main__': main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.