From 2b7ba61912b47473df59693958cce10ef9d389b5 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sat, 26 Nov 2016 17:12:18 +0100 Subject: [PATCH] filter words --- Hangman-bot.py | 90 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 23 deletions(-) diff --git a/Hangman-bot.py b/Hangman-bot.py index a9a8756..a2db432 100644 --- a/Hangman-bot.py +++ b/Hangman-bot.py @@ -3,26 +3,63 @@ import operator import os -words = open('words.txt') +# Start Functions + def clear(): + ''' + Clear console + ''' os.system('cls' if os.name=='nt' else 'clear') + +def print_words(pos=False): + ''' + Print words resolves + Arguments: + pos - View numbers + ''' + print_pos = ' ' + print_pos_bar = ' ' + print_pos_letters = ' ' + for i in range(num_letters): + if pos: + print_pos = print_pos + str(i + 1) + ' ' + print_pos_bar = print_pos_bar + '| ' + print_pos_letters = print_pos_letters + word_resolve[i] + ' ' + print(print_pos) + print(print_pos_bar) + print(print_pos_letters) + print('') + print('') + def best_letter(word_resolve): + ''' + Search best letter + Arguments: + word_resolve - list with word resolve + ''' # Read all words and count letters letters = dict() ignore = ('\xb1', '\xc3', '\xb6', '.', '\xae') + words = open('words.txt') # Read alls words for dictionary for word in words.readlines(): # Just consider which have the same length - if len(word_resolve) == len(word): + if len(word_resolve) == len(word.strip()): l_word = list(word.strip().lower()) + # Check word_resolve is same letters + fit = True + for i in range(len(l_word)): + if word_resolve[i] != l_word[i] and word_resolve[i] != '_': + fit = False # Count the letters - for letter in l_word: - if letter.lower() not in ignore: - if letter in letters: - letters[letter] = letters[letter] + 1 - else: - letters[letter] = 1 + if fit: + for letter in l_word: + if letter.lower() not in ignore: + if letter in letters: + letters[letter] = letters[letter] + 1 + elif not letter in letters_used: + letters[letter] = 1 # Sort sorted_letters = sorted( letters.items(), key=operator.itemgetter(1), reverse=True @@ -31,6 +68,9 @@ def best_letter(word_resolve): # Best letter return sorted_letters[0][0] +# End Functions + +# Start clear() print('Hangman Bot 1.0v') print(' _________ ') @@ -44,6 +84,7 @@ print('| ') # Get num letters and make list resolve num_letters = input('Number of letters: ') clear() +play = True word_resolve = list() letters_used = list() for pos in range(num_letters): @@ -52,13 +93,14 @@ for pos in range(num_letters): print('') print('Okay, come on!') print('') -print('') + # Logic -while True: +while play: # Get best letter best_option = best_letter(word_resolve) # The guard not to give it back letters_used.append(best_option) + print_words() # Print best letter print('Test with the letter> {letter}'.format( letter=best_option.upper() @@ -68,20 +110,22 @@ while True: question_success = raw_input('I successful? (yes o no): ').lower() clear() if question_success == 'no': + clear() print('') print('Ups!') - print('') - print('') elif question_success == 'yes': - print_pos = ' ' - print_pos_bar = ' ' - print_pos_letters = ' ' - for i in range(num_letters): - print_pos = print_pos + str(i + 1) + ' ' - print_pos_bar = print_pos_bar + '| ' - print_pos_letters = print_pos_letters + word_resolve[i] + ' ' - print(print_pos) - print(print_pos_bar) - print(print_pos_letters) print('') - good_pos = raw_input('Tell me that positions (Example> 2 4 7): ') + print_words(True) + good_pos = raw_input('Tell me that positions (Example> 2 4 7): ').split(' ') + clear() + for pos in good_pos: + word_resolve[int(pos) - 1] = best_option + + # Game over + end = True + for letter in word_resolve: + if letter != '_': + end = False + if end: + play = False + print('Game over :)') \ No newline at end of file