Advertisement
VuK39

Dice gamecscs

Nov 29th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import os.path, time, random
  2. from datetime import datetime
  3. from time import sleep
  4. now = datetime.now()
  5. zero = ":"
  6. if len(str(now.minute)) == 1:
  7. zero = (":"+str(0))
  8.  
  9. def welcome():
  10. print("Hi! Welcome to the dice game, the current time is %s%s%s and the date is %s/%s/%s\n" % (now.hour, zero, now.minute, now.day, now.month, now.year),".")
  11. option = str(input("Please choose what option you would like to select:\n1. Register\n2. Login\n3. Quit\n"))
  12. if option == "1": register()
  13. elif option == "2": login()
  14. else: quit
  15.  
  16. def register():
  17. username = str(input("Please create a username:\n"))
  18. if not os.path.isfile(username+".txt"):
  19. password = str(input("Please input a password:\n"))
  20. confirm_password = str(input("Please confirm your password:\n"))
  21. if password == confirm_password:
  22. user_file = open(username+".txt", "w")
  23. user_file.write(password)
  24. user_file.close()
  25. print("You have now successfully registered and you are ready to play!")
  26. welcome()
  27. else:
  28. print("Failed to confirm password, please try again")
  29. register()
  30. else:
  31. print(username,"already exists, please try using a different one.")
  32. register()
  33.  
  34. def login():
  35. username = str(input("Please input your username:\n"))
  36. if os.path.isfile(username+".txt"):
  37. input_password = str(input("Please input your password:\n"))
  38. user_file = open(username+".txt", "r")
  39. read_file = user_file.readlines()
  40. password = read_file[0]
  41. if password == input_password:
  42. print("You have now logged in as",username)
  43. else:
  44. print("Sorry but that is the wrong password, please try again")
  45. login()
  46. else:
  47. print("There is no Username called:",username)
  48.  
  49. welcome()
  50.  
  51. person1_name = input("Player 1, what is your name: ")
  52. person2_name = input("Player 2, what is your name: ")
  53.  
  54. names = [person1_name, person2_name]
  55. totals = [0, 0]
  56. rolls = 1
  57. maxrolls = 5
  58.  
  59. def roll():
  60.  
  61. random.seed(datetime.now())
  62. player = 0
  63. while player <= 1:
  64. number1, number2 = random.randint(1,6), random.randint(1,6)
  65. print(names[player], "- 1st roll =", number1, "and 2nd roll =", number2)
  66.  
  67. total = number1 + number2
  68.  
  69. if total % 2 == 0:
  70. total += 10
  71.  
  72. else:
  73. if total < 4:
  74. total = 0
  75. else:
  76. total -= 5
  77.  
  78. print(names[player]," has rolled", total, "points.")
  79.  
  80. totals[player] += total
  81. player += 1
  82. sleep(2)
  83.  
  84. def checkscores():
  85. print("\nFinal results: ", names[0], totals[0],"-",totals[1] , person2_name)
  86. if totals[0] > totals[1]:
  87. print(person1_name," is the winner! ")
  88. elif totals[0] == totals[1]:
  89. print("\nIt's now sudden death!")
  90. roll()
  91. checkscores()
  92. else:
  93. print(person2_name," is the winner! ")
  94.  
  95. while rolls <= maxrolls:
  96. print("\nRoll", rolls)
  97. rolls += 1
  98. roll()
  99.  
  100. checkscores()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement