discord-aquatic-bot/hangfish.py

71 lines
2.1 KiB
Python
Raw Normal View History

2020-09-15 19:00:00 +08:00
class Hangfish():
discord_instances = {}
def __init__(self, word):
2020-09-09 21:08:32 +08:00
# Word to be guessed
self.word = word
# Stores the graphical string of hangman progress.
self.graphical_string = ""
# Keeps track of characters guessed.
self.guessed_indices = [False] * len(word)
# Output of characters guessed
self.graphical_progress_string = ""
for i in range(len(word)):
self.graphical_progress_string += "_"
2020-09-15 19:00:00 +08:00
self.guesses = 26
2020-09-09 21:08:32 +08:00
self.letters_guessed = 0
# Game state
self.running = True
self.status_message = ""
def guess(self, c):
2020-09-15 19:00:00 +08:00
if not self.running: return
if len(c) == 1:
for i in range(len(self.word)):
if self.word[i] == c:
if not self.guessed_indices[i]:
self.letters_guessed += 1
temp_list = list(self.graphical_progress_string)
temp_list[i] = c
self.graphical_progress_string = ''.join(temp_list)
self.guessed_indices[i] = True
2020-09-09 21:08:32 +08:00
self.guesses -= 1
2020-09-15 19:00:00 +08:00
word_guessed = self.letters_guessed == len(self.word) or c == self.word
2020-09-09 21:08:32 +08:00
out_of_guesses = self.guesses == 0
if word_guessed or out_of_guesses:
self.running = False
if word_guessed:
self.status_message = "Word Guessed."
elif out_of_guesses:
self.status_message = "Out of Guesses."
def getString(self):
2020-09-15 19:00:00 +08:00
string = ""
string += self.graphical_string + 'guesses left = {}\n'.format(self.guesses) + self.graphical_progress_string + '\n'
if not self.running: string += self.status_message
return string
2020-09-09 21:08:32 +08:00
def main():
2020-09-15 19:00:00 +08:00
hangfish = Hangfish("Gen")
2020-09-15 19:00:00 +08:00
print(hangfish.getString())
while (hangfish.running):
try:
letter = str(input("Enter Letter\n"))[0]
2020-09-15 19:00:00 +08:00
hangfish.guess(letter)
print(hangfish.getString())
except IndexError:
print("Please enter a letter.")
if __name__ == "__main__":
main()