
import sys, random
import numpy as np
import matplotlib.pyplot as plt

def gamble(p1, g1_amt, g2_amt):
########################################################
#  YOUR CODE GOES HERE:
#    Recode function to execute gamblers ruin
#    game or copy in appropriate code from first
#    program.
########################################################
    return g1_seq, g2_seq, steps
#
#   Enter data from keyboard:
#        p, g1, g2, num_sim
#     p = probability that g1 wins. Must be >0 and <1.
#     g1,g2 = initial amounts for g1 and g2. Must be >0.
#     num_sim = number of simulations of game to execute             
#
##########################################################
#  YOUR CODE GOES HERE:
#    Add commands to enter each input and check conditions,
#    If  condtion not meet, print message and exit program
#    gracefully.  
############################################################
#   Set up variables to accumulate number of steps to ruin 
#     for each simulation and total number of wins by g1
ruin_steps = np.zeros(num_sim)
g1_wins = 0
#
#   Execute main loop 'num_sim'  times
#
#############################################################
#   YOUR CODE GOES HERE  
#     Hints:  
#         1) Use for loop to execute num_sim simulations 
#         2) Accumulate result of each simulation   
#         3) g1 wins if end of g2_walk = 0 ( g2_walk[-1] == 0) 
#############################################################
#   Print out key statistics and plot histogram of number of steps to ruin 
#
print ('Statistics for gamblers ruin with %d simulations:' % num_sim)
print ('   Prob g1 = %f , g1 bankroll = %d ,  g2 bankroll = %d' % (p,g1,g2)) 
print ('   Gambler1 wins  %f  \% of time' % round (100.0*(g1_wins/float(num_sim)))) 
print ('   Average number of steps to ruin is: %f ' %  int(np.mean(ruin_steps)))
print ('   Largest number of steps to ruin is: %f ' %  int(np.max(ruin_steps))) 
#  Print histogram of results
plt.hist(ruin_steps, bins=50, color='blue')      
plt.title(' Gambler\'s ruin: p='+ str(p)+ ', g1 = \$'+ str(g1)+ ', g2 = \$'+ str(g2))
plt.grid(True)  
plt.show()


