Guest User

Untitled

a guest
Nov 15th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. import csv, time, random, operator
  2.  
  3. def Menu():
  4. valid = False
  5. while not valid:
  6. choice = int(input("""
  7. --------------------------------------------------------------------------------
  8. Options:
  9. 1. Admin
  10. 2. Sign up
  11. 3. Play (2 player log in required)
  12. 4. View Top 5 scorers
  13.  
  14. 5. Exit
  15. --------------------------------------------------------------------------------
  16. What do you want to do? (1-5): """))
  17. if choice == 1:
  18. valid == True
  19. Admin()
  20. elif choice == 2:
  21. valid == True
  22. SignUp()
  23. elif choice == 3:
  24. valid == True
  25. diceRoll()
  26. elif choice == 4:
  27. valid == True
  28. Top5()
  29. elif choice == 5:
  30. valid == True
  31. exit()
  32. else:
  33. valid == False
  34. print("\nYour response was invalid, try again.\n")
  35.  
  36. def Admin():
  37. attempts = 3
  38. while attempts != 0:
  39. password = "sumeet"
  40. userpass = input("\nEnter the administrator password: ")
  41. if password == userpass:
  42. userfile = open("userfile.csv", "w")
  43. WriteLine = "Username" + "," + "Password" + "," + "Security" + "\n"
  44. userfile.write(WriteLine)
  45. userfile.close()
  46.  
  47. scorefile = open("scorefile.csv", "w")
  48. WriteLine = "Username" + "," + "Score" + "\n"
  49. scorefile.write(WriteLine)
  50. scorefile.close()
  51. print("\nCSV files created!")
  52. time.sleep(1)
  53. Menu()
  54. else:
  55. attempts = attempts - 1
  56. if attempts == 1:
  57. print("Wrong password!",attempts,"attempt left!")
  58. elif attempts == 0:
  59. print("Wrong password! No attempts left!")
  60. else:
  61. print("Wrong password!",attempts,"attempts left!")
  62. Menu()
  63.  
  64. def SignUp():
  65. userfile = open("userfile.csv", "a")
  66. sUser = input("\nChoose a username: ")
  67. sPass = input("Choose a password: ")
  68. sQues = int(input("""
  69. --------------------------------------------------------------------------------
  70. Security Questions:
  71. 1. What is/was your first pet's name?
  72. 2. Which town were you born in?
  73. 3. What is your mother's maiden name?
  74. 4. At what age did you buy your first car?
  75. 5. How many seasons of Spongebob have you watched?
  76. 6. What secondary school did you go to?
  77. --------------------------------------------------------------------------------
  78. Choose a Security Question (1-6): """))
  79. s1 = ("\nWhat is/was your first pet's name?")
  80. s2 = ("\nWhich town were you born in?")
  81. s3 = ("\nWhat is your mother's maiden name?")
  82. s4 = ("\nAt what age did you buy your first car?")
  83. s5 = ("\nHow many seasons of Spongebob have you watched?")
  84. s6 = ("\nWhat secondary school did you go to?")
  85. answered = False
  86. while answered == False:
  87. if sQues == 1:
  88. print(s1)
  89. sAns = input("Answer: ")
  90. answered = True
  91. if sQues == 2:
  92. print(s2)
  93. sAns = input("Answer: ")
  94. answered = True
  95. if sQues == 3:
  96. print(s3)
  97. sAns = input("Answer: ")
  98. answered = True
  99. if sQues == 4:
  100. print(s4)
  101. sAns = input("Answer: ")
  102. answered = True
  103. if sQues == 5:
  104. print(s5)
  105. sAns = input("Answer: ")
  106. answered = True
  107. if sQues == 6:
  108. print(s6)
  109. sAns = input("Answer: ")
  110. answered = True
  111. WriteLine = sUser + "," + sPass + "," + sAns + "\n"
  112. userfile.write(WriteLine)
  113. userfile.close()
  114. print("\nAccount Created!")
  115. Menu()
  116.  
  117. def LogIn():
  118. players = 0
  119. player1 = 0
  120. player2 = 0
  121. while players != 2:
  122. while player1 == 0:
  123. with open("userfile.csv") as csvfile:
  124. reader = csv.DictReader(csvfile)
  125. database = []
  126. for row in reader:
  127. database.append(dict(username=row["Username"],password=row["Password"],security=row["Security"]))
  128. loggedin = False
  129. while not loggedin:
  130. Username1 = input("\nEnter your username: ")
  131. Password1 = input("Enter your password: ")
  132. Security1 = input("Answer to your security question: ")
  133. for row in database:
  134. Username_File = row["username"]
  135. Password_File = row["password"]
  136. Security_File = row["security"]
  137. if (Username_File == Username1 and
  138. Password_File == Password1 and
  139. Security_File == Security1):
  140. loggedin = True
  141. print("\n", Username1, "logged in successfully!")
  142. players = players + 1
  143. player1 = str(Username1)
  144. print("Player 1 assgined to:", player1)
  145. if loggedin is not True:
  146. print("\nLog in failed! Make sure that your details are correct.")
  147.  
  148. while player2 == 0:
  149. with open("userfile.csv") as csvfile:
  150. reader = csv.DictReader(csvfile)
  151. database = []
  152. for row in reader:
  153. database.append(dict(username=row["Username"],password=row["Password"],security=row["Security"]))
  154. loggedin = False
  155. while not loggedin:
  156. Username2 = input("\nEnter your username: ")
  157. Password2 = input("Enter your password: ")
  158. Security2 = input("Answer to your security question: ")
  159. for row in database:
  160. Username_File = row["username"]
  161. Password_File = row["password"]
  162. Security_File = row["security"]
  163. if (Username_File == Username2 and
  164. Password_File == Password2 and
  165. Security_File == Security2):
  166. loggedin = True
  167. if Username1 != Username2:
  168. print("\n", Username2, "logged in successfully!")
  169. players = players + 1
  170. player2 = str(Username2)
  171. print("Player 2 assigned to:", player2,"\n")
  172. else:
  173. print("\nYou cannot use the same user for both players!")
  174. if loggedin is not True:
  175. print("\nLog in failed! Make sure that your details are correct.")
  176. return player1, player2
  177.  
  178. def diceRoll():
  179. player1, player2 = LogIn()
  180. turn = 0
  181. rally = 1
  182. score = 0
  183. total = 0
  184. score2 = 0
  185. total2 = 0
  186. again = "Y"
  187.  
  188. while again == "Y" and turn != 5:
  189. print("\n------ Rally",rally,"------")
  190. print("\n",player1,"'s turn!")
  191. throw1 = random.randint(1,6)
  192. throw2 = random.randint(1,6)
  193. time.sleep(1)
  194. print("Die 1 =",throw1)
  195. time.sleep(1)
  196. print("Die 2 =",throw2)
  197. total = throw1 + throw2
  198. score = score + total
  199. time.sleep(0.5)
  200. print("Total for this round:",total)
  201. if total % 2 == 0:
  202. time.sleep(0.5)
  203. print("(=) Even total, +10 to score!")
  204. score = score + 10
  205. else:
  206. time.sleep(0.5)
  207. print("(-) Odd total, -5 from score!")
  208. score = score - 5
  209. if score < 0:
  210. time.sleep(0.5)
  211. print("(/) Score is reset to 0 due to negative total!")
  212. score = 0
  213. if throw1 == throw2:
  214. time.sleep(0.5)
  215. print("(!) Rolled a double! You get to roll an extra die!")
  216. throw3 = random.randint(1,6)
  217. time.sleep(0.5)
  218. print("(!) Die 3 =",throw3)
  219. time.sleep(0.5)
  220. print("(!)",throw3,"added to your score!")
  221. score = score + throw3
  222. time.sleep(0.3)
  223. print("Score:",score)
  224. time.sleep(1.5)
  225.  
  226. print("\n",player2,"'s turn!")
  227. throw1 = random.randint(1,6)
  228. throw2 = random.randint(1,6)
  229. time.sleep(1)
  230. print("Die 1 =",throw1)
  231. time.sleep(1)
  232. print("Die 2 =",throw2)
  233. total2 = throw1 + throw2
  234. score2 = score2 + total2
  235. time.sleep(0.5)
  236. print("Total for this round:",total2)
  237. if total2 % 2 == 0:
  238. time.sleep(0.5)
  239. print("(=) Even total, +10 to score!")
  240. score2 = score2 + 10
  241. else:
  242. time.sleep(0.5)
  243. print("(-) Odd total, -5 from score!")
  244. score2 = score2 - 5
  245. if score2 < 0:
  246. time.sleep(0.5)
  247. print("(/) Score is reset to 0 due to negative total!")
  248. score2 = 0
  249. if throw1 == throw2:
  250. time.sleep(0.5)
  251. print("(!) Rolled a double! You get to roll an extra die!")
  252. throw3 = random.randint(1,6)
  253. time.sleep(0.5)
  254. print("(!) Die 3 =",throw3)
  255. time.sleep(0.5)
  256. print("(!)",throw3,"added to your score!")
  257. score2 = score2 + throw3
  258. time.sleep(0.3)
  259. print("Score:",score2)
  260. turn = turn + 1
  261. rally = rally + 1
  262. time.sleep(1.5)
  263.  
  264. again = input("\nWant to go again? (Y/N): ").upper()
  265. while turn == 5 and score == score2:
  266. print("\nDraw! Play until there is a winner!")
  267. time.sleep(0.5)
  268. throw1 = random.randint(1,6)
  269. print("\n",player1,"rolls a die and gets:",throw1)
  270. time.sleep(1)
  271. score = score + throw1
  272. throw2 = random.randint(1,6)
  273. print("",player2,"rolls a die and gets:",throw2)
  274. score2 = score2 + throw2
  275.  
  276. if turn == 5 and again == "N":
  277. time.sleep(2)
  278. print("\n------ Final scores ------")
  279. print(player1, score)
  280. print(player2, score2)
  281. print("--------------------------")
  282. if score > score2:
  283. print("Winner:",player1)
  284. else:
  285. print("Winner:",player2)
  286. print("\nScores written to a CSV file!")
  287. time.sleep(2)
  288.  
  289. if turn == 5 and again == "Y":
  290. print("\nYou have already had 5 turns! Log in to play again.")
  291. time.sleep(2)
  292. print("\n------ Final scores ------")
  293. print(player1, score)
  294. print(player2, score2)
  295. print("--------------------------")
  296. if score > score2:
  297. print("Winner:",player1)
  298. else:
  299. print("Winner:",player2)
  300. print("\nScores written to a CSV file!")
  301. time.sleep(2)
  302.  
  303. with open('scorefile.csv', 'a', newline='') as file:
  304. writer = csv.writer(file)
  305. writer.writerow((player1, score))
  306.  
  307. with open('scorefile.csv', 'a', newline='') as file:
  308. writer = csv.writer(file)
  309. writer.writerow((player2, score2))
  310.  
  311. data = csv.reader(open('scorefile.csv'),delimiter=',')
  312. sortedlist = sorted(data, key=operator.itemgetter(1), reverse=True)
  313. with open("top5file.csv", "w") as f:
  314. fileWriter = csv.writer(f, delimiter=',')
  315. for row in sortedlist:
  316. fileWriter.writerow(row)
  317.  
  318. def Top5():
  319. with open('top5file.csv') as csvfile:
  320. reader = csv.DictReader(csvfile)
  321. print("\nCurrent Top 5:")
  322. for i,row in enumerate(reader):
  323. print(row['Username'], row['Score'])
  324. if(i >= 4):
  325. break
  326. Menu()
Add Comment
Please, Sign In to add comment