This is a follow up after revisions made from this question.
I've separated the program into 2 classes. The HangManData
class is used to store game data such as the word, letters guessed(separating the ones actually in the word from those that arent), remaining number of guesses and the phase of the image.
The second class UI
controls user interaction and sends the data back to the HangManData
class to be stored.
This is definitely a lot more readable compared to the original code. Let me know of any improvements I could make, especially if there is something I've done unnecessarily that could be simplified.
I'll be leaving the unnecessary phases file and word file out to prevent any repetition, refer to original question if required
import random
from phases import hang_phases
class HangManData:
def __init__(self, random_word):
self.word = random_word
self.missed_letters = []
self.successful_letters = []
self.man = hang_phases
self.remaining_chances = 9
def get_man(self):
self.man = hang_phases[self.remaining_chances]
return self.man
def get_word(self):
return self.word
def set_successful_letters(self, letter):
self.successful_letters.append(letter)
def get_successful_letters(self):
return self.successful_letters
def set_missed_letters(self, letter):
self.missed_letters.append(letter)
def get_missed_letters(self):
return self.missed_letters
class UI:
def __init__(self):
self.word_list = open('Words').read().split("\n")
self.word = random.choice(self.word_list).upper()
self.filled = ""
def generate_word(self):
return self.word
def fill_blanks(self, hm):
self.filled = ""
for i in range(0, len(hm.get_word())):
if hm.get_word()[i] in hm.get_successful_letters():
self.filled += hm.get_word()[i]
else:
self.filled += "_"
return self.filled
def display(self, hm):
print(hm.get_man())
print(self.fill_blanks(hm))
print(f"Incorrect Letters Guessed {hm.get_missed_letters()}")
def play(self):
hm = HangManData(self.generate_word())
while True:
self.display(hm)
if hm.get_word() == "":
break
elif "_" not in self.fill_blanks(hm):
print("Congrats!")
break
elif hm.remaining_chances == 0:
print(f"The Word Was {hm.get_word()}")
break
else:
guessed_letter = input("Guess A Letter: ").upper()
if guessed_letter == "QUIT":
print(f"The Word Was {hm.get_word()}")
break
elif guessed_letter in hm.get_missed_letters() or guessed_letter in hm.get_successful_letters():
print("You Already Guessed This Letter")
elif guessed_letter in hm.get_word():
number_of_appearances = hm.get_word().count(guessed_letter)
if number_of_appearances == 1:
print(f"{guessed_letter} Appears Once In The Word")
else:
print(f"{guessed_letter} Appears {number_of_appearances} Times In The Word")
hm.set_successful_letters(guessed_letter)
else:
hm.set_missed_letters(guessed_letter)
hm.remaining_chances -= 1
def main():
play = True
while play:
u = UI()
u.play()
while True:
print("Play Again?")
play_again = input("Y/n > ").upper()
if play_again == "Y":
print("Setting Up...")
break
elif play_again == "N":
print("Goodbye")
play = False
break
else:
print("Invalid Input")
if __name__ == "__main__":
main()