Amazing-Python-Scripts
125 строк · 3.7 Кб
1import random2import string3import time4
5# Existing code (is_valid_word, is_valid_move, word_list)
6
7# Additional features
8
9
10def get_word_length_difficulty(difficulty):11if difficulty == "Easy":12return 313elif difficulty == "Medium":14return 415elif difficulty == "Hard":16return 517else:18raise ValueError("Invalid difficulty level.")19
20
21def get_random_letter():22return random.choice(string.ascii_lowercase)23
24
25def get_hint(prev_word):26# Simple hint: Suggest a valid word with one letter change from prev_word27for _ in range(10): # Try 10 times to find a hint28new_word = prev_word[:3] + get_random_letter() + prev_word[4:]29if is_valid_word(new_word) and is_valid_move(prev_word, new_word):30return new_word31return None # Return None if a hint can't be found32
33
34def word_chain_game():35# Existing code36
37print("Select Difficulty Level:")38print("1. Easy")39print("2. Medium")40print("3. Hard")41level_choice = input("Enter the level number: ")42
43if level_choice == "1":44difficulty = "Easy"45elif level_choice == "2":46difficulty = "Medium"47elif level_choice == "3":48difficulty = "Hard"49else:50print("Invalid choice. Defaulting to Easy difficulty.")51difficulty = "Easy"52
53word_length_required = get_word_length_difficulty(difficulty)54
55print(f"Difficulty Level: {difficulty}")56
57# Optional: Implement a timer for each turn58def set_timer():59return time.time()60
61def check_time_elapsed(start_time, max_time):62return time.time() - start_time > max_time63
64max_turn_time = 30 # Maximum time in seconds per turn65
66while True:67# Existing code68
69# Optional: Timer70start_time = set_timer()71
72new_word = input("Enter a word: ").strip().lower()73
74if check_time_elapsed(start_time, max_turn_time):75print("Time's up! You took too long to answer. You lose this round.")76break77
78# Optional: Hints79if new_word.lower() == "hint":80hint = get_hint(prev_word)81if hint:82print(f"Hint: Try using '{hint}'.")83continue84else:85print("Sorry, couldn't find a hint this time.")86continue87
88# Optional: Save and Load89if new_word.lower() == "save":90# Implement the save functionality to save the game progress.91# You can use pickle or JSON to save the state of the game.92print("Game progress saved.")93continue94elif new_word.lower() == "load":95# Implement the load functionality to resume the saved game.96# Load the state of the game and continue from there.97print("Game progress loaded.")98continue99
100# Existing code (validation checks)101
102# Calculate points based on word length103points = len(new_word)104
105# Optional: Points for making certain challenging moves106# For example, bonus points for using certain letters, patterns, etc.107
108# Existing code (append new_word to chain, update player_turn)109
110# Check if player's word is too short for the difficulty level111if len(new_word) < word_length_required:112print(113f"Word should be at least {word_length_required} letters long for {difficulty} difficulty.")114print("You lose this round.")115break116
117# Check if the game should end118if player_turn > 1 and not is_valid_move(chain[-2], chain[-1]):119print(120f"Game over! {player_name} cannot find a valid word. {player_name} wins!")121break122
123
124if __name__ == "__main__":125word_chain_game()126