-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
154 lines (128 loc) · 4.47 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
import random
# This program lets two players play the game Hangman.
# Player 1 enters a secret word. Then, player 2 has seven wrong guesses before losing the game.
def getSecretWord():
"""Asks Player 1 for secret word, makes sure it is valid, and stores the word in lowercase."""
invalid = True
while invalid:
secretWord = input(secretWordPrompt).strip().lower()
if ('?' in secretWord) or ('\n' in secretWord) or ('\t' in secretWord):
continue
elif len(secretWord) == 0:
continue
else:
invalid = False
return secretWord
def getRandomWord():
"""Gets random word at least 6 characters long from list of common English words"""
wordList = []
with open('4000-most-common-english-words.csv') as file:
for line in file:
if len(line) > 6:
wordList.append(line)
wordList.pop(0)
randomNumber = random.choice(range(len(wordList)))
return wordList[randomNumber]
def displayHangman(wrongGuesses):
"""Display current state of Hangman based on current number of wrong guesses with a blank line on either side."""
print()
if wrongGuesses >= 1:
print(' | ')
if wrongGuesses >= 2:
print(' 0 ')
if wrongGuesses >= 3:
if wrongGuesses == 3:
print(' | ')
elif wrongGuesses == 4:
print('/| ')
else:
print('/|\\')
if wrongGuesses >= 6:
if wrongGuesses == 6:
print('/ ')
else:
print('/ \\')
print()
return
def displaySecretWord(secretWord, guesses):
"""Builds string of the current state of the secret word, with unguessed characters masked as question marks"""
length = len(secretWord)
displayWord = ''
for char in secretWord:
if char.isalpha():
if char in guesses:
displayWord += char
else:
displayWord += '?'
else:
displayWord += char
return displayWord
def displayPreviousGuesses(previousGuesses):
"""Displays the characters guessed so far in sorted order separated by commas"""
print(previousGuessesPrompt, end='')
previousGuesses.sort()
length = len(previousGuesses)
if length == 0:
print()
else:
for i in range(length):
if i < (length - 1):
print(previousGuesses[i], end=', ')
else:
print(previousGuesses[i])
return
def getGuess(previousGuesses):
"""Asks Player 2 for a single character guess and makes sure it is valid"""
invalid = True
while invalid:
guess = input(nextGuessPrompt).strip().lower()
if len(guess) > 1:
print('You can only guess a single character.')
continue
elif len(guess) < 1:
print('You must enter a guess.')
elif guess in previousGuesses:
print('You already guessed the character:', guess)
continue
else:
invalid = False
return guess
# Set-up
secretWordPrompt = 'Please enter a word or phrase to be guessed that does not contain ?: '
previousGuessesPrompt = 'So far you have guessed: '
nextGuessPrompt = 'Please enter your next guess: '
wrongGuesses = 0
guessedList = []
# ------------------------------------
# Player 1 enters secret word
# ------------------------------------
# Toggle to switch between 2 player and random word option.
# secretWord = getSecretWord()
secretWord = getRandomWord()
# Print 30 blank lines to hide word
for line in range(30):
print()
# ------------------------------------
# Player 2 tries to guess secret word
# ------------------------------------
while wrongGuesses < 7:
# Display current state of the game
displayHangman(wrongGuesses)
displayWord = displaySecretWord(secretWord, guessedList)
print(displayWord)
displayPreviousGuesses(guessedList)
# Get guess from Player 2
currentGuess = getGuess(guessedList)
guessedList.append(currentGuess)
# Check if Player 2 has won. If so, congratulate them and exit the loop.
displayWord = displaySecretWord(secretWord, guessedList)
if displayWord == secretWord:
print('You correctly guessed the secret word:', secretWord)
break
# If guess is wrong, add to count.
if currentGuess not in secretWord:
wrongGuesses += 1
else:
# If Player 2 gets 7 wrong guesses, they lose.
displayHangman(wrongGuesses)
print('You failed to guess the secret word:', secretWord)