Advertisement
makispaiktis

Roulette 2 - Options "user" and "automatic"

Dec 18th, 2020 (edited)
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.50 KB | None | 0 0
  1. from random import randrange
  2. from matplotlib import pyplot as plt
  3.  
  4. # Function 1
  5. def doIContinue(answer):
  6.     flag = True
  7.     if answer.lower() == "no":
  8.         print("So, you want to stop. Nice game!")
  9.         flag = False
  10.     elif answer.lower() == "yes" or answer.lower() == "sure":
  11.         print("Wise choice, maybe not. Let's continue to the next bet!")
  12.         flag = True
  13.     else:
  14.         print("You did not answer neither 'yes' nor 'no'. I will suppose 'yes', next time be more careful.")
  15.         flag = True
  16.     print()
  17.     return flag
  18.  
  19. # Function 2
  20. def play(userStyle, betStyleComputerCode, LIMIT):
  21.     print()
  22.     print("Welcome to the Roulette game. Rules are simple. You can only bet in red color")
  23.     wins = 0
  24.     tries = 0
  25.     stats01 = []
  26.     bets = []
  27.     money = int(input("Money here: "))
  28.     while money < 0:
  29.         money = int(input("Money here: "))
  30.     initialMoney = money
  31.     answer = "yes"
  32.  
  33.     # I will simulate the simulation with another way: red will be if my number is between 1 and 18,
  34.     # black will be if my number is between 19 and 36 and "0" will be number 37
  35.     while money > 0 and answer != "no":
  36.         # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37.         # About answer: There are 2 possible ways: Either user plays, either PC on its own
  38.         # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39.         if userStyle == "user":
  40.             answer = input("Do we continue: ")
  41.         elif userStyle == "automatic":
  42.             answer = answerAutomatic(tries, LIMIT)
  43.         flag = doIContinue(answer)
  44.  
  45.  
  46.         if flag == True:  # That means we continue
  47.             tries += 1
  48.             if tries == 1:          # It will only play role in case of "automatic" choice
  49.                 standardBet = 0
  50.  
  51.             print("************************ Try ", tries, "************************")
  52.             if userStyle == "user":
  53.                 bet = int(input("Put your bet here: "))
  54.                 while bet > money:
  55.                     bet = int(input("Put your bet here: "))
  56.  
  57.             elif userStyle == "automatic" and tries == 1:
  58.                 bet = int(input("Put your bet here: "))
  59.                 while bet > money:
  60.                     bet = int(input("Put your bet here: "))
  61.                 standardBet = bet
  62.  
  63.             elif userStyle == "automatic" and tries != 1:
  64.                 # Here, we have to check the value of the 2nd parameter named "betStyleComputerCode"
  65.                 # If this variable is 1 ---> computer makes standard bets
  66.                 # If this variable is 2 ---> computer makes bets 1,2,4,8,....
  67.                 if betStyleComputerCode == 1:
  68.                     bet = standardBet
  69.             bets.append(bet)
  70.  
  71.             # Now, we have the bet that user put in. This bet will be substracted from sum = money
  72.             money = money - bet
  73.             # We will suppose that our user ALWAYS bets 'red', so we want the random generated number to be
  74.             # between 1 and 18
  75.             randomNumber = randrange(0, 37)
  76.             print("Bet =", bet, ", randomNumber =", randomNumber)
  77.             if randomNumber >= 1 and randomNumber <= 18:
  78.                 wins += 1
  79.                 money += 2 * bet
  80.                 stats01.append(1)
  81.                 print("You won, because 1 <=", randomNumber, "<= 18")
  82.                 print("Current money: ", money)
  83.             elif randomNumber >= 19 and randomNumber <= 36:
  84.                 stats01.append(0)
  85.                 print("You lost, because 19 <=", randomNumber, "<= 36")
  86.                 print("Current money: ", money)
  87.             else:
  88.                 stats01.append(0)
  89.                 print("You lost, because randomNumber = 0 ----> green color")
  90.                 print("Current money: ", money)
  91.             print("*******************************************************")
  92.             print()
  93.  
  94.     # Now, the basic process has finished. The remaining stuff is to show the stats in the user and the plot of his luck
  95.     print()
  96.     print("*******************************************************")
  97.     print("Here, I will present you the stats of your gameplay.")
  98.     print("Tries =", tries, ", wins =", wins, ", losses =", tries - wins)
  99.     winPercentage = wins / tries
  100.     winPercentage = round(10000 * winPercentage) / 10000
  101.     print("Win percentage  = ", 100 * winPercentage, "%")
  102.     print()
  103.     gain = money - initialMoney
  104.     gainPercentage = gain / initialMoney
  105.     gainPercentage = round(10000 * gainPercentage) / 10000
  106.     print("Initial money =", initialMoney, ", Final Money =", money, "(", 100 * gainPercentage, "%)")
  107.     # Plotting our data
  108.     plt.plot(stats01, 'ro')
  109.     plt.title("0 = Loss, 1 = Win")
  110.     plt.xlabel("Rounds")
  111.     plt.ylabel("Win/Loss")
  112.     plt.show()
  113.     plt.plot(bets, 'bx')
  114.     TITLE = "Bets value per round with initialMoney =" + str(initialMoney)
  115.     plt.title(TITLE)
  116.     plt.xlabel("Rounds")
  117.     plt.ylabel("Bet")
  118.     plt.show()
  119.  
  120.  
  121.  
  122. # Function 3
  123. def answerAutomatic(tries, LIMIT):
  124.     answer = "no"
  125.     if tries < LIMIT:
  126.         answer = "yes"
  127.     return answer
  128.  
  129.  
  130.  
  131. # **********************************************************************************************************
  132. # ********************************** MAIN FUNCTION *********************************************************
  133. # **********************************************************************************************************
  134. play("user", 0, 0)
  135. play("automatic", 1, 1000)
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement