In: Computer Science
Write a program in Python to simulate a Craps game:
1. When you bet on the Pass Line, you win (double your money) if the FIRST roll (a pair of dice) is a 7 or 11, you lose if it is ”craps” (2, 3, or 12). Otherwise, if it is x ∈ {4, 5, 6, 8, 9, 10}, then your point x is established, and you win when that number is rolled again before ’7’ comes up. The game is over and you will lose when ’7’ comes up before your established point.
Please can you do a step by step explanation and how it works?
from random import *
#get usr input function
def get_Input():
#Enter the amount of games of craps need to be simulate
number=int(raw_input())
return number
#printing the result function
def print_Results(number, win_count):
#printing the result of the simulation
print('\nFor', number, 'games of craps simulated,', win_count, 'games were wins, giving success rate of '+ str(100*(win_count/number)) + '%.')
#roll function which return the win count
def sim_Roll(n):
#declare roll count initialise to 0
rollCount=0
#declare wincount initialise to 0
winCount=0
#declare pointforroll initialise to 0
PointForRoll=0
#using while loop till n given input by user
while rollCount < n:
#increment the rollcount
rollCount=rollCount+1
#get the random roll of two roll and add , save it to randomroll variable
randomRoll=randrange(1,7) + randrange (1,7)
#given condition in the question if roll is 2,3,or 12 then player loss
#so wincount will not incement
if randomRoll == 2 or randomRoll == 3 or randomRoll == 12:
winCount = winCount + 0
#given condition in the question if roll is 7,or 11 then player loss
#so wincount will incement by 1
if randomRoll == 7 or randomRoll == 11:
winCount = winCount + 1
#else while loop using pointfor roll according to the given condition
else:
while PointForRoll != 7 or PointForRoll != randomRoll:
PointForRoll = randrange(1,7) + randrange(1,7)
#if pointfor roll is equal to random roll
#then wincout will be wincount number
if PointForRoll == randomRoll:
winCount=winCount
#if pointfor toll is equal to 7 then wincount incement by 1
if PointForRoll == 7:
winCount=winCount+1
#returning pintforroll
return PointForRoll
#returning the wincount
return winCount
#craps function for the simulation
def craps():
#Taking user input
number=get_Input()
#calling the Roll function to get the win count
win_count=sim_Roll(number)
#printing the result
print_Results(number, win_count)
#calling the craps simulator function
craps()