Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###### Basics ######
- import random
- from pathlib import Path
- player_one = ""
- player_two = ""
- player_one_score = 0
- player_two_score = 0
- def clear():
- print("\n" * 400)
- def reset():
- global player_one, player_two
- player_one = ""
- player_two = ""
- player_one_score = 0
- player_two_score = 0
- ###### Main Game ######
- ## Main Game ##
- def mg_start_game():
- # Check Usernames Exist
- if Path("usernames.txt").exists():
- if len(open("usernames.txt").readlines()) < 2:
- print("\nThere are not two users!")
- input()
- return
- global player_one, player_two, player_one_score, player_two_score # Globalise variables
- # Access Accounts
- clear()
- reset()
- uas_check_account("one")
- clear()
- uas_check_account("two")
- if player_one == "" or player_one == None or player_two == "" or player_two == None:
- # Reset Players
- reset()
- # Error
- clear()
- print("The users entered were not users in the database.")
- input()
- # Return to Menu
- return
- clear()
- print("The logged in users are player one:", player_one, "and player two:", player_two + ".")
- input()
- clear()
- round_end = False
- for rounds in range(5):
- current_round = mg_round(rounds)
- player_one_score += current_round[0]
- player_two_score += current_round[1]
- clear()
- # Check For Same Scores
- clear()
- if player_one_score == player_two_score:
- print("The total scores are the same for both players. Therefore each player will get an extra die roll until a winner is found.")
- while player_one_score == player_two_score:
- roll_one = mg_roll_die()
- roll_two = mg_roll_die()
- input()
- print("Press enter to roll your dice, player one (" + player_one + ").")
- input()
- print("Player one (" + player_one + ") rolled", str(roll_one) + ".")
- player_one_score += roll_one
- input()
- print("Press enter to roll your dice, player two (" + player_two + ").")
- input()
- print("Player two (" + player_two + ") rolled", str(roll_two) + ".")
- player_two_score += roll_two
- input()
- if player_one_score == player_two_score:
- print("The scores are still the same! Another round of dice rolls must continue to find a winner.")
- # Print Total Scores
- clear()
- print("The game scores are as follows:\n")
- print("Player One (" + player_one + "):", player_one_score)
- print("Player Two (" + player_two + "):", player_two_score)
- # Display Winner
- if player_one_score > player_two_score:
- print("\nTherefore player one has won!\n")
- else:
- print("\nTherefore player two has won!\n")
- # Save Scores
- mg_save_scores()
- # Reset Logged In Users
- reset()
- input()
- ## Show ASCII Dice ##
- def mg_ascii_die(die_number):
- print("\n")
- if die_number == 1:
- print("############################\n# #\n# #\n# #\n# #\n# #\n# #### #\n# #### #\n# #### #\n# #\n# #\n# #\n# #\n# #\n############################")
- elif die_number == 2:
- print("############################\n# #\n# #### #\n# #### #\n# #### #\n# #\n# #\n# #\n# #\n# #\n# #### #\n# #### #\n# #### #\n# #\n############################")
- elif die_number == 3:
- print("############################\n# #\n# #### #\n# #### #\n# #### #\n# #\n# #### #\n# #### #\n# #### #\n# #\n# #### #\n# #### #\n# #### #\n# #\n############################")
- elif die_number == 4:
- print("############################\n# #\n# #### #### #\n# #### #### #\n# #### #### #\n# #\n# #\n# #\n# #\n# #\n# #### #### #\n# #### #### #\n# #### #### #\n# #\n############################")
- elif die_number == 5:
- print("############################\n# #\n# #### #### #\n# #### #### #\n# #### #### #\n# #\n# #### #\n# #### #\n# #### #\n# #\n# #### #### #\n# #### #### #\n# #### #### #\n# #\n############################")
- elif die_number == 6:
- print("############################\n# #\n# #### #### #\n# #### #### #\n# #### #### #\n# #\n# #### #### #\n# #### #### #\n# #### #### #\n# #\n# #### #### #\n# #### #### #\n# #### #### #\n# #\n############################")
- ## Simulate Die Roll ##
- def mg_roll_die():
- # Get Random Number Between 1 - 6
- return random.randint(1, 6)
- ## Commence Single Round ##
- def mg_round(current_round):
- # Display Current Round
- print("\n## Round", current_round + 1)
- # Calculate Dice Roll Results
- player_one_die_one = mg_roll_die()
- player_one_die_two = mg_roll_die()
- player_two_die_one = mg_roll_die()
- player_two_die_two = mg_roll_die()
- # Player One's Turn
- print("\nIt is player one's (" + player_one + ") turn.")
- input("\nPress enter to roll the first dice, player one (" + player_one + ").\n")
- print("Player one (" + player_one + ") rolled", str(player_one_die_one) + ".")
- mg_ascii_die(player_one_die_one)
- input()
- input("Press enter to roll the second dice, player one (" + player_one + ").\n")
- print("Player one (" + player_one + ") rolled", str(player_one_die_two) + ".")
- mg_ascii_die(player_one_die_two)
- input()
- # Player Two's Turn
- print("\n\nIt is player two's (" + player_two + ") turn.")
- input("\nPress enter to roll the first dice, player two (" + player_two + ").\n")
- print("Player two (" + player_two + ") rolled", str(player_two_die_one) + ".")
- mg_ascii_die(player_two_die_one)
- input()
- input("Press enter to roll the second dice, player two (" + player_two + ").\n")
- print("Player two (" + player_two + ") rolled", str(player_two_die_two) + ".")
- mg_ascii_die(player_two_die_two)
- input()
- # Calculate Scores Based on Rules
- player_one_score = player_one_die_one + player_one_die_two
- player_two_score = player_two_die_one + player_two_die_two
- if player_one_score % 2 == 0:
- player_one_score += 10
- else:
- player_one_score -= 5
- if player_two_score % 2 == 0:
- player_two_score += 10
- else:
- player_two_score -= 5
- # Give Extra Die Roll
- if player_one_die_one == player_one_die_two:
- input()
- print("Player one (" + player_one + ") gets another die roll because they rolled a double.")
- input()
- extra_die_roll = mg_roll_die()
- player_one_score += extra_die_roll
- print("Player one (" + player_one + ") rolled", extra_die_roll)
- mg_ascii_die(extra_die_roll)
- input()
- if player_two_die_one == player_two_die_two:
- input()
- print("Player two (" + player_two + ") gets another die roll because they rolled a double.")
- input()
- extra_die_roll = mg_roll_die()
- player_two_score += extra_die_roll
- print("Player two (" + player_two + ") rolled", extra_die_roll)
- mg_ascii_die(extra_die_roll)
- input()
- # Check Score
- if player_one_score < 0:
- player_one_score = 0
- if player_two_score < 0:
- player_two_score = 0
- # Display Scores
- print("\n\nTotal Round Scores:")
- print(" Player One (" + player_one + "):", player_one_score)
- print(" Player Two (" + player_two + "):", player_two_score)
- input()
- # Return Total Scores
- return [player_one_score, player_two_score]
- ## Save Scores ##
- def mg_save_scores():
- file = open("scores.txt", "a+")
- file.write(player_one + "\n")
- file.write(str(player_one_score) + "\n")
- file.write(player_two + "\n")
- file.write(str(player_two_score) + "\n")
- file.close()
- ###### Game Setup System -- Login to two user accounts, show rules, etc ######
- ## Display Rules ##
- def gss_display_rules():
- clear()
- print("Here are the rules of the game:")
- print(" # Score")
- print(" 1. The points rolled on each player's dice are added to their score.")
- print(" 2. If the total is an even number, an additional 10 points are added to their score.")
- print(" 3. If the total is an odd number, 5 points are subtracted from their score.")
- print(" 4. If they roll a double, they get to roll one extra die and get the number of points added to their score.")
- print(" 5. The score of a player cannot go below 0 at any point.")
- print(" # Winning")
- print(" 6. The person with the highest score at the end of the 5 rounds wins.")
- 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).")
- input()
- ## Display High Scores ##
- def gss_display_high_scores():
- clear()
- # Check If File Opened
- if Path("scores.txt").exists():
- # Open Score File
- scores_file = open("scores.txt", "r")
- # Get Scores File
- scores_file_content = scores_file.readlines()
- # Get Usernames & Scores
- usernames = scores_file_content[0::2]
- scores = scores_file_content[1::2]
- # Check Scores More Than or Equal to Five
- if len(scores) >= 5:
- print("Top Five Scores:")
- # Sort Scores
- sorted_scores = []
- sorted_scores = sorted(scores, key = float, reverse = True)
- # Get Usernames of Top Five
- top_five_usernames = []
- for i in range(len(scores)):
- if scores[i] == sorted_scores[0]:
- top_five_usernames.append(usernames[i])
- for i in range(len(scores)):
- if scores[i] == sorted_scores[1]:
- top_five_usernames.append(usernames[i])
- for i in range(len(scores)):
- if scores[i] == sorted_scores[2]:
- top_five_usernames.append(usernames[i])
- for i in range(len(scores)):
- if scores[i] == sorted_scores[3]:
- top_five_usernames.append(usernames[i])
- for i in range(len(scores)):
- if scores[i] == sorted_scores[4]:
- top_five_usernames.append(usernames[i])
- # Print Scores
- if len(scores) >= 5:
- for i in range(5):
- print(" " + str(i + 1) + ".", top_five_usernames[i].strip() + " -", sorted_scores[i].strip())
- else:
- print("There are not enough scores to display the top five!")
- # Close Scores File
- scores_file.close()
- else:
- print("There are not enough scores to display the top five!")
- input()
- ###### User Authentication System ######
- ## Delete Account ##
- def uas_delete_account():
- print("")
- if not ms_delete_menu():
- clear()
- print("The entered username and password was not found.")
- input()
- ## Create Account ##
- def uas_create_account():
- ms_create_menu()
- ## Check Account ##
- def uas_check_account(current_user):
- print("")
- login = ms_check_menu(current_user)
- global player_one, player_two
- if login == 5:
- print("Error: There are no users.")
- else:
- if player_one == "":
- player_one = login
- elif player_two == "":
- player_two = login
- input()
- ## Show Accounts ##
- def uas_show_accounts():
- print("\nAll Users:\n")
- if Path("usernames.txt").exists():
- usernames_file = open("usernames.txt", "r")
- for line in usernames_file.readlines():
- print(line.strip())
- input()
- ###### Menu System ######
- ## Create Menu ##
- def ms_create_menu():
- clear()
- # Get Username & Password
- entered_username = input("What is the username of this new user? ")
- entered_password = input("What is the password of this new user? ")
- # Preexisting Username & Password
- preexisting_username = False
- preexisting_password = False
- # Check Password is Valid
- if entered_password == "" or entered_username == "":
- input()
- clear()
- print("The details entered were not valid. The user has not been created.")
- input()
- clear()
- return
- else:
- # Check Username File Exist
- if Path("usernames.txt").exists():
- usernames_file = open("usernames.txt", "r")
- for line in usernames_file.readlines():
- if entered_username == line.strip():
- print("\nA user with that username already exists!")
- preexisting_username = True
- # Check Password Doesn't Exist
- if Path("passwords.txt").exists():
- passwords_file = open("passwords.txt", "r")
- for line in passwords_file.readlines():
- if entered_password == line.strip():
- print("\nA user with that password already exists!")
- preexisting_password = True
- if not preexisting_username and not preexisting_password:
- # Add Username
- usernames_file = open("usernames.txt", "a+")
- usernames_file.write(entered_username.strip() + "\n")
- usernames_file.close()
- # Add Password
- passwords_file = open("passwords.txt", "a+")
- passwords_file.write(entered_password.strip() + "\n")
- passwords_file.close()
- input()
- ## Delete Menu ##
- def ms_delete_menu():
- # Check File Exist
- if Path("usernames.txt").exists() and Path("passwords.txt").exists():
- # Get Username & Password
- entered_username = input("What is the username of the user you want to delete? ")
- entered_password = input("What is the password of the user you want to delete? ")
- # Store Location of Line of Found Username & Password
- username_found_line = 1000000
- password_found_line = 1000000
- # Search for Username
- username_list = open("usernames.txt", "r").readlines()
- for line in range(len(username_list)):
- if entered_username == username_list[line].strip():
- username_found_line = line
- # Search for Password
- password_list = open("passwords.txt", "r").readlines()
- for line in range(len(password_list)):
- if entered_password == password_list[line].strip():
- password_found_line = line
- # Check if Both Found
- if username_found_line == 1000000 or password_found_line == 1000000:
- return False
- # Check if Both Same
- if username_found_line == password_found_line:
- # Delete Username & Password from Lists
- username_list.pop(username_found_line)
- password_list.pop(password_found_line)
- # Write New Username File
- username_file = open("usernames.txt", "w")
- username_file.write("".join(username_list))
- # Write New Password File
- password_file = open("passwords.txt", "w")
- password_file.write("".join(password_list))
- # Return Found
- return True
- else:
- return False
- else:
- clear()
- print("Error: There are no users.")
- input()
- return True
- ## Check Menu ##
- def ms_check_menu(current_user):
- # Check Files Exist
- if Path("usernames.txt").exists() and Path("passwords.txt").exists():
- # Get Username & Password
- entered_username = input("What is the username of player " + current_user + " that you want to access? ")
- entered_password = input("What is the password of player " + current_user + " that you want to access? ")
- # Check Username
- if entered_username == player_one:
- return ""
- # Store Whether Username & Password Found
- username_found = False
- password_found = False
- # Search for Username
- username_list = open("usernames.txt", "r").readlines()
- username_line = 0
- for line in range(len(username_list)):
- if entered_username == username_list[line].strip():
- username_found = True
- username_line = line
- # Search for Password
- password_list = open("passwords.txt", "r").readlines()
- if username_found:
- if entered_password == password_list[username_line].strip():
- password_found = True
- # Check if Both Found
- if username_found and password_found:
- return entered_username
- else:
- return ""
- else:
- return 5
- ## Main Menu ##
- def ms_main_menu():
- clear()
- print("#" * 38)
- print("# #")
- print("# [1] Create Account #")
- print("# [2] Delete Account #")
- print("# [3] Show Accounts #")
- print("# #")
- print("# [4] Start Game #")
- print("# #")
- print("# [5] Show Rules #")
- print("# [6] Show High Scores #")
- print("# [7] Close Program #")
- print("# #")
- print("#" * 38)
- menu_choice = input("\nEnter your choice: ")
- if menu_choice == "1":
- uas_create_account()
- elif menu_choice == "2":
- uas_delete_account()
- elif menu_choice == "3":
- uas_show_accounts()
- elif menu_choice == "4":
- mg_start_game()
- elif menu_choice == "5":
- gss_display_rules()
- elif menu_choice == "6":
- gss_display_high_scores()
- elif menu_choice == "7":
- return False
- return True
- input()
- clear()
- ###### Main Loop ######
- clear()
- ## Loop ##
- while ms_main_menu():
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement