Make this a standalone application and add level file support

master
BodgeMaster 2022-12-23 12:18:11 +01:00
parent 9e9fb38b60
commit e3475eefc3
1 changed files with 55 additions and 1 deletions

View File

@ -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<number> | 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")