import random import os Constants HIGHSCORESFILE = 'data/highscores.txt' USERDATAFILE =…
- User
import random import os
Constants
HIGH_SCORES_FILE = 'data/high_scores.txt' USER_DATA_FILE = 'data/user_data.txt'
Expanded word lists
DIFFICULTY_WORDS = { 'easy': ['cat', 'fish', 'dog', 'bird', 'frog', 'apple', 'table', 'chair', 'book', 'lamp'], 'medium': ['giraffe', 'monkey', 'penguin', 'squirrel', 'elephant', 'dolphin', 'computer', 'pencil', 'candle', 'window'], 'hard': ['chimpanzee', 'extraordinary', 'microorganism', 'unbelievable', 'determination', 'superintendent', 'phenomenon', 'incomprehensible', 'electromagnetic', 'characteristic'] }
Set number of tries based on difficulty
TRIES = { 'easy': 10, 'medium': 7, 'hard': 5 }
def ensure_data_directory(): """Ensure the data directory exists.""" if not os.path.exists('data'): os.makedirs('data')
def load_user_data(): """Load user data from a file.""" if not os.path.exists(USER_DATA_FILE): return {}
with open(USER_DATA_FILE, 'r') as file: user_data = {} for line in file: username = line.strip() user_data[username] = True # Using a dictionary to track registered users return user_datadef register_user(username): """Register a new user.""" with open(USER_DATA_FILE, 'a') as file: file.write(username + '\n')
def user_login(): """Handle user login or registration.""" user_data = load_user_data()
while True: choice = input("Login (l) or Register (r): ").lower() username = input("Enter your username: ").strip() if choice == 'l': if username in user_data: print(f"Welcome back, {username}!") return username else: print("Username does not exist. Please register.") elif choice == 'r': if username not in user_data: register_user(username) print(f"User {username} registered successfully!") return username else: print("Username already exists. Please choose a different one.") else: print("Invalid choice. Please enter 'l' for Login or 'r' for Register.")def load_high_scores(): """Load high scores from a file.""" if not os.path.exists(HIGH_SCORES_FILE): return {}
with open(HIGH_SCORES_FILE, 'r') as file: high_scores = {} for line in file: difficulty, name, score = line.strip().split(',') high_scores[difficulty] = (name, int(score)) return high_scoresdef save_high_score(name, score, difficulty): """Save player's name and score to a file.""" ensure_data_directory() # Ensure data directory exists with open(HIGH_SCORES_FILE, 'a') as file: file.write(f"{difficulty},{name},{score}\n")
def get_word(difficulty): """Select a random word from a given difficulty level.""" return random.choice(DIFFICULTY_WORDS[difficulty])
def display_progress(selected_word, guessed_letters): """Display the current state of the guessed word.""" display_word = '' for letter in selected_word: if letter in guessed_letters: display_word += letter + ' ' else: display_word += '_ ' print("\nCurrent word:", display_word.strip())
- Assistant
- User
- Assistant