Advertisement
Guest User

Untitled

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