Amazing-Python-Scripts

Форк
0
125 строк · 3.7 Кб
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

9

10
def get_word_length_difficulty(difficulty):
11
    if difficulty == "Easy":
12
        return 3
13
    elif difficulty == "Medium":
14
        return 4
15
    elif difficulty == "Hard":
16
        return 5
17
    else:
18
        raise ValueError("Invalid difficulty level.")
19

20

21
def get_random_letter():
22
    return random.choice(string.ascii_lowercase)
23

24

25
def get_hint(prev_word):
26
    # Simple hint: Suggest a valid word with one letter change from prev_word
27
    for _ in range(10):  # Try 10 times to find a hint
28
        new_word = prev_word[:3] + get_random_letter() + prev_word[4:]
29
        if is_valid_word(new_word) and is_valid_move(prev_word, new_word):
30
            return new_word
31
    return None  # Return None if a hint can't be found
32

33

34
def word_chain_game():
35
    # Existing code
36

37
    print("Select Difficulty Level:")
38
    print("1. Easy")
39
    print("2. Medium")
40
    print("3. Hard")
41
    level_choice = input("Enter the level number: ")
42

43
    if level_choice == "1":
44
        difficulty = "Easy"
45
    elif level_choice == "2":
46
        difficulty = "Medium"
47
    elif level_choice == "3":
48
        difficulty = "Hard"
49
    else:
50
        print("Invalid choice. Defaulting to Easy difficulty.")
51
        difficulty = "Easy"
52

53
    word_length_required = get_word_length_difficulty(difficulty)
54

55
    print(f"Difficulty Level: {difficulty}")
56

57
    # Optional: Implement a timer for each turn
58
    def set_timer():
59
        return time.time()
60

61
    def check_time_elapsed(start_time, max_time):
62
        return time.time() - start_time > max_time
63

64
    max_turn_time = 30  # Maximum time in seconds per turn
65

66
    while True:
67
        # Existing code
68

69
        # Optional: Timer
70
        start_time = set_timer()
71

72
        new_word = input("Enter a word: ").strip().lower()
73

74
        if check_time_elapsed(start_time, max_turn_time):
75
            print("Time's up! You took too long to answer. You lose this round.")
76
            break
77

78
        # Optional: Hints
79
        if new_word.lower() == "hint":
80
            hint = get_hint(prev_word)
81
            if hint:
82
                print(f"Hint: Try using '{hint}'.")
83
                continue
84
            else:
85
                print("Sorry, couldn't find a hint this time.")
86
                continue
87

88
        # Optional: Save and Load
89
        if 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.
92
            print("Game progress saved.")
93
            continue
94
        elif 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.
97
            print("Game progress loaded.")
98
            continue
99

100
        # Existing code (validation checks)
101

102
        # Calculate points based on word length
103
        points = len(new_word)
104

105
        # Optional: Points for making certain challenging moves
106
        # 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 level
111
        if len(new_word) < word_length_required:
112
            print(
113
                f"Word should be at least {word_length_required} letters long for {difficulty} difficulty.")
114
            print("You lose this round.")
115
            break
116

117
        # Check if the game should end
118
        if player_turn > 1 and not is_valid_move(chain[-2], chain[-1]):
119
            print(
120
                f"Game over! {player_name} cannot find a valid word. {player_name} wins!")
121
            break
122

123

124
if __name__ == "__main__":
125
    word_chain_game()
126

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.