-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-hangman
More file actions
198 lines (172 loc) · 4.52 KB
/
python-hangman
File metadata and controls
198 lines (172 loc) · 4.52 KB
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
188
189
190
191
192
193
194
195
196
197
198
from random import randint
from os import system
from sys import platform
HANGMAN = [
'''
_____
| |
|
|
|
|
__|__
''',
'''
_____
| |
O |
|
|
|
__|__
''',
'''
_____
| |
O |
| |
|
|
__|__
''',
'''
_____
| |
O |
/| |
|
|
__|__
''',
'''
_____
| |
O |
/|\ |
|
|
__|__
''',
'''
_____
| |
O |
/|\ |
/ |
|
__|__
''',
'''
_____
| |
O |
/|\ |
/ \ |
|
__|__
'''
]
def clear():
if platform == "linux" or platform == "linux2":
a = system('clear')
elif platform == "win32":
a = system('cls')
# This function adds blanks to the randomly selected word
# Returns a dictionary file with a word with blanks, a display word
# with spaced blanks and the number of blanks
def add_blanks(word):
total_blanks = len(word) // 2
word = list(word) # converting string to list for mutation
display_word = list(word)
count = 0
for i in range(0, total_blanks):
blank = randint(0, len(word) - 1)
word[blank] = '_'
display_word[blank] = ' _ '
for char in word:
if char == "_":
count += 1
word = "".join(word) # converting mutated list back to string
display_word = "".join(display_word)
return {"word": word, "display_word": display_word, "count": count}
# Checks the input to see if any of the blanks match the guess
def check_input(word, word_blanks, guess):
word = list(word)
word_blanks = list(word_blanks)
count = 0
for i in range(0, len(word)):
if word[i].lower() == guess.lower() and word_blanks[i] == "_":
word_blanks[i] = word[i]
count += 1
else:
continue
word_blanks = "".join(word_blanks)
data = {"word": word_blanks, "count": count}
return data
# Randomly selects a word from a predefined list file
def get_word():
words = ['test', 'testing', 'Ninad', 'anything', 'randomword']
word = words[randint(0, len(words) - 1)]
length = len(word)
blanks = add_blanks(word)
word_data = {
"word": word,
"word_blanks": blanks["word"],
"total_blanks": blanks["count"],
"display_word": blanks["display_word"],
"length": length
}
return word_data
# Main function - Does a lot of shitz
def main():
word = get_word()
blanks_left = word["total_blanks"]
word_blnk = word["word_blanks"]
display_word = word["display_word"]
tries = 6
i = 0
clear()
print("*" * 20, "Begin Hangman", "*" * 20)
print("\n\nTry to guess the word: \n\n", display_word)
print("\n\n")
while tries >= 0:
if blanks_left == 0:
print(HANGMAN[i])
print("Congrats! You successfuly completed it!")
print("The word is: ", word["word"])
print("You saved the hangman!")
break
print(HANGMAN[i])
if tries == 0:
print(tries, " tries left.")
print("Hangman died. :( ")
print("Game over")
break
guess = input("Enter your guess (one letter only): ")
if len(guess) > 1:
clear()
print("Please enter only one letter at a time. Here, try again")
continue
else:
clear()
word_chk = check_input(word["word"], word_blnk, guess)
blanks_left -= word_chk["count"]
if word_chk["count"] > 0:
if blanks_left == 0:
continue
else:
print("\nGreat job! Correct guess!")
print("\nRemaining blanks: ", word_chk["word"])
print("\n", tries, " attempts left")
word_blnk = word_chk["word"]
continue
elif word_chk["count"] == 0 and tries > 0:
tries -= 1
i += 1
print("\nThe letter ", guess, " did not match any blank.")
print("\nRemaining blanks: ", word_chk["word"])
print("\n", tries, " tries left")
continue
else:
pass
main()