Skip to content

Commit 44824e8

Browse files
authored
big update
added: saving to file hard mode normal mode better display lives more information collected
1 parent 4e38748 commit 44824e8

File tree

1 file changed

+117
-55
lines changed

1 file changed

+117
-55
lines changed

hangman.py

Lines changed: 117 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,104 @@
22
import random
33
import time
44

5-
# base
6-
word = ''
7-
used = []
8-
correct = 0
9-
fullWord = []
10-
# stats
11-
attempts = 12
125

6+
def reset(): # resets vars for new round
7+
global word, used, correct, fullWord, attempts, condition
8+
word = ''
9+
used = []
10+
correct = 0
11+
fullWord = []
12+
attempts = 12
13+
condition = 0
1314

14-
def info():
15+
16+
def start(): # start function
17+
global mode, autoSave
1518
print('---< commands >---')
1619
print('enter any letter as your guess')
1720
print('enter !guess to guess the whole word')
18-
print('enter !used at any time during the game to see the letters you already used')
19-
print('\n')
21+
print('enter !used to see used letters')
22+
print('enter !end to give and end the game')
23+
print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
24+
reset()
25+
time.sleep(1)
26+
print('do you want to auto save results to a file?')
27+
print('enter y to continue\nenter any key to skip')
28+
option = input('enter your choice: ')
29+
if option == 'y':
30+
autoSave = True
31+
else:
32+
autoSave = False
33+
print('!normal is for normal mode | 12 lives, full word wrong -2 lives')
34+
print('!hard is for hard mode | longer words, 6 lives, full word wrong you die')
35+
mode = input('enter !normal or !hard: ')
36+
main()
37+
38+
39+
def again():
40+
global mode, attempts, endCondition
41+
option = input('enter !save to save your results\nenter !play to play again\nenter !stop to stop\n')
42+
if option == '!play':
43+
reset()
44+
time.sleep(1)
45+
print('!normal is for normal mode | 12 lives, full word wrong -2 lives')
46+
print('!hard is for hard mode | longer words, 6 lives, full word wrong you die')
47+
mode = input('enter !normal or !hard: ')
48+
main()
49+
elif option == '!save' or autoSave is True:
50+
if autoSave is True:
51+
print('auto saved')
52+
file = open('history', 'a')
53+
file.write('\n=-=-=-=-=-=-=-=-=-=-=-=-=-=\n')
54+
file.write(f'{endCondition}\n')
55+
file.write(
56+
f'word: {word[:-1]}\nmode: {mode[1:]}\nlives left: {lives}\nwrong letters: {attempts - lives}\nused letters: {len(used)}\n')
57+
file.write(f'used letter list: {used}\n')
58+
again()
59+
else:
60+
exit()
2061

2162

2263
def stats(): # stats for end of game
23-
wrongLetter()
64+
global mode, attempts, lives, endCondition
65+
if attempts < 0:
66+
attempts = 0
67+
lives = attempts
68+
if mode == '!hard':
69+
attempts = 6
70+
else:
71+
attempts = 12
2472
print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
25-
print(f'word: {word[:-1]}\nlives left: {attempts}\nwrong letters: {wrongGuess}\nused letters: {len(used)}')
73+
if condition == 0: # sets end condition of the game
74+
endCondition = 'you won'
75+
elif condition == 1:
76+
endCondition = 'you lost | you ran out of lives'
77+
elif condition == 2:
78+
endCondition = 'you lost | you guessed the wrong word'
79+
elif condition == 3:
80+
endCondition = 'you lost | you gave up'
81+
else:
82+
endCondition = 'you lost | no lose information'
83+
print(endCondition)
84+
print(
85+
f'word: {word[:-1]}\nmode: {mode[1:]}\nlives left: {lives}\nwrong letters: {attempts - lives}\nused letters: {len(used)}')
86+
print(f'used letter list: {used}')
2687
time.sleep(1)
2788
print('\ncheck out my github: https://github.com/cqb13')
2889
print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
29-
time.sleep(20)
30-
exit()
90+
again()
3191

3292

33-
def wrongLetter(): # finds the amount of wrong letter I think
34-
global wrongGuess
35-
num = 0
36-
wrongGuess = (len(used))
37-
for i in used:
38-
if used[num] in word:
39-
wrongGuess -= 1
40-
else:
41-
pass
42-
num += 1
43-
44-
45-
def options():
46-
global fullWord, word, correct, attempts
93+
def main():
94+
global fullWord, word, correct, attempts, condition
4795
word = linecache.getline('words', random.randrange(0, 8749))
96+
if mode == '!hard':
97+
print('you are on hard mode')
98+
attempts = 6
99+
while len(word) <= 6:
100+
word = linecache.getline('words', random.randrange(0, 8749))
101+
else:
102+
print('you are on normal mode')
48103
wordlength = len(word)
49104
while wordlength != 0: # creates the word visual
50105
fullWord.append('_')
@@ -55,82 +110,89 @@ def options():
55110
print(f'lives: {attempts}')
56111

57112
while attempts > 0:
58-
option = input('enter a guess')
113+
option = input('enter a guess: ')
59114

60115
if option == '!used':
61116
print(f'used letters: {used}')
117+
elif option == '!end':
118+
print('are you sure you want to give up?')
119+
print('enter y to give up\nenter any key to skip')
120+
option = input('enter your choice: ')
121+
if option == 'y':
122+
condition = 3
123+
stats()
124+
else:
125+
print('skipped')
62126
elif option == '!guess': # option to guess the full word
63-
print('if you get the word wrong you will lose.\npress y to continue '
64-
'or x to skip')
65-
option = input('y to continue, or any key to skip')
127+
if mode == '!hard':
128+
print('if incorrect you will lose')
129+
else:
130+
print('if incorrect you will lose 2 lives')
131+
print('enter y to give up\n enter any key to skip')
132+
option = input('enter your choice: ')
66133
word = word[:-1] # something wrong with word, idk anymore
67134
if option == 'y': # makes sure that you actually want to do it
68-
option = input('enter word')
135+
option = input('enter word: ')
69136
if option == word: # checks if the word is right
70-
print('you won')
137+
condition = 0
71138
stats()
139+
elif option != word and mode != '!hard': # checks if you are on normal mode
140+
print('that\'s not the right word | -2 attempts')
141+
attempts -= 2
142+
print(f'lives: {attempts}')
143+
if attempts < 0:
144+
condition = 1
145+
stats()
72146
else:
73-
print('you lost')
147+
condition = 2
74148
stats()
75149
else:
76150
print('skipped')
77151
else:
78152
if len(option) > 1: # checks that you only entered 1 letter
79-
print('enter !full to guess the word')
153+
print('enter !guess to guess the full word')
80154
else:
81155
letterUsed = False # I don't fucking know, it does something I think
82156
num = 0
83-
for i in used: # checks if letter has been used before
157+
for _ in used: # checks if letter has been used before
84158
if option == used[num]:
85159
print(f'letter {option} has already been used')
86160
print('enter !used to see all used letters')
87161
letterUsed = True
88-
pass
89-
else:
90-
pass
91162
num += 1
92163

93164
num = 0
94-
for i in word: # adds letter to its spot in hidden word
165+
for _ in word: # adds letter to its spot in hidden word
95166
if letterUsed is True:
96167
pass
97168
elif option == word[num]: # weird word symbol thing
98169
temp = list(fullWord)
99170
temp[num] = option
100171
fullWord = ''.join(temp)
101172
correct += 1
102-
else:
103-
pass
104173
num += 1
105174

106175
if option not in word and option not in used: # checks if it should take away a life
107176
attempts -= 1
108-
else:
109-
pass
110177

111178
print(fullWord)
112179

113180
if letterUsed is True: # adds letter to used list
114181
pass
115182
else: # adds every letter to list
116183
used.append(option)
117-
if correct == len(word) - 1: # checks if you won
118-
print('you won')
184+
if fullWord == word[:-1]: # checks if you won
185+
condition = 0
119186
stats()
120187
elif attempts <= 0: # checks if you have too many wrong letters
121-
print('you lost')
188+
condition = 1
122189
stats()
123-
else:
124-
pass
125190

126191
print(f'lives: {attempts}')
127192
print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
128193

129194

130195
print('---< welcome to hangman >---')
131-
print('---< made by: cqb13 >---')
132-
print('\n')
133-
time.sleep(1)
134-
info()
196+
print('---< made by: cqb13 >---\n')
135197
time.sleep(1)
136-
options()
198+
start()

0 commit comments

Comments
 (0)