-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
103 lines (77 loc) · 2.09 KB
/
hangman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import requests
URL = "http://upe.42069.fun/jizyr"
most_common_letters = ['e', 't', 'o', 'i', 'a', 'n', 's', 'h', 'r', 'l', 'u', 'd', 'y', 'g', 'm', 'c', 'w', 'p', 'k', 'f', 'b', 'v', 'j', 'z', 'q', 'x']
f = open("20k.txt", "r")
dictionary = []
for line in f:
dictionary.append(line.strip())
def getWords(state):
words = state.split(' ')
for i in range(len(words)):
for c in words[i]:
if c != '_' and not c.isalpha():
words[i] = words[i].replace(c, '')
return words
unused = [True]*26
def chooseLetter(words):
letter_count = [0]*26
for word in words:
size = len(word)
for w in dictionary:
if len(w) == size:
shouldContinue = False
for i in range(size):
if word[i] != w[i] and word[i] != '_':
shouldContinue = True
break
if shouldContinue:
continue
for i in range(size):
if word[i] == '_' and unused[ord(w[i]) - ord('a')]:
letter_count[ord(w[i]) - ord('a')] += 1
max_count = max(letter_count)
max_letters = []
for i, count in enumerate(letter_count):
if count == max_count:
max_letters.append(chr(i + ord('a')))
for l in most_common_letters:
if l in max_letters and unused[ord(l) - ord('a')]:
unused[ord(l) - ord('a')] = False
return l
for l in most_common_letters:
if unused[ord(l) - ord('a')]:
unused[ord(l) - ord('a')] = False
return l
def addWords(lyrics):
words = lyrics.split()
for i, word in enumerate(words):
words[i] = word.strip()
for i in range(len(words)):
word = words[i]
for c in word:
if not c.isalpha():
word = word.replace(c, '')
words[i] = word
for word in words:
with open("20k.txt", "a") as myfile:
myfile.write(word + "\n")
while(True):
r = requests.get(url=URL)
data = r.json()
print(data)
games = 0
while(True):
words = getWords(data['state'])
guess = {}
guess['guess'] = chooseLetter(words)
print(guess['guess'])
r = requests.post(url=URL, data=guess)
data = r.json()
print(data)
games = data['games']
if data['status'] == 'FREE' or data['status'] == 'DEAD':
unused = [True]*26
addWords(data['lyrics'])
break
if games == 100:
break