-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceman.py
202 lines (163 loc) · 6.88 KB
/
spaceman.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import random
def load_word():
'''
A function that reads a text file of words and randomly selects one to use
as the secret word from the list.
Returns:
string: The secret word to be used in the spaceman guessing game
'''
f = open('words.txt', 'r')
words_list = f.readlines()
f.close()
word_list = words_list[0].split(' ')
secret_word = random.choice(word_list)
return secret_word
def is_word_guessed(secret_word, letter_guessed):
'''
A function that checks if all the letters of the secret word have been guessed.
Args:
secret_word (string): the random word the user is trying to guess.
letter_guessed (list of strings): list of letters that have been guessed so far.
Returns:
bool: True only if all the letters of secret_word are in letter_guessed,
False otherwise
'''
# Loop through the letters in the secret_word and check if a letter is not in lettersGuessed
# not in explanation: https://stackoverflow.com/questions/10406130/check-if-something-is-not-in-a-list-in-python
# print(secret_word)
# print(letter_guessed)
for letter in secret_word:
# print(letter)
if letter not in letter_guessed:
# print("false")
return False
# print("true")
return True
# pass
def get_guessed_word(secret_word, letter_guessed):
'''
A function that is used to get a string showing the letters
guessed so far in the secret word and underscores for letters that
have not been guessed yet.
Args:
secret_word (string): the random word the user is trying to guess.
letter_guessed (list of strings): list of letters that have been guessed so far.
Returns:
string: letters and underscores. For letters in the word that the
user has guessed correctly, the string should contain the letter at the correct
position. For letters in the word that the user has not yet guessed, shown an _
(underscore) instead.
'''
#Loop through the letters in secret word and build a string that shows the letters
#that have been guessed correctly so far that are saved in letter_guessed and underscores
#for the letters that have not been guessed yet
display_word = ""
for letter in secret_word:
if letter in letter_guessed:
display_word += letter
else:
display_word += "_"
return display_word
# pass
def is_guess_in_word(guess, secret_word):
'''
A function to check if the guessed letter is in the secret word
Args:
guess (string): The letter the player guessed this round
secret_word (string): The secret word
Returns:
bool: True if the guess is in the secret_word, False otherwise
'''
#check if the letter guess is in the secret word
if guess in secret_word:
return True
else:
return False
#pass
# play again: https://stackoverflow.com/questions/17897183/python-starting-the-game-again
def play_again():
while True:
answer = input("Do you want to play again? (Y/n): ")
if not answer or answer.lower() in ('y', 'yes'):
return True
elif answer.lower() in ('n', 'no'):
return False
else:
print("Not a valid answer!")
def spaceman(secret_word):
alphabet = set("abcdefghijklmnopqrstuvwxyz")
guesses_left = 7
guess = ''
guessed = set()
letter_guessed = ""
letters_guessed = []
'''
A function that controls the game of spaceman. Will start spaceman in the command line.
Args:
secret_word (string): the secret word to guess.
'''
#show the player information about the game according to the project spec
print("Welcome to Spaceman!")
print(f"The secret word contains: {len(secret_word)} letters")
print("You have " + str(guesses_left) + " incorrect guesses, please enter one letter per round")
print("-------------------------------------")
# print(secret_word)
while guesses_left > 0:
input_recieved = False
#Ask the player to guess one letter per round and check that it is only one letter
while input_recieved == False:
guess = input("Enter a letter: ").strip()
if len(guess) < 1:
print("You didn't enter anything")
elif len(guess) > 1:
print("Please only enter one letter at a time")
elif guess in letter_guessed:
print("You already guessed that letter")
elif not guess.isalpha():
print("That's not a letter")
else:
input_recieved = True
letters_guessed.append(guess) # add letter to array to keep track of user guesses
letter_guessed += guess
#Check if the guessed letter is in the secret or not and give the player feedback
# print(guess)
# print(secret_word)
if is_guess_in_word(guess, secret_word) == True:
print("Your guess appears in the word!")
else:
print("Sorry, your guess was not in the word, try again")
guesses_left -= 1
#show the guessed word so far
print("guessed word: " + get_guessed_word(secret_word, letter_guessed))
#print how many guesses are left
print("You have " + str(guesses_left) + " incorrect guesses, please enter one letter per round")
# show letters that have not been guessed yet
# about sets: https://stackoverflow.com/questions/57840813/how-to-remove-a-character-from-string-after-every-loop-in-a-while-loop
guessed.add(guess)
print("These letters haven't been guessed yet: " + ''.join(sorted(alphabet - guessed)))
print("-------------------------------------")
#check if the game has been won or lost
if is_word_guessed(secret_word, letter_guessed):
print("You won!")
break;
elif guesses_left == 0:
print("Sorry you didn't win, try again!")
print("Your secret word was" + secret_word)
def main(): #entry point for game, starts game
while True:
spaceman(load_word())
if not play_again():
return
if __name__ == '__main__':
main() # new game starts
exit() # if user does not want to play again
#main()
def test_guessed_word(): #tests to see if letters guessed are in word
assert get_guessed_word(("halloween"), ['a', 'l', 'e', 'n']) == "_all__een"
assert get_guessed_word(("halloween"), ['h','w', 'e', 'x']) == "h____wee_"
def test_word_guessed(): #tests to see if user guesses word correctly
assert is_word_guessed(("better"), ['b', 'e', 't', 'r']) is True
assert is_word_guessed(("better"), ['b', 'e', 't']) is False
def test_is_guess_in_word(): #tests to see if user letter guessed is correct
assert is_guess_in_word(("a"), ('art')) is True
assert is_guess_in_word(("b"), ('art')) is False