-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
187 lines (159 loc) · 3.62 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import random
import os
BOARD = ['''
+--------+
| |
|
|
|
|
|
|
==========
''' , '''
+--------+
| |
| O
|
|
|
|
|
==========
''','''
+--------+
| |
| O
| /
|
|
|
|
==========
''','''
+--------+
| |
| O
| / \\
|
|
|
|
==========
''','''
+--------+
| |
| O
| / \\
| |
|
|
|
==========
''','''
+--------+
| |
| O
| / \\
| |
| /
|
|
==========
''','''
+--------+
| |
| O
| / \\
| |
| / \\
|
|
==========
''']
WORDLIST = 'australia arrow algebra bat board boston brazil banana coriander clam canada croissant crane crowbar dam django \
dinasour dessert eagle fanta fox fuse gamer germany hawk lionardo lumbini lamb mole money manchester mouse mother norway \
nitrogen omnipotent opeth pantera panda pokhara python rock room rat raven rhino silver seal shark sheep setsquare server \
snake spider student swan tentacle toad turnip turkey turtle window white wolf whisper zootopia'.split()
guessed_list = []
def welcome_screen(): #prints the welcome screen
print '''
__
| |__ _____ ____ ____ _____ _____ ____
| | \\\\__ \ / \ / ___\ / \\\\__ \ / \
| Y \/ __ \| | \/ /_/ > Y Y \/ __ \| | \\
|___| (____ /___| /\___ /|__|_| (____ /___| /
\/ \/ \//_____/ \/ \/ \/
******WELCOME TO HANGMAN******
The rules are simple.. you just need to guess the word in 6 tries...
Press Enter To Play
'''
flag = raw_input()
os.system('clear')
return
def get_rand_word(): #gets random word from the wordlist
WordIndex = random.randint(0, len(WORDLIST)-1)
return WORDLIST[WordIndex]
def print_blanks(ans_word, guessed_letter = None):
if guessed_letter == None: #if nothing passed, prints blanks equal to length of the word
print '_ ' * len(ans_word)
return
guessed_list.append(guessed_letter)
for i in range(0, len(ans_word)):
if guessed_letter == ans_word[i]:
print guessed_letter,
elif ans_word[i] in guessed_list:
print ans_word[i],
else:
print "_",
check_if_won(ans_word, guessed_letter)
def check_if_won(ans_word, guessed_letter):
flag = 0
for i in range(0, len(ans_word)):
if ans_word[i] in guessed_list:
flag = flag + 1
if flag >= len(ans_word):
print "\nCongratulation you've guessed correctly"
exit(0)
def get_user_input():
print "\n\nEnter the letter: "
guessed_letter = raw_input()
return guessed_letter
def update_board(count = 0):
print BOARD[count]
heart_symbol = u'\u2764'
heart = 'Lives Remaining: ' + (6 - count) * heart_symbol
print heart
def main():
life = 0
welcome_screen()
ans_word = get_rand_word()
update_board()
print_blanks(ans_word)
while life < 6:
guessed_letter = get_user_input()
if len(guessed_letter) != 1 or guessed_letter not in 'abcdefghijklmnopqrstuvwxyz': # to check if input is valid
os.system('clear')
update_board(life)
print_blanks(ans_word, guessed_letter)
print '\n\nPlease enter a valid character'
continue
elif guessed_letter in guessed_list:
os.system('clear')
update_board(life)
print_blanks(ans_word, guessed_letter)
print '\n\nAlready guessed.Guess again:'
continue
elif guessed_letter in ans_word:
os.system('clear')
update_board(life)
print_blanks(ans_word, guessed_letter)
else:
life = life + 1
os.system('clear')
update_board(life)
print_blanks(ans_word, guessed_letter)
print "\n\n\nSorry you've run out of lives!!!"
print "\n\nThe word was " + ans_word
if __name__ == '__main__':
main()