#### TODO: CREATE OPTION TO SHOW AVAILABLE USERS #### TODO: WHEN A PASSWORD OR USERNAME ISN'T ACCEPTED FOR THE GAME, SHOW A MESSAGE INSTEAD OF JUST GOING BACK TO THE MENU #### 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 #### PASSWORD, IT WILL WORK BECAUSE IT ONLY LOOKS FOR A PASSWORD AND USERNAME, NOT IF THEY MATCH TOGETHER ###### 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(): player_one = "" player_two = "" player_one_score = 0 player_two_score = 0 ###### Main Game ###### ## Main Game ## def mg_start_game(): # Check Usernames Exist if not 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() 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.") # 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() ## 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) + ".") 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) + ".") 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) + ".") 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) + ".") 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) 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) 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 player_one == "": player_one = login elif player_two == "": player_two = login 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 Username Doesn't 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 # Check Password is Valid if entered_password == "": input() clear() print("The password entered was not valid. The user has not been created.") input() clear() return 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(): # 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 # 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 ## Check Menu ## def ms_check_menu(current_user): # 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? ") # Store Whether Username & Password Found username_found = False password_found = False # Search for Username username_list = open("usernames.txt", "r").readlines() for line in username_list: if entered_username == line.strip(): username_found = True # Search for Password password_list = open("passwords.txt", "r").readlines() for line in password_list: if entered_password == line.strip(): password_found = True # Check if Both Found if username_found and password_found: return entered_username else: return "" ## Main Menu ## def ms_main_menu(): clear() print("#" * 38) print("# #") print("# [1] Create Account #") print("# [2] Delete Account #") print("# #") print("# [3] Start Game #") print("# #") print("# [4] Show Rules #") print("# [5] Show High Scores #") print("# [6] 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": mg_start_game() elif menu_choice == "4": gss_display_rules() elif menu_choice == "5": gss_display_high_scores() elif menu_choice == "6": return False return True input() clear() ###### Main Loop ###### clear() ## Loop ## while ms_main_menu(): print()