Advertisement
makispaiktis

Dice Game

Apr 29th, 2021 (edited)
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.09 KB | None | 0 0
  1. from random import randrange
  2.  
  3. # Function 1
  4. def checkMultiplier(multiplier):
  5.     if multiplier < 1 or multiplier > 3.5:
  6.         print(str(multiplier) + " is not an acceptable value.")
  7.         print("Try to insert a value between 1 and 3.5")
  8.         multiplier = float(input("Multiplier: "))
  9.         return checkMultiplier(multiplier)
  10.     else:
  11.         print(str(multiplier) + " is a nice choice of multiplier.")
  12.         return multiplier
  13.  
  14.  
  15. # FUNCTION 2
  16. def checkTarget(target, targetLimit):
  17.     if target < targetLimit:
  18.         print("Target points must be greater or equal than " + str(targetLimit))
  19.         target = int(input("Target points: "))
  20.         return checkTarget(target, targetLimit)
  21.     else:
  22.         print("Target = " + str(target))
  23.         return target
  24.  
  25. # FUNCTION 3
  26. def dice():
  27.     return randrange(1, 7), randrange(1, 7)
  28.  
  29.  
  30. # FUNCTION 4
  31. def show(list1, list2, multiplier):
  32.     if len(list1) != len(list2):
  33.         print("ERROR with lists' lengths")
  34.         return -1000
  35.     points1Real = 0
  36.     points1 = 0
  37.     points2Real = 0
  38.     points2 = 0
  39.     print("    THOMAS      |        USER")
  40.     print("Dice   Points   |     Dice   Points")
  41.     for i in range(len(list1)):
  42.         result = "  " + str(list1[i]) + "      " + str(list1[i]) + "      |"
  43.         result += "      " + str(list2[i]) + "      "
  44.         if list2[i] == 6:
  45.             points2 += multiplier * 6
  46.             result += str(points2)
  47.         else:
  48.             result += "0"
  49.         print(result)
  50.     # Print sums
  51.     print("-----------------------------------")
  52.     points1Real = sum(list1)
  53.     points1 = points1Real
  54.     points2Real = sum(list2)
  55.     print("  " + str(points1Real) + "     " + str(points1) + "     |      " + str(points2Real) + "     " + str(points2))
  56.     return points1, points2
  57.  
  58.  
  59. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  61. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  63.  
  64.  
  65. # MAIN FUNCTION
  66. # 1. Intro
  67. print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  68. print("Hello user, let's play a game with dice.")
  69. print("I will inform you about my strategy and how I am taking points in our game.")
  70. print("Then, I will let you know about your strategy and I will let you make a decision.")
  71. print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  72. print("!!!! THOMAS !!!! I will take so many points as the dice throw result.")
  73. print("!!!!! USER !!!!! Your dice throws will count when you throw 6, but with a multiplier.")
  74. print("You can maybe win without a multiplier if you throw many 6-s, but I want the game to be fair.")
  75. print("So, I want you to tell me the multiplier in 6-s. Be logic. Don't write down 4,5,6,...")
  76. print("Typical values of multiplier are close to 2. You can write 1.5 or 2.5 also or other numbers.")
  77. print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  78.  
  79. # 2. Choose multiplier value
  80. multiplier = float(input("Multiplier: "))
  81. multiplier = checkMultiplier(multiplier)
  82.  
  83. # 3. Let the game begin
  84. print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  85. print("Computer will create randomly numbers from 1 to 6 for THOMAS and USER")
  86. target = int(input("Target points: "))
  87. targetLimit = 40
  88. target = checkTarget(target, targetLimit)
  89. print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  90.  
  91. # 4. Let the game begin
  92. print("Multiplier = " + str(multiplier) + ", target = " + str(target))
  93. print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  94. print()
  95. print()
  96. thomasPoints = 0
  97. userPoints = 0
  98. thomasList = list()
  99. userList = list()
  100. counter = 0
  101. while thomasPoints < target and userPoints < target:
  102.     counter += 1
  103.     print("~~~~~~~~~~~~~ Round " + str(counter) + " ~~~~~~~~~~~~~")
  104.     thomasDice, userDice = dice()
  105.     print("THOMAS ----> " + str(thomasDice) + ", USER ----> " + str(userDice))
  106.     thomasList.append(thomasDice)
  107.     userList.append(userDice)
  108.     thomasPoints, userPoints = show(thomasList, userList, multiplier)
  109.  
  110. print()
  111. if thomasPoints == userPoints:
  112.     print("Game over after " + str(counter) + " rounds! It's a tie!")
  113. else:
  114.     result = "Game Over after " + str(counter) + " rounds! "
  115.     winner = ""
  116.     if thomasPoints > userPoints:
  117.         winner = "THOMAS"
  118.     else:
  119.         winner = "USER"
  120.     result +=  winner + " wins!"
  121.     print(result)
  122. print("Score:   THOMAS - USER : " + str(thomasPoints) + " - " + str(userPoints))
  123. print("Average THOMAS dice throw: " + str(round(thomasPoints / counter, 2)))
  124. print("Average  USER  dice throw: " + str(round(userPoints/ counter, 2)) + " = " + str(multiplier) + " * " + str(round(userPoints/counter/multiplier, 2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement