Advertisement
Guest User

\<

a guest
Nov 5th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.16 KB | None | 0 0
  1. #NEA Task 2 // Katarina's two-player dice game. />Sebastian
  2. import csv, time, random, operator
  3.  
  4. def Menu():
  5. valid = False
  6. while not valid:
  7. choice = int(input("""
  8. --------------------------------------------------------------------------------
  9. Options:
  10. 1. Admin
  11. 2. Sign up
  12. 3. Play (2 player log in required)
  13. 4. View Top 5 scorers
  14.  
  15. 5. Exit
  16. --------------------------------------------------------------------------------
  17. What do you want to do? (1-5): """))
  18. if choice == 1:
  19. valid == True
  20. Admin()
  21. elif choice == 2:
  22. valid == True
  23. SignUp()
  24. elif choice == 3:
  25. valid == True
  26. diceRoll()
  27. elif choice == 4:
  28. valid == True
  29. Top5()
  30. elif choice == 5:
  31. valid == True
  32. exit()
  33. else:
  34. valid == False
  35. print("\nYour response was invalid, try again.\n")
  36.  
  37. def Admin():
  38. attempts = 3
  39. while attempts != 0:
  40. password = "1a2b3c"
  41. userpass = input("\nEnter the administrator password: ")
  42. if password == userpass:
  43. userfile = open("userfile.csv", "w")
  44. WriteLine = "Username" + "," + "Password" + "," + "Security" + "\n"
  45. userfile.write(WriteLine)
  46. userfile.close()
  47.  
  48. scorefile = open("scorefile.csv", "w")
  49. WriteLine = "Username" + "," + "Score" + "\n"
  50. scorefile.write(WriteLine)
  51. scorefile.close()
  52. print("\nCSV files created!")
  53. time.sleep(1)
  54. Menu()
  55. else:
  56. attempts = attempts - 1
  57. if attempts == 1:
  58. print("Wrong password!",attempts,"attempt left!")
  59. elif attempts == 0:
  60. print("Wrong password! No attempts left!")
  61. else:
  62. print("Wrong password!",attempts,"attempts left!")
  63. Menu()
  64.  
  65. def SignUp():
  66. userfile = open("userfile.csv", "a")
  67. sUser = input("\nChoose a username: ")
  68. sPass = input("Choose a password: ")
  69. sQues = int(input("""
  70. --------------------------------------------------------------------------------
  71. Security Questions:
  72. 1. What is/was your first pet's name?
  73. 2. Which town were you born in?
  74. 3. What is your mother's maiden name?
  75. 4. At what age did you buy your first car?
  76. 5. How many seasons of Spongebob have you watched?
  77. 6. What secondary school did you go to?
  78. --------------------------------------------------------------------------------
  79. Choose a Security Question (1-6): """))
  80. s1 = ("\nWhat is/was your first pet's name?")
  81. s2 = ("\nWhich town were you born in?")
  82. s3 = ("\nWhat is your mother's maiden name?")
  83. s4 = ("\nAt what age did you buy your first car?")
  84. s5 = ("\nHow many seasons of Spongebob have you watched?")
  85. s6 = ("\nWhat secondary school did you go to?")
  86. answered = False
  87. while answered == False:
  88. if sQues == 1:
  89. print(s1)
  90. sAns = input("Answer: ")
  91. answered = True
  92. if sQues == 2:
  93. print(s2)
  94. sAns = input("Answer: ")
  95. answered = True
  96. if sQues == 3:
  97. print(s3)
  98. sAns = input("Answer: ")
  99. answered = True
  100. if sQues == 4:
  101. print(s4)
  102. sAns = input("Answer: ")
  103. answered = True
  104. if sQues == 5:
  105. print(s5)
  106. sAns = input("Answer: ")
  107. answered = True
  108. if sQues == 6:
  109. print(s6)
  110. sAns = input("Answer: ")
  111. answered = True
  112. WriteLine = sUser + "," + sPass + "," + sAns + "\n"
  113. userfile.write(WriteLine)
  114. userfile.close()
  115. print("\nAccount Created!")
  116. Menu()
  117.  
  118. def LogIn():
  119. players = 0
  120. player1 = 0
  121. player2 = 0
  122. while players != 2:
  123. while player1 == 0:
  124. with open("userfile.csv") as csvfile:
  125. reader = csv.DictReader(csvfile)
  126. database = []
  127. for row in reader:
  128. database.append(dict(username=row["Username"],password=row["Password"],security=row["Security"]))
  129. loggedin = False
  130. while not loggedin:
  131. Username1 = input("\nEnter your username: ")
  132. Password1 = input("Enter your password: ")
  133. Security1 = input("Answer to your security question: ")
  134. for row in database:
  135. Username_File = row["username"]
  136. Password_File = row["password"]
  137. Security_File = row["security"]
  138. if (Username_File == Username1 and
  139. Password_File == Password1 and
  140. Security_File == Security1):
  141. loggedin = True
  142. print("\n", Username1, "logged in successfully!")
  143. players = players + 1
  144. player1 = str(Username1)
  145. print("Player 1 assgined to:", player1)
  146. if loggedin is not True:
  147. print("\nLog in failed! Make sure that your details are correct.")
  148.  
  149. while player2 == 0:
  150. with open("userfile.csv") as csvfile:
  151. reader = csv.DictReader(csvfile)
  152. database = []
  153. for row in reader:
  154. database.append(dict(username=row["Username"],password=row["Password"],security=row["Security"]))
  155. loggedin = False
  156. while not loggedin:
  157. Username2 = input("\nEnter your username: ")
  158. Password2 = input("Enter your password: ")
  159. Security2 = input("Answer to your security question: ")
  160. for row in database:
  161. Username_File = row["username"]
  162. Password_File = row["password"]
  163. Security_File = row["security"]
  164. if (Username_File == Username2 and
  165. Password_File == Password2 and
  166. Security_File == Security2):
  167. loggedin = True
  168. if Username1 != Username2:
  169. print("\n", Username2, "logged in successfully!")
  170. players = players + 1
  171. player2 = str(Username2)
  172. print("Player 2 assigned to:", player2,"\n")
  173. else:
  174. print("\nYou cannot use the same user for both players!")
  175. if loggedin is not True:
  176. print("\nLog in failed! Make sure that your details are correct.")
  177. return player1, player2
  178.  
  179. def diceRoll():
  180. player1, player2 = LogIn()
  181. turn = 0
  182. rally = 1
  183. score = 0
  184. total = 0
  185. score2 = 0
  186. total2 = 0
  187.  
  188. while 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. while turn == 5 and score == score2:
  264. print("\nDraw! Play until there is a winner!")
  265. time.sleep(0.5)
  266. throw1 = random.randint(1,6)
  267. print("\n",player1,"rolls a die and gets:",throw1)
  268. time.sleep(1)
  269. score = score + throw1
  270. throw2 = random.randint(1,6)
  271. print("",player2,"rolls a die and gets:",throw2)
  272. score2 = score2 + throw2
  273.  
  274. if turn == 5:
  275. time.sleep(2)
  276. print("\n------ Final scores ------")
  277. print(player1, score)
  278. print(player2, score2)
  279. print("--------------------------")
  280. if score > score2:
  281. print("Winner:",player1)
  282. else:
  283. print("Winner:",player2)
  284. print("\nScores written to a CSV file!")
  285. time.sleep(2)
  286.  
  287. with open('scorefile.csv', 'a', newline='') as file:
  288. writer = csv.writer(file)
  289. writer.writerow((player1, score))
  290.  
  291. with open('scorefile.csv', 'a', newline='') as file:
  292. writer = csv.writer(file)
  293. writer.writerow((player2, score2))
  294.  
  295. data = csv.reader(open('scorefile.csv'),delimiter=',')
  296. sortedlist = sorted(data, key=operator.itemgetter(1), reverse=True)
  297. with open("top5file.csv", "w") as f:
  298. fileWriter = csv.writer(f, delimiter=',')
  299. for row in sortedlist:
  300. fileWriter.writerow(row)
  301.  
  302. def Top5():
  303. with open('top5file.csv') as csvfile:
  304. reader = csv.DictReader(csvfile)
  305. print("""
  306. = Leaderboard =
  307. ---------------""")
  308. for i,row in enumerate(reader):
  309. print(row['Username'], row['Score'])
  310. if(i >= 4):
  311. break
  312. print("---------------")
  313. Menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement