-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhang_man.py
131 lines (114 loc) · 3.03 KB
/
hang_man.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
"""
Hangman game in Python
"""
import time
import numpy as np
words = "I love KZ penguin will be always there for the loving bird pragmatic deride ".split()
graphic0 = """
________
| |
|
|
|
|
"""
graphic1 = """
________
| |
| 0
|
|
|
"""
graphic2 = """
________
| |
| 0
| /
|
|
"""
graphic3 = """
________
| |
| 0
| / \
|
|
"""
graphic4 = """
________
| |
| 0
| /|\
|
|
"""
graphic5 = """
________
| |
| 0
| /|\
| |
|
"""
graphic6 = """
________
| |
| 0
| /|\
| |
| /
"""
graphic7 = """
________
| |
| 0
| /|\
| |
| / \
|
"""
hang_graphics = [graphic0, graphic1, graphic2, graphic3, graphic4, graphic5, graphic6, graphic7]
#game settings
#__________________________________________________________________________________________
#rand_word = np.random.choice(words, 1)[0]
rand_word = 'always'
character_remaining = rand_word
num_wrong_guess = 0
display_word = '_' * len(rand_word)
isWinner = False
print "Let's start the fun! :)"
print 'Initial state: %r' % ("|".join(display_word))
print "You are here: ", graphic0
while num_wrong_guess < 7:
guess_char = raw_input("Guess a letter ->")
if len(guess_char) > 1:
print 'Plz insert a single character at a time'
elif len(guess_char) < 1:
print 'Plz insert a single character'
elif guess_char in character_remaining:
character_remaining = character_remaining.replace(guess_char, '', 1)
if not(guess_char in display_word):
ind = rand_word.index(guess_char)
display_word = display_word[:ind] + guess_char + display_word[ind+1:]
elif guess_char in display_word:
ind_disp = display_word.index(guess_char)
ind = rand_word.index(guess_char, ind_disp+1)
display_word = display_word[:ind] + guess_char + display_word[ind+1:]
print 'Good job!'
print 'Current state: %r' % ("|".join(display_word))
print "%r percent completed" % (100.0 - len(character_remaining) * 100 / len(rand_word))
if (len(character_remaining) < 1): #condition for successfully completing the game
isWinner = True
print "Awesome! You won the game!!"
break
elif not(guess_char in character_remaining): #Guessed the wrong character
num_wrong_guess += 1
print "Oops, not the right guess.. :("
print 'Try again please'
print 'Current state: %r' % ("|".join(display_word))
print "Chance(s) remaining: %r more chances" % (7 - num_wrong_guess)
print hang_graphics[num_wrong_guess]
if not(isWinner):
print "Game over! You are hanged!"