Advertisement
Josif_tepe

Untitled

May 3rd, 2025
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. import random
  2.  
  3. import nltk
  4. import regex
  5. import re
  6. nltk.download('wordlist')
  7. from nltk.corpus import words
  8. word_list = []
  9. # for word in words.words():
  10. #     if word.isalpha() and len(word) >= 4:
  11. #         word_list.append(word)
  12. #
  13. # print(word_list)
  14.  
  15. def get_hint(known_pattern, guessed_letters):
  16.     pattern = "^" + known_pattern.replace("_", ".") + "$"
  17.     regex = re.compile(pattern)
  18.  
  19.     possible_words = []
  20.     for word in word_list:
  21.         if len(word) == len(known_pattern) and regex.match(word):
  22.             possible_words.append(word)
  23.  
  24.     letter_count = {}
  25.     for word in possible_words:
  26.         for letter in set(word):
  27.             if letter not in guessed_letters and letter != "_":
  28.                 letter_count[letter] = letter_count.get(letter) + 1
  29.  
  30.     if letter_count:
  31.         return max(letter_count, key=letter_count.get)
  32.  
  33.     return None
  34.  
  35. def hangman_with_ai():
  36.     word_to_guess = random.choice(word_list)
  37.     attemps = 6
  38.    
  39.     print("Welcome to Hangman!")
  40.     print(f"The word has {len(word_to_guess)} letters!")
  41.     print("Start guessing: ")
  42.    
  43.     while attemps > 0:
  44.        
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement