/
unscrambler.py
80 lines (70 loc) · 2.4 KB
/
unscrambler.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
import requests
import random
words = []
word_dict = [[],[],[]]
def fillWordDict():
word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = requests.get(word_site)
words = response.content.splitlines()
random.shuffle(words);
for word in words:
len_word = len(word)
if (len_word > 3 and len_word < 14):
if (len_word <= 6):
word_dict[0].append(word)
elif (len_word <= 10):
word_dict[1].append(word)
elif (len_word > 10):
word_dict[2].append(word)
return word_dict
def chooseAndPrintWord(d):
index = random.randint(1,int(len(word_dict[d])))
word_to_scramble = word_dict[d][index].lower()
l = list(word_to_scramble)
random.shuffle(l);
scrambled_word = ''.join(l)
return scrambled_word,word_to_scramble
def startGame():
round_num = 1
print " "
difficulty = 0
while True:
level = raw_input("Ready to play Unscrambler? What level? [ 1 (easy), 2 (medium), or 3 (hard) ] ")
if int(level) < 4 and int(level > 0):
difficulty = int(level) - 1
break
else:
print "Please choose a number between 1 and 3, with 3 being the most difficult."
print "Let's begin!"
print "Downloading words..."
word_dict = fillWordDict()
word_data = chooseAndPrintWord(difficulty)
print "Your word is: " + word_data[0]
answer = word_data[1]
attempt = 0
while (True):
user_input = raw_input("Answer: ")
attempt += 1
if (user_input == answer):
print "Correct!!"
return True
elif (user_input == "Q" or user_input == "q"):
print "Quitting..."
break
elif (attempt == 3 or attempt>5):
print "Try again! or type Q to quit"
elif (attempt == 5):
tip = raw_input("Would you like a hint? y/n ")
if (tip.upper() == 'Y'):
print "The first letter is " + answer[0]
else:
print "Try again!"
while (startGame()):
play_again = raw_input("Would you like to play again? y/n ")
if (play_again.upper() == 'N'):
print "Okay, shutting down..."
break
else:
words = []
word_dict = [[],[],[]]
print "New game beginning..."