Compare commits

...

7 Commits

Author SHA1 Message Date
BodgeMaster c59f7d7e39 Start adding levels 2023-01-06 15:11:30 +01:00
BodgeMaster ab0912b968 Only print "Available conditons:" section if conditions are available 2023-01-06 15:10:45 +01:00
BodgeMaster ea4e8f0248 Move the delay into setup_and_run_task() 2023-01-06 15:10:12 +01:00
BodgeMaster f8e316bdc4 Repeat the same level until success 2022-12-23 12:56:57 +01:00
BodgeMaster e814cfe21d Replace debug function with debug level 2022-12-23 12:20:48 +01:00
BodgeMaster e3475eefc3 Make this a standalone application and add level file support 2022-12-23 12:19:58 +01:00
BodgeMaster 9e9fb38b60 Make lists of allowed commands/conditions print properly 2022-12-23 12:17:21 +01:00
13 changed files with 391 additions and 8 deletions

View File

@ -514,13 +514,70 @@ def setup_and_run_task(start_field, start_position, start_heading, text, allowed
field = start_field
draw_field()
print("\n"+text)
print("\nAllowed commands: "+" ,".join(allowed_commands))
print("\nAvailable conditions: "+" ,".join(allowed_conditions))
print(evaluate_parser_result(parse_code(input("\nCode: "), allowed_commands, allowed_conditions)))
#TODO: return information about success or failure
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"])
print("\nAllowed commands: "+", ".join(allowed_commands))
if len(allowed_conditions)>0:
print("\nAvailable conditions: "+", ".join(allowed_conditions))
result = evaluate_parser_result(parse_code(input("\nCode: "), allowed_commands, allowed_conditions))
print(result)
time.sleep(3)
if result=="Success!":
return True
return False
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)
while not setup_and_run_task(level_data["field"].copy(), level_data["start position"].copy(), cursor_start, level_data["text"], level_data["allowed commands"].copy(), level_data["allowed conditions"].copy()):
pass
if single_file_mode:
break
level+=1
level_file_name = os.path.join("levels", str(level)+".json")

22
levels/0.json Normal file
View File

@ -0,0 +1,22 @@
{
"field": [
" _________________ ",
"| |",
"| |",
"| |",
"| $ |",
"| |",
"| |",
"| |",
"| |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 4, 4],
"start heading": "east",
"text": "→ This should show up as a roughly square box with a cursor and a $ inside it.\n→ The cursor is indicated by one of these: Λ V > <\n'#' represents a wall, 'ó' represents an apple and '$' represents the goal\nthat is to be reached.\n→ With every level, there is a list of available commands and conditions\nwhich can be used in that level.\n→ Step onto the goal to continue.",
"allowed commands": [
"step"
],
"allowed conditions": [
]
}

24
levels/1.json Normal file
View File

@ -0,0 +1,24 @@
{
"field": [
" _________________ ",
"| |",
"| ### |",
"| #$# |",
"| #### # |",
"| # # |",
"| ###### |",
"| |",
"| |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 4, 5],
"start heading": "east",
"text": "[Level 1]\n\nΛV has three commands to control movement:\n→ The `step` command moves the cursor one step in the direction it is pointing.\n→ The `left` and `right` commands turn the cursor 90° in that direction.",
"allowed commands": [
"step",
"left",
"right"
],
"allowed conditions": [
]
}

24
levels/2.json Normal file
View File

@ -0,0 +1,24 @@
{
"field": [
" _________________ ",
"| $ |",
"|#################|",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 1, 1],
"start heading": "east",
"text": "[Level 2]\n\nThat's quite simple but it can get tedious really quickly.",
"allowed commands": [
"step",
"left",
"right"
],
"allowed conditions": [
]
}

25
levels/3.json Normal file
View File

@ -0,0 +1,25 @@
{
"field": [
" _________________ ",
"| $ |",
"|#################|",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 1, 1],
"start heading": "east",
"text": "[Level 3]\n\nInstead, we can use a loop to repeat things.\n\nΛV has a simplified \"repeat\" loop for demonstration purposes.\nHere is an example of how to use it: repeat<6>(left)",
"allowed commands": [
"step",
"left",
"right",
"repeat"
],
"allowed conditions": [
]
}

25
levels/4.json Normal file
View File

@ -0,0 +1,25 @@
{
"field": [
" _________________ ",
"| # |",
"| # |",
"|# # |",
"| # # |",
"| # # |",
"| # # |",
"| # # |",
"| # $# |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 1, 1],
"start heading": "east",
"text": "[Level 4]\n\nYou can put multiple commands inside a loop.\nThis can be used to repeat a sequence of commands.\n\nExample: repeat<10>(left step left)",
"allowed commands": [
"step",
"left",
"right",
"repeat"
],
"allowed conditions": [
]
}

25
levels/5.json Normal file
View File

@ -0,0 +1,25 @@
{
"field": [
" _________________ ",
"| # #$# |",
"| # # # |",
"| # # # |",
"| # # # |",
"| # # # |",
"| # # # |",
"| ###### # |",
"| # |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 1, 1],
"start heading": "south",
"text": "[Level 5]\n\nYou can even put loops inside other loops.\n\nExample: repeat<10>(step repeat<3>(left))",
"allowed commands": [
"step",
"left",
"right",
"repeat"
],
"allowed conditions": [
]
}

25
levels/6.json Normal file
View File

@ -0,0 +1,25 @@
{
"field": [
" _________________ ",
"| # # # # $|",
"| # # # # # # # ##|",
"| # # # # # # # # |",
"| # # # # # # # # |",
"| # # # # # # # # |",
"| # # # # # # # # |",
"| # # # # # # # # |",
"| # # # # |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 1, 1],
"start heading": "east",
"text": "[Level 6]\n\nLet's do that again but bigger...",
"allowed commands": [
"step",
"left",
"right",
"repeat"
],
"allowed conditions": [
]
}

24
levels/7.json Normal file
View File

@ -0,0 +1,24 @@
{
"field": [
" _________________ ",
"| $ |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 6, 8],
"start heading": "north",
"text": "[Level 7]\n\nNow in the real world, loops usually don't run a predefined number of times.\nOne type of loops found in most programming languages is the \"while loop\".\nIt runs while a given condition is true. Conditions can be negated by adding !\nin front of them.\nExample: while<!I have cookie>(fix_that)",
"allowed commands": [
"step",
"while"
],
"allowed conditions": [
"in front of wall"
]
}

27
levels/8.json Normal file
View File

@ -0,0 +1,27 @@
{
"field": [
" _________________ ",
"| # |",
"| # ############# |",
"| # # # |",
"| # #$ # # |",
"| # ########### # |",
"| # # |",
"| ############### |",
"| |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 1, 1],
"start heading": "south",
"text": "Let's do the same thing but more complex...",
"allowed commands": [
"step",
"left",
"right",
"while"
],
"allowed conditions": [
"in front of wall",
"goal reached"
]
}

35
levels/9.json Normal file
View File

@ -0,0 +1,35 @@
{
"field": [
" _________________ ",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 1, 1],
"start heading": "east",
"text": "TODO: Tell the player that loops will only check their condition at the start of the loop",
"allowed commands": [
"step",
"left",
"right",
"take",
"repeat",
"while",
"if"
],
"allowed conditions": [
"facing north",
"facing south",
"facing east",
"facing west",
"in front of wall",
"goal reached",
"on apple"
]
}

35
levels/debug.json Normal file
View File

@ -0,0 +1,35 @@
{
"field": [
" _________________ ",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| $|",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 1, 1],
"start heading": "east",
"text": "Debug mode",
"allowed commands": [
"step",
"left",
"right",
"take",
"repeat",
"while",
"if"
],
"allowed conditions": [
"facing north",
"facing south",
"facing east",
"facing west",
"in front of wall",
"goal reached",
"on apple"
]
}

35
levels/default.json Normal file
View File

@ -0,0 +1,35 @@
{
"field": [
" _________________ ",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
" ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ "
],
"start position": [ 1, 1],
"start heading": "east",
"text": "",
"allowed commands": [
"step",
"left",
"right",
"take",
"repeat",
"while",
"if"
],
"allowed conditions": [
"facing north",
"facing south",
"facing east",
"facing west",
"in front of wall",
"goal reached",
"on apple"
]
}