Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.43 KB | None | 0 0
  1. #### TODO: CREATE OPTION TO SHOW AVAILABLE USERS
  2.  
  3. #### TODO: WHEN A PASSWORD OR USERNAME ISN'T ACCEPTED FOR THE GAME, SHOW A MESSAGE INSTEAD OF JUST GOING BACK TO THE MENU
  4.  
  5. #### TODO: WHEN YOU HAVE TWO USERS, (E.G. "HI" AND "LOL" WITH PASSWORDS "2" AND "2"), AND YOU ENTER THE SAME NAME FOR BOTH USERS, (E.G. "HI" AND "HI"), BUT THE RIGHT
  6. #### PASSWORD, IT WILL WORK BECAUSE IT ONLY LOOKS FOR A PASSWORD AND USERNAME, NOT IF THEY MATCH TOGETHER
  7.  
  8.  
  9.  
  10. ###### Basics ######
  11.  
  12. import random
  13.  
  14. from pathlib import Path
  15.  
  16. player_one = ""
  17. player_two = ""
  18.  
  19. player_one_score = 0
  20. player_two_score = 0
  21.  
  22. def clear():
  23. print("\n" * 400)
  24.  
  25. def reset():
  26. player_one = ""
  27. player_two = ""
  28.  
  29. player_one_score = 0
  30. player_two_score = 0
  31.  
  32.  
  33. ###### Main Game ######
  34.  
  35. ## Main Game ##
  36. def mg_start_game():
  37. # Check Usernames Exist
  38. if not Path("usernames.txt").exists():
  39. if len(open("usernames.txt").readlines()) < 2:
  40. print("\nThere are not two users!")
  41.  
  42. input()
  43.  
  44. return
  45.  
  46. global player_one, player_two, player_one_score, player_two_score # Globalise variables
  47.  
  48. # Access Accounts
  49. clear()
  50.  
  51. uas_check_account("one")
  52.  
  53. clear()
  54.  
  55. uas_check_account("two")
  56.  
  57. if player_one == "" or player_one == None or player_two == "" or player_two == None:
  58. # Reset Players
  59. reset()
  60.  
  61. # Error
  62. clear()
  63.  
  64. print("The users entered were not users in the database.")
  65.  
  66. # Return to Menu
  67. return
  68.  
  69. clear()
  70.  
  71. print("The logged in users are player one:", player_one, "and player two:", player_two + ".")
  72.  
  73. input()
  74.  
  75. clear()
  76.  
  77. round_end = False
  78.  
  79. for rounds in range(5):
  80. current_round = mg_round(rounds)
  81.  
  82. player_one_score += current_round[0]
  83. player_two_score += current_round[1]
  84.  
  85. clear()
  86.  
  87. # Check For Same Scores
  88. clear()
  89.  
  90. if player_one_score == player_two_score:
  91. print("The total scores are the same for both players. Therefore each player will get an extra die roll until a winner is found.")
  92.  
  93. while player_one_score == player_two_score:
  94. roll_one = mg_roll_die()
  95. roll_two = mg_roll_die()
  96.  
  97. input()
  98.  
  99. print("Press enter to roll your dice, player one (" + player_one + ").")
  100.  
  101. input()
  102.  
  103. print("Player one (" + player_one + ") rolled", str(roll_one) + ".")
  104.  
  105. player_one_score += roll_one
  106.  
  107. input()
  108.  
  109. print("Press enter to roll your dice, player two (" + player_two + ").")
  110.  
  111. input()
  112.  
  113. print("Player two (" + player_two + ") rolled", str(roll_two) + ".")
  114.  
  115. player_two_score += roll_two
  116.  
  117. input()
  118.  
  119. if player_one_score == player_two_score:
  120. print("The scores are still the same! Another round of dice rolls must continue to find a winner.")
  121.  
  122. # Print Total Scores
  123. clear()
  124.  
  125. print("The game scores are as follows:\n")
  126. print("Player One (" + player_one + "):", player_one_score)
  127. print("Player Two (" + player_two + "):", player_two_score)
  128.  
  129. # Display Winner
  130. if player_one_score > player_two_score:
  131. print("\nTherefore player one has won!\n")
  132. else:
  133. print("\nTherefore player two has won!\n")
  134.  
  135. # Save Scores
  136. mg_save_scores()
  137.  
  138. # Reset Logged In Users
  139. reset()
  140.  
  141. input()
  142.  
  143.  
  144.  
  145. ## Simulate Die Roll ##
  146. def mg_roll_die():
  147. # Get Random Number Between 1 - 6
  148. return random.randint(1, 6)
  149.  
  150.  
  151.  
  152. ## Commence Single Round ##
  153. def mg_round(current_round):
  154. # Display Current Round
  155. print("\n## Round", current_round + 1)
  156.  
  157. # Calculate Dice Roll Results
  158. player_one_die_one = mg_roll_die()
  159. player_one_die_two = mg_roll_die()
  160. player_two_die_one = mg_roll_die()
  161. player_two_die_two = mg_roll_die()
  162.  
  163. # Player One's Turn
  164. print("\nIt is player one's (" + player_one + ") turn.")
  165.  
  166. input("\nPress enter to roll the first dice, player one (" + player_one + ").\n")
  167.  
  168. print("Player one (" + player_one + ") rolled", str(player_one_die_one) + ".")
  169.  
  170. input()
  171.  
  172. input("Press enter to roll the second dice, player one (" + player_one + ").\n")
  173.  
  174. print("Player one (" + player_one + ") rolled", str(player_one_die_two) + ".")
  175.  
  176. input()
  177.  
  178. # Player Two's Turn
  179. print("\n\nIt is player two's (" + player_two + ") turn.")
  180.  
  181. input("\nPress enter to roll the first dice, player two (" + player_two + ").\n")
  182.  
  183. print("Player two (" + player_two + ") rolled", str(player_two_die_one) + ".")
  184.  
  185. input()
  186.  
  187. input("Press enter to roll the second dice, player two (" + player_two + ").\n")
  188.  
  189. print("Player two (" + player_two + ") rolled", str(player_two_die_two) + ".")
  190.  
  191. input()
  192.  
  193. # Calculate Scores Based on Rules
  194. player_one_score = player_one_die_one + player_one_die_two
  195. player_two_score = player_two_die_one + player_two_die_two
  196.  
  197. if player_one_score % 2 == 0:
  198. player_one_score += 10
  199. else:
  200. player_one_score -= 5
  201.  
  202. if player_two_score % 2 == 0:
  203. player_two_score += 10
  204. else:
  205. player_two_score -= 5
  206.  
  207. # Give Extra Die Roll
  208. if player_one_die_one == player_one_die_two:
  209. input()
  210.  
  211. print("Player one (" + player_one + ") gets another die roll because they rolled a double.")
  212.  
  213. input()
  214.  
  215. extra_die_roll = mg_roll_die()
  216. player_one_score += extra_die_roll
  217.  
  218. print("Player one (" + player_one + ") rolled", extra_die_roll)
  219.  
  220. input()
  221.  
  222. if player_two_die_one == player_two_die_two:
  223. input()
  224.  
  225. print("Player two (" + player_two + ") gets another die roll because they rolled a double.")
  226.  
  227. input()
  228.  
  229. extra_die_roll = mg_roll_die()
  230. player_two_score += extra_die_roll
  231.  
  232. print("Player two (" + player_two + ") rolled", extra_die_roll)
  233.  
  234. input()
  235.  
  236. # Check Score
  237. if player_one_score < 0:
  238. player_one_score = 0
  239.  
  240. if player_two_score < 0:
  241. player_two_score = 0
  242.  
  243. # Display Scores
  244. print("\n\nTotal Round Scores:")
  245. print(" Player One (" + player_one + "):", player_one_score)
  246. print(" Player Two (" + player_two + "):", player_two_score)
  247.  
  248. input()
  249.  
  250. # Return Total Scores
  251. return [player_one_score, player_two_score]
  252.  
  253.  
  254.  
  255. ## Save Scores ##
  256. def mg_save_scores():
  257. file = open("scores.txt", "a+")
  258.  
  259. file.write(player_one + "\n")
  260.  
  261. file.write(str(player_one_score) + "\n")
  262.  
  263. file.write(player_two + "\n")
  264.  
  265. file.write(str(player_two_score) + "\n")
  266.  
  267. file.close()
  268.  
  269.  
  270.  
  271. ###### Game Setup System -- Login to two user accounts, show rules, etc ######
  272.  
  273. ## Display Rules ##
  274. def gss_display_rules():
  275. clear()
  276.  
  277. print("Here are the rules of the game:")
  278. print(" # Score")
  279. print(" 1. The points rolled on each player's dice are added to their score.")
  280. print(" 2. If the total is an even number, an additional 10 points are added to their score.")
  281. print(" 3. If the total is an odd number, 5 points are subtracted from their score.")
  282. print(" 4. If they roll a double, they get to roll one extra die and get the number of points added to their score.")
  283. print(" 5. The score of a player cannot go below 0 at any point.")
  284. print(" # Winning")
  285. print(" 6. The person with the highest score at the end of the 5 rounds wins.")
  286. print(" 7. If both players have the same score at the end of the 5 rounds, they each roll 1 die and whoever gets the highest score wins (this repeats until someone wins).")
  287.  
  288. input()
  289.  
  290.  
  291.  
  292. ## Display High Scores ##
  293. def gss_display_high_scores():
  294. clear()
  295.  
  296. # Check If File Opened
  297. if Path("scores.txt").exists():
  298. # Open Score File
  299. scores_file = open("scores.txt", "r")
  300.  
  301. # Get Scores File
  302. scores_file_content = scores_file.readlines()
  303.  
  304. # Get Usernames & Scores
  305. usernames = scores_file_content[0::2]
  306. scores = scores_file_content[1::2]
  307.  
  308. # Check Scores More Than or Equal to Five
  309. if len(scores) >= 5:
  310. print("Top Five Scores:")
  311.  
  312. # Sort Scores
  313. sorted_scores = []
  314. sorted_scores = sorted(scores, key = float, reverse = True)
  315.  
  316. # Get Usernames of Top Five
  317. top_five_usernames = []
  318.  
  319. for i in range(len(scores)):
  320. if scores[i] == sorted_scores[0]:
  321. top_five_usernames.append(usernames[i])
  322.  
  323. for i in range(len(scores)):
  324. if scores[i] == sorted_scores[1]:
  325. top_five_usernames.append(usernames[i])
  326.  
  327. for i in range(len(scores)):
  328. if scores[i] == sorted_scores[2]:
  329. top_five_usernames.append(usernames[i])
  330.  
  331. for i in range(len(scores)):
  332. if scores[i] == sorted_scores[3]:
  333. top_five_usernames.append(usernames[i])
  334.  
  335. for i in range(len(scores)):
  336. if scores[i] == sorted_scores[4]:
  337. top_five_usernames.append(usernames[i])
  338.  
  339. # Print Scores
  340. if len(scores) >= 5:
  341. for i in range(5):
  342. print(" " + str(i + 1) + ".", top_five_usernames[i].strip() + " -", sorted_scores[i].strip())
  343. else:
  344. print("There are not enough scores to display the top five!")
  345.  
  346. # Close Scores File
  347. scores_file.close()
  348. else:
  349. print("There are not enough scores to display the top five!")
  350.  
  351. input()
  352.  
  353.  
  354.  
  355. ###### User Authentication System ######
  356.  
  357. ## Delete Account ##
  358. def uas_delete_account():
  359. print("")
  360.  
  361. if not ms_delete_menu():
  362. clear()
  363.  
  364. print("The entered username and password was not found.")
  365.  
  366. input()
  367.  
  368.  
  369.  
  370. ## Create Account ##
  371. def uas_create_account():
  372. ms_create_menu()
  373.  
  374.  
  375.  
  376. ## Check Account ##
  377. def uas_check_account(current_user):
  378. print("")
  379.  
  380. login = ms_check_menu(current_user)
  381.  
  382. global player_one, player_two
  383.  
  384. if player_one == "":
  385. player_one = login
  386. elif player_two == "":
  387. player_two = login
  388.  
  389. input()
  390.  
  391.  
  392.  
  393. ###### Menu System ######
  394.  
  395. ## Create Menu ##
  396. def ms_create_menu():
  397. clear()
  398.  
  399. # Get Username & Password
  400. entered_username = input("What is the username of this new user? ")
  401. entered_password = input("What is the password of this new user? ")
  402.  
  403. # Preexisting Username & Password
  404. preexisting_username = False
  405. preexisting_password = False
  406.  
  407. # Check Username Doesn't Exist
  408. if Path("usernames.txt").exists():
  409. usernames_file = open("usernames.txt", "r")
  410.  
  411. for line in usernames_file.readlines():
  412. if entered_username == line.strip():
  413. print("\nA user with that username already exists!")
  414.  
  415. preexisting_username = True
  416.  
  417. # Check Password Doesn't Exist
  418. if Path("passwords.txt").exists():
  419. passwords_file = open("passwords.txt", "r")
  420.  
  421. for line in passwords_file.readlines():
  422. if entered_password == line.strip():
  423. print("\nA user with that password already exists!")
  424.  
  425. preexisting_password = True
  426.  
  427. # Check Password is Valid
  428. if entered_password == "":
  429. input()
  430.  
  431. clear()
  432.  
  433. print("The password entered was not valid. The user has not been created.")
  434.  
  435. input()
  436.  
  437. clear()
  438.  
  439. return
  440.  
  441. if not preexisting_username and not preexisting_password:
  442. # Add Username
  443. usernames_file = open("usernames.txt", "a+")
  444.  
  445. usernames_file.write(entered_username.strip() + "\n")
  446.  
  447. usernames_file.close()
  448.  
  449. # Add Password
  450. passwords_file = open("passwords.txt", "a+")
  451.  
  452. passwords_file.write(entered_password.strip() + "\n")
  453.  
  454. passwords_file.close()
  455.  
  456. input()
  457.  
  458.  
  459.  
  460. ## Delete Menu ##
  461. def ms_delete_menu():
  462. # Get Username & Password
  463. entered_username = input("What is the username of the user you want to delete? ")
  464. entered_password = input("What is the password of the user you want to delete? ")
  465.  
  466. # Store Location of Line of Found Username & Password
  467. username_found_line = 1000000
  468. password_found_line = 1000000
  469.  
  470. # Search for Username
  471. username_list = open("usernames.txt", "r").readlines()
  472.  
  473. for line in range(len(username_list)):
  474. if entered_username == username_list[line].strip():
  475. username_found_line = line
  476.  
  477. # Search for Password
  478. password_list = open("passwords.txt", "r").readlines()
  479.  
  480. for line in range(len(password_list)):
  481. if entered_password == password_list[line].strip():
  482. password_found_line = line
  483.  
  484. # Check if Both Found
  485. if username_found_line == 1000000 or password_found_line == 1000000:
  486. return False
  487.  
  488. # Delete Username & Password from Lists
  489. username_list.pop(username_found_line)
  490. password_list.pop(password_found_line)
  491.  
  492. # Write New Username File
  493. username_file = open("usernames.txt", "w")
  494.  
  495. username_file.write("".join(username_list))
  496.  
  497. # Write New Password File
  498. password_file = open("passwords.txt", "w")
  499.  
  500. password_file.write("".join(password_list))
  501.  
  502. # Return Found
  503. return True
  504.  
  505.  
  506.  
  507. ## Check Menu ##
  508. def ms_check_menu(current_user):
  509. # Get Username & Password
  510. entered_username = input("What is the username of player " + current_user + " that you want to access? ")
  511. entered_password = input("What is the password of player " + current_user + " that you want to access? ")
  512.  
  513. # Store Whether Username & Password Found
  514. username_found = False
  515. password_found = False
  516.  
  517. # Search for Username
  518. username_list = open("usernames.txt", "r").readlines()
  519.  
  520. for line in username_list:
  521. if entered_username == line.strip():
  522. username_found = True
  523.  
  524. # Search for Password
  525. password_list = open("passwords.txt", "r").readlines()
  526.  
  527. for line in password_list:
  528. if entered_password == line.strip():
  529. password_found = True
  530.  
  531. # Check if Both Found
  532. if username_found and password_found:
  533. return entered_username
  534. else:
  535. return ""
  536.  
  537.  
  538.  
  539. ## Main Menu ##
  540. def ms_main_menu():
  541. clear()
  542.  
  543. print("#" * 38)
  544. print("# #")
  545. print("# [1] Create Account #")
  546. print("# [2] Delete Account #")
  547. print("# #")
  548. print("# [3] Start Game #")
  549. print("# #")
  550. print("# [4] Show Rules #")
  551. print("# [5] Show High Scores #")
  552. print("# [6] Close Program #")
  553. print("# #")
  554. print("#" * 38)
  555.  
  556. menu_choice = input("\nEnter your choice: ")
  557.  
  558. if menu_choice == "1":
  559. uas_create_account()
  560. elif menu_choice == "2":
  561. uas_delete_account()
  562. elif menu_choice == "3":
  563. mg_start_game()
  564. elif menu_choice == "4":
  565. gss_display_rules()
  566. elif menu_choice == "5":
  567. gss_display_high_scores()
  568. elif menu_choice == "6":
  569. return False
  570.  
  571. return True
  572.  
  573. input()
  574.  
  575. clear()
  576.  
  577.  
  578.  
  579. ###### Main Loop ######
  580.  
  581. clear()
  582.  
  583. ## Loop ##
  584. while ms_main_menu():
  585. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement