discord-aquatic-bot/hangfish.py

111 lines
3.8 KiB
Python
Raw Normal View History

2020-09-15 19:00:00 +08:00
class Hangfish():
2020-09-15 19:00:00 +08:00
def __init__(self, word):
2020-09-09 21:08:32 +08:00
# Word to be guessed
self.word = word
# Stores the graphical strings of hangman progress.
self.frames = Hangfish.getFrames("hangfish_states.txt", "hangfish_states_example.txt")
if self.frames is None: self.frames = {}
2020-09-09 21:08:32 +08:00
# 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 += "_"
# Game state
self.running = True
self.status_message = ""
2020-09-25 16:21:13 +08:00
self.guesses = 10
self.attempts = 0
self.letters_guessed = 0
2020-09-09 21:08:32 +08:00
2020-09-25 16:21:13 +08:00
# Guess letter or word.
def guess(self, guess):
2020-09-15 19:00:00 +08:00
if not self.running: return
2020-09-25 16:21:13 +08:00
if len(guess) == 1:
2020-09-15 19:00:00 +08:00
for i in range(len(self.word)):
2020-09-25 16:21:13 +08:00
if self.word[i] == guess:
2020-09-15 19:00:00 +08:00
if not self.guessed_indices[i]:
self.letters_guessed += 1
temp_list = list(self.graphical_progress_string)
2020-09-25 16:21:13 +08:00
temp_list[i] = guess
2020-09-15 19:00:00 +08:00
self.graphical_progress_string = ''.join(temp_list)
self.guessed_indices[i] = True
2020-09-09 21:08:32 +08:00
self.attempts += 1
2020-09-09 21:08:32 +08:00
2020-09-25 16:21:13 +08:00
word_guessed = self.letters_guessed == len(self.word) or guess == self.word
out_of_guesses = self.attempts == self.guesses
2020-09-09 21:08:32 +08:00
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."
2020-09-25 16:21:13 +08:00
# Get string output of the game.
2020-09-09 21:08:32 +08:00
def getString(self):
# Hangfish status.
2020-09-15 19:00:00 +08:00
string = ""
frame = self.frames.get("{}".format(self.attempts+1), None)
string += "Hangfish Status:\n"
if frame is not None:
for line in frame:
string += line+'\n'
# Guess left.
string += "guesses left = {}\n".format(self.guesses-self.attempts) + self.graphical_progress_string + '\n'
2020-09-15 19:00:00 +08:00
if not self.running: string += self.status_message
return string
2020-09-25 16:21:13 +08:00
# Load graphics from file
@staticmethod
def getFrames(filename, alt_filename=""):
for current_filename in (filename, alt_filename):
if current_filename == "": continue;
elif current_filename == alt_filename and filename != alt_filename:
print("Trying fallback file: \"{}\"".format(alt_filename))
try:
file_handle = open(current_filename)
tag = ""
frame = []
frames = {}
while True:
line = file_handle.readline()
if len(frame) > 0 and (not line.startswith("#") or line == ""):
if tag == "":
tag = "Tag: {}".format(len(frames)+1)
frames[tag] = frame
tag = ""
frame = []
if line.startswith("!"):
tag = line.strip()[1:]
elif line.startswith("#"):
frame.append(line.strip()[1:])
if line == "": break
return frames
except FileNotFoundError:
pass
print("Unable to open files.")
return None
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()