Can now open a fallback file for text graphics.

This commit is contained in:
Sheldon Lee 2020-10-14 17:08:11 +00:00
parent e0ed941fcd
commit 53f1b8f791

View File

@ -5,7 +5,8 @@ class Hangfish():
self.word = word self.word = word
# Stores the graphical strings of hangman progress. # Stores the graphical strings of hangman progress.
self.frames = Hangfish.getFrames("hangfish_states.txt") self.frames = Hangfish.getFrames("hangfish_states.txt", "hangfish_states_example.txt")
if self.frames is None: self.frames = {}
# Keeps track of characters guessed. # Keeps track of characters guessed.
self.guessed_indices = [False] * len(word) self.guessed_indices = [False] * len(word)
@ -49,12 +50,14 @@ class Hangfish():
# Get string output of the game. # Get string output of the game.
def getString(self): def getString(self):
# Hangfish status.
string = "" string = ""
frame = self.frames.get("{}".format(self.attempts+1), None) frame = self.frames.get("{}".format(self.attempts+1), None)
string += "Hangfish Status:\n"
if frame is not None: if frame is not None:
for line in frame: for line in frame:
string += line+'\n' string += line+'\n'
# Guess left.
string += "guesses left = {}\n".format(self.guesses-self.attempts) + self.graphical_progress_string + '\n' string += "guesses left = {}\n".format(self.guesses-self.attempts) + self.graphical_progress_string + '\n'
if not self.running: string += self.status_message if not self.running: string += self.status_message
@ -62,25 +65,34 @@ class Hangfish():
# Load graphics from file # Load graphics from file
@staticmethod @staticmethod
def getFrames(filename): def getFrames(filename, alt_filename=""):
file_handle = open(filename) for current_filename in (filename, alt_filename):
tag = "" if current_filename == "": continue;
frame = [] elif current_filename == alt_filename and filename != alt_filename:
frames = {} print("Trying fallback file: \"{}\"".format(alt_filename))
while True: try:
line = file_handle.readline() file_handle = open(current_filename)
if len(frame) > 0 and (not line.startswith("#") or line == ""):
if tag == "":
tag = "Tag: {}".format(len(frames)+1)
frames[tag] = frame
tag = "" tag = ""
frame = [] frame = []
if line.startswith("!"): frames = {}
tag = line.strip()[1:] while True:
elif line.startswith("#"): line = file_handle.readline()
frame.append(line.strip()[1:]) if len(frame) > 0 and (not line.startswith("#") or line == ""):
if line == "": break if tag == "":
return frames 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
def main(): def main():
hangfish = Hangfish("Gen") hangfish = Hangfish("Gen")