|
| 1 | +import random |
| 2 | +import string |
| 3 | +import time |
| 4 | + |
| 5 | +# Existing code (is_valid_word, is_valid_move, word_list) |
| 6 | + |
| 7 | +# Additional features |
| 8 | +def get_word_length_difficulty(difficulty): |
| 9 | + if difficulty == "Easy": |
| 10 | + return 3 |
| 11 | + elif difficulty == "Medium": |
| 12 | + return 4 |
| 13 | + elif difficulty == "Hard": |
| 14 | + return 5 |
| 15 | + else: |
| 16 | + raise ValueError("Invalid difficulty level.") |
| 17 | + |
| 18 | +def get_random_letter(): |
| 19 | + return random.choice(string.ascii_lowercase) |
| 20 | + |
| 21 | +def get_hint(prev_word): |
| 22 | + # Simple hint: Suggest a valid word with one letter change from prev_word |
| 23 | + for _ in range(10): # Try 10 times to find a hint |
| 24 | + new_word = prev_word[:3] + get_random_letter() + prev_word[4:] |
| 25 | + if is_valid_word(new_word) and is_valid_move(prev_word, new_word): |
| 26 | + return new_word |
| 27 | + return None # Return None if a hint can't be found |
| 28 | + |
| 29 | +def word_chain_game(): |
| 30 | + # Existing code |
| 31 | + |
| 32 | + print("Select Difficulty Level:") |
| 33 | + print("1. Easy") |
| 34 | + print("2. Medium") |
| 35 | + print("3. Hard") |
| 36 | + level_choice = input("Enter the level number: ") |
| 37 | + |
| 38 | + if level_choice == "1": |
| 39 | + difficulty = "Easy" |
| 40 | + elif level_choice == "2": |
| 41 | + difficulty = "Medium" |
| 42 | + elif level_choice == "3": |
| 43 | + difficulty = "Hard" |
| 44 | + else: |
| 45 | + print("Invalid choice. Defaulting to Easy difficulty.") |
| 46 | + difficulty = "Easy" |
| 47 | + |
| 48 | + word_length_required = get_word_length_difficulty(difficulty) |
| 49 | + |
| 50 | + print(f"Difficulty Level: {difficulty}") |
| 51 | + |
| 52 | + # Optional: Implement a timer for each turn |
| 53 | + def set_timer(): |
| 54 | + return time.time() |
| 55 | + |
| 56 | + def check_time_elapsed(start_time, max_time): |
| 57 | + return time.time() - start_time > max_time |
| 58 | + |
| 59 | + max_turn_time = 30 # Maximum time in seconds per turn |
| 60 | + |
| 61 | + while True: |
| 62 | + # Existing code |
| 63 | + |
| 64 | + # Optional: Timer |
| 65 | + start_time = set_timer() |
| 66 | + |
| 67 | + new_word = input("Enter a word: ").strip().lower() |
| 68 | + |
| 69 | + if check_time_elapsed(start_time, max_turn_time): |
| 70 | + print("Time's up! You took too long to answer. You lose this round.") |
| 71 | + break |
| 72 | + |
| 73 | + # Optional: Hints |
| 74 | + if new_word.lower() == "hint": |
| 75 | + hint = get_hint(prev_word) |
| 76 | + if hint: |
| 77 | + print(f"Hint: Try using '{hint}'.") |
| 78 | + continue |
| 79 | + else: |
| 80 | + print("Sorry, couldn't find a hint this time.") |
| 81 | + continue |
| 82 | + |
| 83 | + # Optional: Save and Load |
| 84 | + if new_word.lower() == "save": |
| 85 | + # Implement the save functionality to save the game progress. |
| 86 | + # You can use pickle or JSON to save the state of the game. |
| 87 | + print("Game progress saved.") |
| 88 | + continue |
| 89 | + elif new_word.lower() == "load": |
| 90 | + # Implement the load functionality to resume the saved game. |
| 91 | + # Load the state of the game and continue from there. |
| 92 | + print("Game progress loaded.") |
| 93 | + continue |
| 94 | + |
| 95 | + # Existing code (validation checks) |
| 96 | + |
| 97 | + # Calculate points based on word length |
| 98 | + points = len(new_word) |
| 99 | + |
| 100 | + # Optional: Points for making certain challenging moves |
| 101 | + # For example, bonus points for using certain letters, patterns, etc. |
| 102 | + |
| 103 | + # Existing code (append new_word to chain, update player_turn) |
| 104 | + |
| 105 | + # Check if player's word is too short for the difficulty level |
| 106 | + if len(new_word) < word_length_required: |
| 107 | + print(f"Word should be at least {word_length_required} letters long for {difficulty} difficulty.") |
| 108 | + print("You lose this round.") |
| 109 | + break |
| 110 | + |
| 111 | + # Check if the game should end |
| 112 | + if player_turn > 1 and not is_valid_move(chain[-2], chain[-1]): |
| 113 | + print(f"Game over! {player_name} cannot find a valid word. {player_name} wins!") |
| 114 | + break |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + word_chain_game() |
0 commit comments