This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardHangman.py
63 lines (57 loc) · 2.03 KB
/
hardHangman.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
def genPos(word, letter):
#number of combinations the letter can be placed in
numPos = 2**(word.count("_"))
#return values
retval = []
for i in range(numPos):
binnum = bin(i).replace("0b","").zfill(word.count("_"))
newstr = word
for j in range(len(binnum)):
if binnum[j:j+1] == "1":
n = find_nth(word, "_", j+1)
newstr2 = newstr[:n] + letter + newstr[n+1:]
newstr=newstr2
retval.append(newstr)
return retval
def find_nth(haystack, needle, n):
cnt = 0
for i in range(len(haystack)):
if haystack[i:i+1] == needle:
cnt += 1
if cnt == n:
return i
return "help"
def similar(word1, word2):
#word1 is similar to word2 if the letters in word1 and word2
#are the same, given that _ is a wildcard
for i in range(len(word1)):
if word1[i:i+1] != "_" and word1[i:i+1] != word2[i:i+1]:
return False
return True
def main():
bigtext = open('6letterwords.txt', 'r')
bigtextline = bigtext.readline()
currentWord = "______"
wordList = bigtextline.split()
while currentWord.count("_") > 0:
print("The current word is: " + currentWord)
ui = raw_input("Guess a letter: ")
print(ui)
possibilities = genPos(currentWord, ui)
possibilities.sort(key=lambda x: x.count(ui), reverse = True)
buckets=[[] for _ in range(len(possibilities))]
for i in range(len(possibilities)):
stop = len(wordList)
j = 0
while j < stop:
if similar(possibilities[i],wordList[j]):
buckets[i].append(wordList[j])
wordList = wordList[:j]+wordList[j+1:]
stop -= 1
j -= 1
j += 1
bigBucketInfo = max(enumerate(buckets), key = lambda tup: len(tup[1]))
wordList = bigBucketInfo[1]
currentWord = possibilities[bigBucketInfo[0]]
if __name__ == "__main__":
main()