hangman-resolver/Hangman-bot.py

131 lines
3.3 KiB
Python
Raw Normal View History

2016-11-25 11:44:45 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import operator
import os
2016-11-26 17:12:18 +01:00
# Start Functions
2016-11-25 11:44:45 +01:00
def clear():
2016-11-26 17:12:18 +01:00
'''
Clear console
'''
2016-11-25 11:44:45 +01:00
os.system('cls' if os.name=='nt' else 'clear')
2016-11-26 17:12:18 +01:00
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('')
2016-11-25 11:44:45 +01:00
def best_letter(word_resolve):
2016-11-26 17:12:18 +01:00
'''
Search best letter
Arguments:
word_resolve - list with word resolve
'''
2016-11-25 11:44:45 +01:00
# Read all words and count letters
letters = dict()
ignore = ('\xb1', '\xc3', '\xb6', '.', '\xae')
2016-11-26 17:12:18 +01:00
words = open('words.txt')
2016-11-25 11:44:45 +01:00
# Read alls words for dictionary
for word in words.readlines():
# Just consider which have the same length
2016-11-26 17:12:18 +01:00
if len(word_resolve) == len(word.strip()):
2016-11-25 11:44:45 +01:00
l_word = list(word.strip().lower())
2016-11-26 17:12:18 +01:00
# 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
2016-11-25 11:44:45 +01:00
# Count the letters
2016-11-26 17:12:18 +01:00
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
2016-11-25 11:44:45 +01:00
# Sort
sorted_letters = sorted(
letters.items(), key=operator.itemgetter(1), reverse=True
)
# Best letter
return sorted_letters[0][0]
2016-11-26 17:12:18 +01:00
# End Functions
# Start
2016-11-25 11:44:45 +01:00
clear()
print('Hangman Bot 1.0v')
print(' _________ ')
print('| | ')
print('| 0 ')
print('| /|\\ ')
print('| / \\ ')
print('| ')
print('| ')
# Get num letters and make list resolve
num_letters = input('Number of letters: ')
clear()
2016-11-26 17:12:18 +01:00
play = True
2016-11-25 11:44:45 +01:00
word_resolve = list()
letters_used = list()
for pos in range(num_letters):
word_resolve.append('_')
print('')
print('Okay, come on!')
print('')
2016-11-26 17:12:18 +01:00
2016-11-25 11:44:45 +01:00
# Logic
2016-11-26 17:12:18 +01:00
while play:
2016-11-25 11:44:45 +01:00
# Get best letter
best_option = best_letter(word_resolve)
# The guard not to give it back
letters_used.append(best_option)
2016-11-26 17:12:18 +01:00
print_words()
2016-11-25 11:44:45 +01:00
# Print best letter
print('Test with the letter> {letter}'.format(
letter=best_option.upper()
))
# Save successes
print('')
question_success = raw_input('I successful? (yes o no): ').lower()
clear()
if question_success == 'no':
2016-11-26 17:12:18 +01:00
clear()
2016-11-25 11:44:45 +01:00
print('')
print('Ups!')
elif question_success == 'yes':
print('')
2016-11-26 17:12:18 +01:00
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 :)')