-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangManGame.py
117 lines (96 loc) · 3.1 KB
/
hangManGame.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import random
import string
WORDLIST_FILENAME = "words.txt"
def loadWords():
"""
Returns a list of valid words. Words are strings of lowercase letters.
Depending on the size of the word list, this function may
take a while to finish.
"""
print ("Loading word list from file...")
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r', 0)
# line: string
line = inFile.readline()
# wordlist: list of strings
wordlist = string.split(line)
print (" "), (len(wordlist)), ("words loaded.")
return wordlist
def chooseWord(wordlist):
"""
wordlist (list): list of words (strings)
Returns a word from wordlist at random
"""
return random.choice(wordlist)
# end of helper code
# -----------------------------------
def isWordGuessed(secretWord, lettersGuessed):
if lettersGuessed==[]:
return False
else:
for i in range(len(secretWord)):
if lettersGuessed in secretWord[i]:
return True
else:
x= False
return x
def getGuessedWord(secretWord, lettersGuessed):
w=[]
x=''
i=0
for i in range(len(secretWord)):
if secretWord[i] in lettersGuessed:
w.append(secretWord[i])
else:
w.append('_ ')
x=x+w[i]
return x
def getAvailableLetters(lettersGuessed):
LL=''
L=string.ascii_lowercase
if lettersGuessed == []:
return L
else:
for i in range(len(L)):
if L[i] not in lettersGuessed:
LL=LL+(L[i])
return LL
def hangman(secretWord):
lenSw=len(secretWord)
word='_'*lenSw
mistakesMade=0
lettersGuessed=[]
availableLetters=string.ascii_lowercase
print ('Welcome to the game, Hangman!')
print ('I am thinking of a word that is ' + str(lenSw) + ' letters long.')
print (12*'-')
while word != secretWord and mistakesMade <8:
print ('You have ' + str(8-mistakesMade) + ' guesses left.')
availableLetters = getAvailableLetters(lettersGuessed) #show left letters
print ('Available letters: ' + availableLetters)
print ('Please guess a letter: ') ,
letter=str(raw_input().lower())
if letter in lettersGuessed:
print ("Oops! You've already guessed that letter: " + word)
print (12*'-')
else:
lettersGuessed.append(letter)
word=getGuessedWord(secretWord, lettersGuessed) # show progress
result=isWordGuessed(secretWord, letter) #check the letter
if result == True:
print ('Good guess: ' + word)
print (12*'-')
else:
print ('Oops! That letter is not in my word: ' + word)
print (12*'-')
mistakesMade+=1
if word == secretWord:
print ('Congratulations, you won!')
break
elif mistakesMade==8:
print ('Sorry, you ran out of guesses. The word was ' + secretWord +'.')
# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
wordlist = loadWords()
secretWord=chooseWord(wordlist)
hangman(secretWord)