From e3475eefc322884d6d6ac341e1f437cbb4e87b0a Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Fri, 23 Dec 2022 12:18:11 +0100 Subject: [PATCH] Make this a standalone application and add level file support --- lambdaV.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/lambdaV.py b/lambdaV.py index 4bd71d5..1df104c 100644 --- a/lambdaV.py +++ b/lambdaV.py @@ -522,4 +522,58 @@ def debug_setup(): setup_and_run_task(empty_field, [1,1], cursor_east, "Debug mode", ["step", "left", "right", "take", "repeat", "while", "if"], ["facing north", "facing south", "facing east", "facing west", "in front of wall", "goal reached", "on apple"]) if __name__ == "__main__": - pass + import sys, os, json + + level = 0 + single_file_mode = False + level_file_name = "" + + def wrong_usage(): + print("Usage:\n " + sys.argv[0] + " [l | level_file_name.json]\n\n- Run without arguments to start at level 0.\n- Run with a single argument made of 'l' and a number to start at that level.\n- Run with a file name to start that specific level.") + sys.exit(1) + + if len(sys.argv)>1: + if len(sys.argv)>2: + wrong_usage() + if sys.argv[1][0]=="l": + try: + level=int(sys.argv[1][1:]) + except: + if os.path.isfile(sys.argv[1]): + level_file_name = sys.argv[1] + single_file_mode = True + else: + wrong_usage() + elif os.path.isfile(sys.argv[1]): + level_file_name = sys.argv[1] + single_file_mode = True + else: + wrong_usage() + + if not single_file_mode: + level_file_name = os.path.join("levels", str(level)+".json") + + while os.path.isfile(level_file_name): + level_file = open(level_file_name, "r") + level_raw = level_file.read() + level_file.close() + level_data = json.loads(level_raw) + + cursor_start = "" + if level_data["start heading"]=="north": + cursor_start = cursor_north + elif level_data["start heading"]=="south": + cursor_start = cursor_south + elif level_data["start heading"]=="east": + cursor_start = cursor_east + elif level_data["start heading"]=="west": + cursor_start = cursor_west + else: + print("Unknown start heading!") + sys.exit(1) + setup_and_run_task(level_data["field"], level_data["start position"], cursor_start, level_data["text"], level_data["allowed commands"], level_data["allowed conditions"]) + + if single_file_mode: + break + level+=1 + level_file_name = os.path.join("levels", str(level)+".json")