Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randrange
- from matplotlib import pyplot as plt
- # Function 1
- def doIContinue(answer):
- flag = True
- if answer.lower() == "no":
- print("So, you want to stop. Nice game!")
- flag = False
- elif answer.lower() == "yes" or answer.lower() == "sure":
- print("Wise choice, maybe not. Let's continue to the next bet!")
- flag = True
- else:
- print("You did not answer neither 'yes' nor 'no'. I will suppose 'yes', next time be more careful.")
- flag = True
- print()
- return flag
- # Function 2
- def play(userStyle, betStyleComputerCode, LIMIT):
- print()
- print("Welcome to the Roulette game. Rules are simple. You can only bet in red color")
- wins = 0
- tries = 0
- stats01 = []
- bets = []
- money = int(input("Money here: "))
- while money < 0:
- money = int(input("Money here: "))
- initialMoney = money
- answer = "yes"
- # I will simulate the simulation with another way: red will be if my number is between 1 and 18,
- # black will be if my number is between 19 and 36 and "0" will be number 37
- while money > 0 and answer != "no":
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # About answer: There are 2 possible ways: Either user plays, either PC on its own
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- if userStyle == "user":
- answer = input("Do we continue: ")
- elif userStyle == "automatic":
- answer = answerAutomatic(tries, LIMIT)
- flag = doIContinue(answer)
- if flag == True: # That means we continue
- tries += 1
- if tries == 1: # It will only play role in case of "automatic" choice
- standardBet = 0
- print("************************ Try ", tries, "************************")
- if userStyle == "user":
- bet = int(input("Put your bet here: "))
- while bet > money:
- bet = int(input("Put your bet here: "))
- elif userStyle == "automatic" and tries == 1:
- bet = int(input("Put your bet here: "))
- while bet > money:
- bet = int(input("Put your bet here: "))
- standardBet = bet
- elif userStyle == "automatic" and tries != 1:
- # Here, we have to check the value of the 2nd parameter named "betStyleComputerCode"
- # If this variable is 1 ---> computer makes standard bets
- # If this variable is 2 ---> computer makes bets 1,2,4,8,....
- if betStyleComputerCode == 1:
- bet = standardBet
- bets.append(bet)
- # Now, we have the bet that user put in. This bet will be substracted from sum = money
- money = money - bet
- # We will suppose that our user ALWAYS bets 'red', so we want the random generated number to be
- # between 1 and 18
- randomNumber = randrange(0, 37)
- print("Bet =", bet, ", randomNumber =", randomNumber)
- if randomNumber >= 1 and randomNumber <= 18:
- wins += 1
- money += 2 * bet
- stats01.append(1)
- print("You won, because 1 <=", randomNumber, "<= 18")
- print("Current money: ", money)
- elif randomNumber >= 19 and randomNumber <= 36:
- stats01.append(0)
- print("You lost, because 19 <=", randomNumber, "<= 36")
- print("Current money: ", money)
- else:
- stats01.append(0)
- print("You lost, because randomNumber = 0 ----> green color")
- print("Current money: ", money)
- print("*******************************************************")
- print()
- # Now, the basic process has finished. The remaining stuff is to show the stats in the user and the plot of his luck
- print()
- print("*******************************************************")
- print("Here, I will present you the stats of your gameplay.")
- print("Tries =", tries, ", wins =", wins, ", losses =", tries - wins)
- winPercentage = wins / tries
- winPercentage = round(10000 * winPercentage) / 10000
- print("Win percentage = ", 100 * winPercentage, "%")
- print()
- gain = money - initialMoney
- gainPercentage = gain / initialMoney
- gainPercentage = round(10000 * gainPercentage) / 10000
- print("Initial money =", initialMoney, ", Final Money =", money, "(", 100 * gainPercentage, "%)")
- # Plotting our data
- plt.plot(stats01, 'ro')
- plt.title("0 = Loss, 1 = Win")
- plt.xlabel("Rounds")
- plt.ylabel("Win/Loss")
- plt.show()
- plt.plot(bets, 'bx')
- TITLE = "Bets value per round with initialMoney =" + str(initialMoney)
- plt.title(TITLE)
- plt.xlabel("Rounds")
- plt.ylabel("Bet")
- plt.show()
- # Function 3
- def answerAutomatic(tries, LIMIT):
- answer = "no"
- if tries < LIMIT:
- answer = "yes"
- return answer
- # **********************************************************************************************************
- # ********************************** MAIN FUNCTION *********************************************************
- # **********************************************************************************************************
- play("user", 0, 0)
- play("automatic", 1, 1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement