|
| 1 | +import random |
| 2 | +import string |
| 3 | + |
| 4 | +MAX_ATTEMPTS = 8 |
| 5 | + |
| 6 | +WORDS = [ "karate", "judo", "taekwondo", "aikido", "kungfu", "muaythai", "capoeira", "boxing", "python", "javascript", "algorithm", "compiler", "debugger", "recursion", "variable", "function", "database", "network", "kernel", "encryption", "github", "docker", "linux", "server", "cloud", "runtime", "binary", "pointer", "thread", "naruto", "sasuke", "goku", "luffy", "zoro", "gojo", "tanjiro", "levi", "eren", "light", "lelouch", "pikachu" ] |
| 7 | + |
| 8 | +WORDS_BY_LENGTH = {} |
| 9 | + |
| 10 | +for word in WORDS: |
| 11 | + length = len(word) |
| 12 | + if length not in WORDS_BY_LENGTH: |
| 13 | + WORDS_BY_LENGTH[length] = [] |
| 14 | + WORDS_BY_LENGTH[length].append(word) |
| 15 | + |
| 16 | + |
| 17 | +def display_intro(): |
| 18 | + print("REVERSE HANGMAN") |
| 19 | + print("=" * 40) |
| 20 | + print("The computer will try to guess your word.") |
| 21 | + print("Choose any valid word from the dictionary.") |
| 22 | + print("The AI uses elimination logic and letter frequency.\n") |
| 23 | + |
| 24 | + |
| 25 | +def get_secret_word(): |
| 26 | + while True: |
| 27 | + word = input("Enter your secret word: ").lower().strip() |
| 28 | + if not word.isalpha(): |
| 29 | + print("Invalid input. Please enter letters only.") |
| 30 | + continue |
| 31 | + if word not in WORDS: |
| 32 | + print("Word not found in the dictionary.") |
| 33 | + continue |
| 34 | + return word |
| 35 | + |
| 36 | + |
| 37 | +def get_possible_words(pattern, guessed_letters, wrong_letters): |
| 38 | + possible_words = [] |
| 39 | + |
| 40 | + for word in WORDS_BY_LENGTH[len(pattern)]: |
| 41 | + valid = True |
| 42 | + for i in range(len(word)): |
| 43 | + if pattern[i] != "_" and word[i] != pattern[i]: |
| 44 | + valid = False |
| 45 | + break |
| 46 | + |
| 47 | + if pattern[i] == "_" and word[i] in guessed_letters: |
| 48 | + valid = False |
| 49 | + break |
| 50 | + if valid: |
| 51 | + for letter in wrong_letters: |
| 52 | + if letter in word: |
| 53 | + valid = False |
| 54 | + break |
| 55 | + if valid: |
| 56 | + possible_words.append(word) |
| 57 | + return possible_words |
| 58 | + |
| 59 | +def choose_best_letter(possible_words, guessed_letters): |
| 60 | + frequency = {} |
| 61 | + |
| 62 | + for word in possible_words: |
| 63 | + for letter in set(word): |
| 64 | + if letter not in guessed_letters: |
| 65 | + frequency[letter] = frequency.get(letter, 0) + 1 |
| 66 | + if not frequency: |
| 67 | + remaining = [c for c in string.ascii_lowercase if c not in guessed_letters] |
| 68 | + return random.choice(remaining) |
| 69 | + return max(frequency, key=frequency.get) |
| 70 | + |
| 71 | + |
| 72 | +def update_pattern(secret_word, pattern, guess): |
| 73 | + pattern = list(pattern) |
| 74 | + for i in range(len(secret_word)): |
| 75 | + if secret_word[i] == guess: |
| 76 | + pattern[i] = guess |
| 77 | + return "".join(pattern) |
| 78 | + |
| 79 | + |
| 80 | +def play_game(): |
| 81 | + print("\nREVERSE HANGMAN STARTED!!!\n") |
| 82 | + secret_word = get_secret_word() |
| 83 | + pattern = "_" * len(secret_word) |
| 84 | + guessed_letters = set() |
| 85 | + wrong_letters = set() |
| 86 | + attempts_left = MAX_ATTEMPTS |
| 87 | + print("Analyzing the word...\n") |
| 88 | + |
| 89 | + while attempts_left > 0 and "_" in pattern: |
| 90 | + possible_words = get_possible_words(pattern, guessed_letters, wrong_letters) |
| 91 | + guess = choose_best_letter(possible_words, guessed_letters) |
| 92 | + guessed_letters.add(guess) |
| 93 | + print("Computer guesses:", guess) |
| 94 | + |
| 95 | + if guess in secret_word: |
| 96 | + print("Correct guess") |
| 97 | + pattern = update_pattern(secret_word, pattern, guess) |
| 98 | + else: |
| 99 | + print("Wrong guess") |
| 100 | + wrong_letters.add(guess) |
| 101 | + attempts_left -= 1 |
| 102 | + print("Current word:", " ".join(pattern)) |
| 103 | + print("Attempts left:", attempts_left) |
| 104 | + print("\n") |
| 105 | + |
| 106 | + if "_" not in pattern: |
| 107 | + print("Computer successfully guessed your word!!!") |
| 108 | + else: |
| 109 | + print("Computer failed to guess the word.") |
| 110 | + print("Your secret word was:", secret_word) |
| 111 | + |
| 112 | + |
| 113 | +while True: |
| 114 | + play_game() |
| 115 | + choice = input("Wanna play again? (y/n): ").lower().strip() |
| 116 | + if choice != "y": |
| 117 | + print("Thanks for playing Reverse Hangman!") |
| 118 | + break |
0 commit comments