Guest User

Untitled

a guest
Nov 24th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.10 KB | None | 0 0
  1. from random import randint
  2. from time import sleep
  3. import time
  4. import sys
  5. import random
  6. import operator
  7.  
  8. total_score2 = 0
  9. total_score1 = 0
  10. rounds = 0
  11. playerOnePoints = 0
  12. playerTwoPoints = 0
  13. print("*****************Welcome To The DICE Game*******************")
  14. print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user and enter 's' to display scores")
  15. ens=input("")
  16. while ens != ("e") and ens != ("n") and ens != ("s"): # if anything else but these characters are entered it will loop until it is correct
  17. print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user and enter 's' to display scores")
  18. ens = input()
  19. if ens == "s":
  20. s = open("scores.txt","r")
  21.  
  22. file_content = s.read().splitlines()
  23.  
  24. users_points = {i.split()[0]: int(i.split()[2]) for i in file_content}
  25. best_player = max(users_points.items(), key=operator.itemgetter(1))[0]
  26. print("LeaderBoard: ")
  27. print("n")
  28. print('player with maximum points is {}, this player has {} points'.format(best_player, users_points[best_player]))
  29. best_players = sorted(users_points, key=users_points.get, reverse=True)
  30. for bp in best_players:
  31. print('{} has {} points'.format(bp, users_points[bp])) # This prints all players scores
  32.  
  33. if ens == "n":
  34. username=input("Please enter appropiate username: ")
  35. password1=input("Please enter password: ")
  36. password2=input("Please re-enter password: ")
  37. if password1 == password2: # checking if both passwords entered are the same
  38. print("your account has been successfully been made Thankyou")
  39. file = open("accountfile.txt","a")
  40. file.write("username: ")
  41. file.write(username)
  42. file.write(" ")
  43. file.write("password: ")
  44. file.write(password2)
  45. file.write("n")
  46. file.close()
  47. print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user")
  48. ens=input(" ")
  49. if password1 != password2: # if passwords entered are not the same will loop until they are correctly entered
  50. correctPassword=(password1)
  51. while True:
  52. password=input('Enter password again ')
  53. if password == correctPassword:
  54. print('Correct password has been entered')
  55. f = open ("accountfile.txt","a+")
  56. f.write("username: ")
  57. f.write(username)
  58. f.write(" ")
  59. f.write("password: ")
  60. f.write(correctPassword)
  61. f.write("n")
  62. f.close()
  63. print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user")
  64. en=input(" ")
  65. print('Incorrect password ')
  66.  
  67. if ens == "e":
  68. counter = 0
  69. check_failed = True
  70. while check_failed:
  71. print("Could player 1 enter their username and password")
  72. username1=input("Please enter your username ")
  73. password=input("Please enter your password ")
  74. with open("accountfile.txt","r") as username_finder:
  75. for line in username_finder:
  76. if ("username: " + username1 + " password: " + password) == line.strip():
  77. print("you are logged in")
  78. check_failed = False
  79. counter = 0
  80. check_failed = True
  81. while check_failed:
  82. print("Could player 2 enter their username and password")
  83. username2=input("Please enter your username ")
  84. password=input("Please enter your password ")
  85. with open("accountfile.txt","r") as username_finder:
  86. for line in username_finder:
  87. if ("username: " + username2 + " password: " + password) == line.strip():
  88. print("you are logged in")
  89. check_failed = False
  90. time.sleep(1)
  91. print("Welcome to the dice game")
  92. time.sleep(1)
  93. while rounds < 5:
  94. total_score2 = total_score2 + playerTwoPoints
  95. total_score1 = total_score1 + playerOnePoints
  96. rounds = rounds + 1
  97. number = random.randint(1,6)
  98. number2 = random.randint(1,6)
  99. playerOnePoints = number + number2
  100. print("Round",rounds)
  101. print("-------------------------------------------")
  102. print("Player 1's turn Type 'roll' to roll the dice")
  103. userOneInput = input(">>> ")
  104. if userOneInput == "roll":
  105. time.sleep(1)
  106. print("Player 1's first roll is", number)
  107. print("Player 1's second roll Type 'roll' to roll the dice")
  108. userOneInput = input(">>> ")
  109. if userOneInput == "roll":
  110. time.sleep(1)
  111. print("player 1's second roll is", number2)
  112. if playerOnePoints <= 0:
  113. playerOnePoints = 0
  114. if playerOnePoints % 2 == 0:
  115. playerOnePoints = playerOnePoints + 10
  116. print("Player 1's total is even so + 10 points")
  117. print("-------------------------------------------")
  118. print("Player 1 has",playerOnePoints, "points")
  119. else:
  120. playerOnePoints = playerOnePoints - 5
  121. print("player 1's total is odd so -5 points")
  122. print("-------------------------------------------")
  123. print("Player 1 has",playerOnePoints, "points")
  124. if playerOnePoints <= 0:
  125. playerOnePoints = 0
  126. number = random.randint(1,6)
  127. number2 = random.randint(1,6)
  128. playerTwoPoints = number + number2
  129. print("-------------------------------------------")
  130. print("Player 2's turn Type 'roll' to roll the dice")
  131. userTwoInput = input(">>> ")
  132. if userTwoInput == "roll":
  133. time.sleep(1)
  134. print("Player 2's first roll is", number)
  135. print("Player 2's second roll Type 'roll' to roll the dice")
  136. userTwoInput = input(">>> ")
  137. if userTwoInput == "roll":
  138. time.sleep(1)
  139. print("player 2's second roll is", number2)
  140. if playerTwoPoints <= 0:
  141. playerTwoPoints = 0
  142. if playerTwoPoints % 2 == 0:
  143. playerTwoPoints = playerTwoPoints + 10
  144. print("Player 2's total is even so + 10 points")
  145. print("-------------------------------------------")
  146. print("Player 2 has",playerTwoPoints, "points")
  147. else:
  148. playerTwoPoints = playerTwoPoints - 5
  149. print("player 2's total is odd so -5 points")
  150. print("-------------------------------------------")
  151. print("Player 2 has",playerTwoPoints, "points")
  152. print("-------------------------------------------")
  153.  
  154. print("Total score for player 1 is", total_score1)
  155. print("-------------------------------------------")
  156. print("Total score for player 2 is", total_score2)
  157. print("-------------------------------------------")
  158.  
  159. if total_score1 > total_score2:
  160. print("Player 1 Wins!")
  161. file = open("scores.txt","a")
  162. file.write(username1)
  163. file.write(" has ")
  164. file.write(str(total_score1))
  165. file.write(" points")
  166. file.write("n")
  167. file.close()
  168. sys.exit()
  169. if total_score2 > total_score1:
  170. print("Player 2 Wins!")
  171. file = open("scores.txt","a")
  172. file.write(username2)
  173. file.write(" has ")
  174. file.write(str(total_score2))
  175. file.write(" points")
  176. file.write("n")
  177. file.close()
  178. sys.exit()
  179. if total_score1 == total_score2:
  180. print("Its a draw!")
  181. print("So both players will have to roll one more dice")
  182. time.sleep(2)
  183. print("-------------------------------------------")
  184. print("Player 1's turn Type 'roll' to roll the dice")
  185. userOneInput = input(">>> ")
  186. if userOneInput == "roll":
  187. time.sleep(1)
  188. print("Player 1's first roll is", number)
  189. print("Player 1's second roll Type 'roll' to roll the dice")
  190. userOneInput = input(">>> ")
  191. if userOneInput == "roll":
  192. time.sleep(1)
  193. print("player 1's second roll is", number2)
  194. if playerOnePoints % 2 == 0:
  195. playerOnePoints = playerOnePoints + 10
  196. print("Player 1's total is even so + 10 points")
  197. print("-------------------------------------------")
  198. print("Player 1 has",playerOnePoints, "points")
  199. else:
  200. playerOnePoints = playerOnePoints - 5
  201. print("player 1's total is odd so -5 points")
  202. print("-------------------------------------------")
  203. print("Player 1 has",playerOnePoints, "points")
  204. number = random.randint(1,6)
  205. number2 = random.randint(1,6)
  206. playerTwoPoints = number + number2
  207. print("-------------------------------------------")
  208. print("Player 2's turn Type 'roll' to roll the dice")
  209. userTwoInput = input(">>> ")
  210. if userTwoInput == "roll":
  211. time.sleep(1)
  212. print("Player 2's first roll is", number)
  213. print("Player 2's second roll Type 'roll' to roll the dice")
  214. userTwoInput = input(">>> ")
  215. if userTwoInput == "roll":
  216. time.sleep(1)
  217. print("player 2's second roll is", number2)
  218. if playerTwoPoints % 2 == 0:
  219. playerTwoPoints = playerTwoPoints + 10
  220. print("Player 2's total is even so + 10 points")
  221. print("-------------------------------------------")
  222. print("Player 2 has",playerTwoPoints, "points")
  223. else:
  224. playerTwoPoints = playerTwoPoints - 5
  225. print("player 2's total is odd so -5 points")
  226. print("-------------------------------------------")
  227. print("Player 2 has",playerTwoPoints, "points")
  228. print("-------------------------------------------")
  229. if total_score1 > total_score2:
  230. print("Player 1 Wins!")
  231. file = open("scores.txt","a")
  232. file.write(username1)
  233. file.write(" has ")
  234. file.write(str(total_score1))
  235. file.write(" points")
  236. file.write("n")
  237. file.close()
  238. if total_score2 > total_score1:
  239. print("Player 2 Wins!")
  240. file = open("scores.txt","a")
  241. file.write(username2)
  242. file.write(" has ")
  243. file.write(str(total_score2))
  244. file.write(" points")
  245. file.write("n")
  246. file.close()
  247. sys.exit()
  248. else:
  249. print("Sorry, this username or password does not exist please try again")
  250. counter = counter + 1
  251. if counter == 3:
  252. print("----------------------------------------------------")
  253. print("You have been locked out please restart to try again")
  254. sys.exit()
  255.  
  256. else:
  257. print("Sorry, this username or password does not exist please try again")
  258. counter = counter + 1
  259. if counter == 3:
  260. print("----------------------------------------------------")
  261. print("You have been locked out please restart to try again")
  262. sys.exit()
Add Comment
Please, Sign In to add comment