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,8 +65,13 @@ 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):
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 = "" tag = ""
frame = [] frame = []
frames = {} frames = {}
@ -81,6 +89,10 @@ class Hangfish():
frame.append(line.strip()[1:]) frame.append(line.strip()[1:])
if line == "": break if line == "": break
return frames return frames
except FileNotFoundError:
pass
print("Unable to open files.")
return None
def main(): def main():
hangfish = Hangfish("Gen") hangfish = Hangfish("Gen")