{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "9adfcb20", "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 4, "id": "de910107", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'if > then'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stmt=\"if > then\"\n", "s = stmt.lower().strip()\n", "# 1) strip ALL balanced <…>\n", "while s.startswith(\"<\") and s.endswith(\">\"):\n", " s = s[1:-1].strip()\n", "# 2) strip stray unmatched < or >\n", "while s.startswith(\"<\") and not s.endswith(\">\"):\n", " s = s[1:].strip()\n", "while s.endswith(\">\") and not s.startswith(\"<\"):\n", " s = s[:-1].strip()\n", " \n", "s" ] }, { "cell_type": "code", "execution_count": 20, "id": "af367513", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n", "the stmt was this if < and > then and parsed was this if < and > then\n" ] }, { "ename": "SyntaxError", "evalue": "'return' outside function (234319892.py, line 51)", "output_type": "error", "traceback": [ " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[20]\u001b[39m\u001b[32m, line 51\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mreturn {\"kind\": \"block\", \"block\": block_id}\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m 'return' outside function\n" ] } ], "source": [ "import re\n", "\n", "s = \"if > then\"\n", "\n", "m_touch = re.fullmatch(r\"touching\\s*\\[\\s*([^\\]]+)\\s*v\\]\\??\", s, re.IGNORECASE)\n", "print(m_touch)\n", "if m_touch:\n", " sprite = m_touch.group(1).strip()\n", " val = {'mouse-pointer':'_mouse_', 'edge':'_edge_'}.get(sprite, sprite)\n", " mid = _register_block(\n", " \"sensing_touchingobjectmenu\", parent_key, True, pick_key_func, all_generated_blocks,\n", " fields={\"TOUCHINGOBJECTMENU\":[val, None]}\n", " )\n", " bid = _register_block(\n", " \"sensing_touchingobject\", parent_key, False, pick_key_func, all_generated_blocks,\n", " inputs={\"TOUCHINGOBJECTMENU\":[1, mid]}\n", " )\n", " #all_generated_blocks[mid][\"parent\"] = bid \n", " print(bid)\n", " #return {\"kind\":\"block\",\"block\":bid}\n", "\n", "s = stmt.lower().strip()\n", "#s = strip_outer_angle_brackets(s)\n", "# 1) strip ALL balanced <…>\n", "while s.startswith(\"<\") and s.endswith(\">\"):\n", " s = s[1:-1].strip()\n", "# 2) strip stray unmatched < or >\n", "while s.startswith(\"<\") and not s.endswith(\">\"):\n", " s = s[1:].strip()\n", "while s.endswith(\">\") and not s.startswith(\"<\"):\n", " s = s[:-1].strip()\n", " \n", "print(f\"the stmt was this {stmt} and parsed was this {s}\")\n", "# 1a) Comparisons with explicit angle wrappers: < (...) op (...) >\n", "m = re.fullmatch(\n", " r\"\\s*<\\s*(.+?)\\s*(?P<|=|>)\\s*(.+?)\\s*>\\s*\",\n", " s,\n", " re.VERBOSE\n", ")\n", "if m:\n", " left_txt, right_txt = m.group(1), m.group(3)\n", " operand1_obj = parse_reporter_or_value(unparen(left_txt), None, pick_key_func, all_generated_blocks)\n", " operand2_obj = parse_reporter_or_value(unparen(right_txt), None, pick_key_func, all_generated_blocks)\n", " op_map = {'<': 'operator_lt', '=': 'operator_equals', '>': 'operator_gt'}\n", " \n", " inputs = {\"OPERAND1\": operand1_obj, \"OPERAND2\": operand2_obj}\n", " block_id = _register_block(op_map[m.group('op')], parent_key, False, pick_key_func, all_generated_blocks, inputs=inputs)\n", " # Set parents for nested inputs\n", " if operand1_obj.get(\"kind\") == \"block\": all_generated_blocks[operand1_obj[\"block\"]][\"parent\"] = block_id\n", " if operand2_obj.get(\"kind\") == \"block\": all_generated_blocks[operand2_obj[\"block\"]][\"parent\"] = block_id\n", " return {\"kind\": \"block\", \"block\": block_id}\n", "# 1b) Simple comparisons without angle wrappers: A op B\n", "m_simple = re.fullmatch(r\"\\s*(.+?)\\s*(?P<|=|>)\\s*(.+?)\\s*\", s)\n", "if m_simple:\n", " left_txt, right_txt = m_simple.group(1), m_simple.group(3)\n", " operand1_obj = parse_reporter_or_value(unparen(left_txt), None, pick_key_func, all_generated_blocks)\n", " operand2_obj = parse_reporter_or_value(unparen(right_txt), None, pick_key_func, all_generated_blocks)\n", " op_map = {'<': 'operator_lt', '=': 'operator_equals', '>': 'operator_gt'}\n", " \n", " inputs = {\"OPERAND1\": operand1_obj, \"OPERAND2\": operand2_obj}\n", " block_id = _register_block(op_map[m_simple.group('op')], parent_key, False, pick_key_func, all_generated_blocks, inputs=inputs)\n", " if operand1_obj.get(\"kind\") == \"block\": all_generated_blocks[operand1_obj[\"block\"]][\"parent\"] = block_id\n", " if operand2_obj.get(\"kind\") == \"block\": all_generated_blocks[operand2_obj[\"block\"]][\"parent\"] = block_id\n", " return {\"kind\": \"block\", \"block\": block_id}" ] }, { "cell_type": "code", "execution_count": 23, "id": "3a315968", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[TEST] if then\n", "→ Extracted: 'mouse down?'\n", " Expected: 'mouse down?'\n", " ✅ PASS\n", "\n", "[TEST] if > then\n", "→ Extracted: 'not '\n", " Expected: 'not '\n", " ✅ PASS\n", "\n", "[TEST] if < and > then\n", "→ Extracted: 'mouse down?> and > or > >> then\n", "→ Extracted: 'mouse down?>> or '\n", " ❌ FAIL\n", "\n", "[TEST] if <(x position) < (100)> then\n", "→ Extracted: '<(x position) < (100)> then'\n", " Expected: '(x position) < (100)'\n", " ❌ FAIL\n", "\n" ] } ], "source": [ "def extract_condition_balanced(stmt):\n", " stmt = stmt.strip()\n", " if \"if \" in stmt.lower():\n", " stmt = stmt.lower().split(\"if\", 1)[1].strip()\n", "\n", " # Handle bracket balancing\n", " if stmt.startswith(\"<\"):\n", " count = 0\n", " condition = \"\"\n", " for i, ch in enumerate(stmt):\n", " if ch == \"<\":\n", " count += 1\n", " elif ch == \">\":\n", " count -= 1\n", " condition += ch\n", " if count == 0:\n", " break\n", "\n", " # Strip outer brackets recursively\n", " inner = condition\n", " while inner.startswith(\"<\") and inner.endswith(\">\"):\n", " inner = inner[1:-1].strip()\n", "\n", " return inner\n", " return stmt.strip()\n", "\n", "test_cases = [\n", " (\"if then\", \"mouse down?\"),\n", " (\"if > then\", \"not \"),\n", " (\"if < and > then\", \"mouse down? and touching [edge v]?\"),\n", " (\"if <<> or > >> then\", \"mouse down? or not \"),\n", " (\"if <(x position) < (100)> then\", \"(x position) < (100)\"),\n", "]\n", "\n", "for stmt, expected in test_cases:\n", " result = extract_condition_balanced(stmt)\n", " print(f\"[TEST] {stmt}\\n→ Extracted: '{result}'\\n Expected: '{expected}'\\n {'✅ PASS' if result == expected else '❌ FAIL'}\\n\")\n" ] }, { "cell_type": "code", "execution_count": 38, "id": "34ddb52b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Testing improved function ---\n", "[TEST] if then\n", "→ Extracted: 'mouse down?'\n", " Expected: 'mouse down?'\n", " ✅ PASS\n", "\n", "[TEST] repeat until \n", "→ Extracted: 'touching [edge v]?'\n", " Expected: 'touching [edge v]?'\n", " ✅ PASS\n", "\n", "[TEST] if then\n", "→ Extracted: 'key [left arrow v] pressed?'\n", " Expected: 'key [left arrow v] pressed?'\n", " ✅ PASS\n", "\n", "[TEST] if then\n", "→ Extracted: 'touching [edge v]?'\n", " Expected: 'touching [edge v]?'\n", " ✅ PASS\n", "\n", "[TEST] if < and > then\n", "→ Extracted: ' and '\n", " Expected: 'mouse down? and touching [edge v]?'\n", " ❌ FAIL\n", "\n", "[TEST] if <(x position) < (100)> then\n", "→ Extracted: '(x position) < (100)'\n", " Expected: '(x position) < (100)'\n", " ✅ PASS\n", "\n", "[TEST] if <> then\n", "→ Extracted: ''\n", " Expected: 'mouse down?'\n", " ❌ FAIL\n", "\n", "[TEST] if > then\n", "→ Extracted: 'not<>'\n", " Expected: 'not<>'\n", " ✅ PASS\n", "\n", "[TEST] if <<> or <>> then\n", "→ Extracted: '<> or <>'\n", " Expected: '<> or <>'\n", " ✅ PASS\n", "\n", "[TEST] if <> then\n", "→ Extracted: ''\n", " Expected: ''\n", " ✅ PASS\n", "\n", "[TEST] if mouse down? then\n", "→ Extracted: 'mouse down?'\n", " Expected: 'mouse down?'\n", " ✅ PASS\n", "\n", "[TEST] \n", "→ Extracted: 'mouse down?'\n", " Expected: 'mouse down?'\n", " ✅ PASS\n", "\n" ] } ], "source": [ "import re\n", "\n", "def extract_condition_balanced(stmt):\n", " # 1. Remove \"if\" and \"then\"\n", " stmt = stmt.strip()\n", " if stmt.lower().startswith(\"if \"):\n", " stmt = stmt[3:].strip()\n", " if stmt.lower().startswith(\"repeat until\"):\n", " stmt = stmt[12:].strip()\n", " if stmt.lower().endswith(\" then\"):\n", " stmt = stmt[:-5].strip()\n", "\n", " # Helper to detect and strip single outer balanced angle brackets\n", " def unwrap_balanced(s):\n", " if s.startswith(\"<\") and s.endswith(\">\"):\n", " depth = 0\n", " for i in range(len(s)):\n", " if s[i] == \"<\":\n", " depth += 1\n", " elif s[i] == \">\":\n", " depth -= 1\n", " if depth == 0 and i < len(s) - 1:\n", " return s # Early balance → not a single outer wrapper\n", " if depth == 0:\n", " return s[1:-1].strip()\n", " return s\n", "\n", " # Recursively simplify things like > to not \n", " def simplify(s):\n", " s = unwrap_balanced(s)\n", " s = s.strip()\n", "\n", " # Match > pattern\n", " m = re.fullmatch(r\"not\\s*<(.+)>\", s, re.IGNORECASE)\n", " if m:\n", " inner = m.group(1).strip()\n", " inner = simplify(inner)\n", " return f\"not <{inner}>\"\n", "\n", " # Match comparison operators like <(x position) < (100)>\n", " m_comp = re.fullmatch(r\"<\\s*\\(([^<>]+?)\\)\\s*([<>=])\\s*\\(([^<>]+?)\\)\\s*>\", stmt)\n", " if m_comp:\n", " return f\"({m_comp.group(1).strip()}) {m_comp.group(2)} ({m_comp.group(3).strip()})\"\n", "\n", " return s\n", "\n", " return simplify(stmt)\n", "\n", "# Test cases\n", "# test_cases = [\n", "# (\"if then\", \"mouse down?\"),\n", "# (\"if > then\", \"not \"),\n", "# (\"if < and > then\", \" and \"),\n", "# (\"if < or >> then\", \" or >\"),\n", "# (\"if <(x position) < (100)> then\", \"(x position) < (100)\"),\n", "# ]\n", "test_cases = [\n", " (\"if then\", \"mouse down?\"),\n", " (\"repeat until \", \"touching [edge v]?\"),\n", " (\"if then\", \"key [left arrow v] pressed?\"),\n", " (\"if then\", \"touching [edge v]?\"),\n", " (\"if < and > then\", \"mouse down? and touching [edge v]?\"),\n", " (\"if <(x position) < (100)> then\", \"(x position) < (100)\"),\n", " (\"if <> then\", \"mouse down?\"),\n", " (\"if > then\", \"not<>\"),\n", " (\"if <<> or <>> then\", \"<> or <>\"),\n", " (\"if <> then\", \"\"),\n", " (\"if mouse down? then\", \"mouse down?\"),\n", " (\"\", \"mouse down?\"),\n", "]\n", "\n", "print(\"--- Testing improved function ---\")\n", "for stmt, expected in test_cases:\n", " result = extract_condition_balanced(stmt)\n", " print(f\"[TEST] {stmt}\\n→ Extracted: '{result}'\\n Expected: '{expected}'\\n {'✅ PASS' if result == expected else '❌ FAIL'}\\n\")\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "bc324bc0", "metadata": {}, "outputs": [], "source": [ "all_logics={'logic_0': 'when green flag clicked\\n switch backdrop to [blue sky v]\\n set [score v] to 0\\n show variable [score v]\\n broadcast [Game Start v]',\n", "'logic_1': 'when I receive [Game Over v]\\n if <(score) > (High Score)> then\\n set [High Score v] to (score)\\n end\\n switch backdrop to [Game Over v]',\n", "'logic_2': 'when I receive [Level Up v]\\n change [level v] by 1\\n set [ballSpeed v] to (ballSpeed * 1.1)',\n", "'logic_3': 'when green flag clicked\\n go to x: 0 y: -100\\n forever\\n if then\\n jump\\n end\\n move 5 steps\\n if then\\n broadcast [Game Over v]\\n end\\n end',\n", "'logic_4': 'when [space v] key pressed\\n change y by 10\\n wait 0.2 seconds\\n change y by -10',\n", "'logic_5': 'when green flag clicked\\n go to x: 0 y: 100\\n forever\\n glide 2 seconds to x: pick random (-240, 240) y: 100\\n if then\\n broadcast [Game Over v]\\n end\\n end',\n", "'logic_6': 'when green flag clicked\\n set [score v] to 0\\n set [health v] to 1\\n set [speed v] to 1\\n switch backdrop to [blue sky v]\\n broadcast [Game Start v]',\n", "'logic_7': 'when I receive [Game Over v]\\n if <(score) > (High Score)> then\\n set [High Score v] to (score)\\n end\\n switch backdrop to [Game Over v]',\n", "'logic_8': 'when green flag clicked\\n go to x: 0 y: -130\\n set [shield v] to 0',\n", "'logic_9': 'when [space v] key pressed\\n if <(y position) = -130> then\\n repeat (10)\\n change y by 20\\n wait (0.1) seconds\\n change y by -20\\n end\\n end',\n", "'logic_10': 'when I receive [Power-Up v]\\n if <(Power-Up) = [Shield v]> then\\n set [shield v] to 1\\n end',\n", "'logic_11': 'when green flag clicked\\n go to x: 50 y: 100\\n forever\\n change x by 5\\n if <(x position) > 240> then\\n set x to -240\\n end\\n if <(x position) < -240> then\\n set x to 240\\n end\\n if then\\n broadcast [Game Over v]\\n end\\n end',}" ] }, { "cell_type": "markdown", "id": "6de2c3ab", "metadata": {}, "source": [ "1. Example for the logic_1:\n", "when green flag clicked\n", " switch backdrop to [blue sky v]\n", " set [score v] to 0\n", " show variable [score v]\n", " broadcast [Game Start v]\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `0` should be enclosed in brackets `(0)` which is missing here.\n", " 2. there should be `end` for the `when green flag clicked` at the last line of pesudo-code.\n", " 3. correct use of variables [score v] and options [Game Start v],[blue sky v] \n", " ]\n", "\n", "2. Example for the logic_2:\n", "when I receive [Game Over v]\n", " if <(score) > (High Score)> then\n", " set [High Score v] to (score)\n", " end\n", " switch backdrop to [Game Over v]\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the score and High score where variable but llm conisder as string so it keep it simple but thats wrong e.g. `[High Score v]` and [score v]\n", " 2. in `set [High Score v] to (score)` the `[High Score v]`is correct but the vairable should be ([score v])\n", " 3. there should be `end` for the `when I receive [Game Over v]` at the last line of pesudo-code.\n", " 4. correct use of options [Game Over v],[High Score v].\n", " ]\n", "\n", "3. Example for the logic_3:\n", "when I receive [Level Up v]\n", " change [level v] by 1\n", " set [ballSpeed v] to (ballSpeed * 1.1)\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `1` and should be enclosed in brackets `(1)` which is missing here.\n", " 2. in `set [ballSpeed v] to (ballSpeed * 1.1)` the problem goes with invalid use of the operator block should be like this `(([ballSpeed v])*(1.1))`\n", " 3. the `ballSpeed` is variable but llm conisder as string so it keep it simple but thats wrong in the set for second occurance e.g. `[ballSpeed v]` \n", " 4. there should be `end` for the `when I receive [Level Up v]` at the last line of pesudo-code.\n", " ]\n", "\n", "4. Example for the logic_4:\n", "when green flag clicked\n", " go to x: 0 y: -100\n", " forever\n", " if then\n", " jump\n", " end\n", " move 5 steps\n", " if then\n", " broadcast [Game Over v]\n", " end\n", " end\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `0`,`100` and `5` should be enclosed in brackets `(0)`,`(100)` and `(5)` which is missing here.\n", " 2. there is invalid code use that is `jump`it should be script where you use few code of with event to have jumping motion.\n", " 3. correct use of options [Game Over v],[Ball v] and [space v]\n", " 4. there should be `end` for the `when green flag clicked` at the last line of pesudo-code.\n", " ]\n", "\n", "5. Example for the logic_5:\n", "when [space v] key pressed\n", " change y by 10\n", " wait 0.2 seconds\n", " change y by -10\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `10`,`0.2` and `-10` should be enclosed in brackets`(10)`,`(0.2)` and `(-10)` which is missing here.\n", " 2. correct use of options [space v]\n", " 3. there should be `end` for the `when [space v] key pressed` at the last line of pesudo-code.\n", " ]\n", "\n", "6. Example for the logic_6:\n", "when green flag clicked\n", " go to x: 0 y: 100\n", " forever\n", " glide 2 seconds to x: pick random (-240, 240) y: 100 #(pick random () to ())\n", " if then\n", " broadcast [Game Over v]\n", " end\n", " end\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `0`,`100` and `2` should be enclosed in brackets `(0)`,`(100)` and `(2)` which is missing here.\n", " 2. incorrect use of opcode in `glide 2 seconds to x: pick random (-240, 240) y: 100` it should be `glide (2) seconds to x: (pick random (-240) to (240)) y: (100)`\n", " 3. correct use of options [Game Over v],[Cat v].\n", " 4. there should be `end` for the `when green flag clicked` at the last line of pesudo-code.\n", " ]\n", "\n", "7. Example for the logic_7:\n", "when green flag clicked\n", " set [score v] to 0\n", " set [health v] to 1\n", " set [speed v] to 1\n", " switch backdrop to [blue sky v]\n", " broadcast [Game Start v]\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `0`,`1` and `1` should be enclosed in brackets `(0)`,`(1)` and `(1)` which is missing here.\n", " 2. there should be `end` for the `when green flag clicked` at the last line of pesudo-code.\n", " 3. correct use of variables [score v] and options [Game Start v],[blue sky v] \n", " ]\n", "\n", "8. Example for the logic_8:\n", "when green flag clicked\n", " go to x: 0 y: -130\n", " set [shield v] to 0\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `0`,`-130` should be enclosed in brackets `(0)`,`(-130)` which is missing here.\n", " 2. there should be `end` for the `when green flag clicked` at the last line of pesudo-code.\n", " 3. correct use of variables [shield v]\n", " ]\n", "\n", "9. Example for the logic_9:\n", "when [space v] key pressed\n", " if <(y position) = -130> then\n", " repeat (10)\n", " change y by 20\n", " wait (0.1) seconds\n", " change y by -20\n", " end\n", " end\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `20`,`-130` and `-20` should be enclosed in brackets `(20)`,`(-130)` and `(-20)` which is missing here.\n", " 2. the reportor block such as `(y position)` when used as nesting as seen `if <(y position) = -130> then` it should enclosed in `()` the correct use `if <((y position)) = (-130)> then`\n", " 3. correct use of options [space v]\n", " 4. there should be `end` for the `when [space v] key pressed` at the last line of pesudo-code.\n", " ]\n", "\n", "10. Example for the logic_10:\n", "when I receive [Power-Up v]\n", " if <(Power-Up) = [Shield v]> then\n", " set [shield v] to 1\n", " end\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `1` should be enclosed in brackets `(1)` which is missing here.\n", " 2. there should be `end` for the `when I receive [Power-Up v]` at the last line of pesudo-code.\n", " 3. correct use of variables [Power-Up v] and options [Power-Up v],[Shield v] \n", " ]\n", "\n", "11. Example for the logic_11:\n", "when green flag clicked\n", " go to x: 50 y: 100\n", " forever\n", " change x by 5\n", " if <(x position) > 240> then\n", " set x to -240\n", " end\n", " if <(x position) < -240> then\n", " set x to 240\n", " end\n", " if then\n", " broadcast [Game Over v]\n", " end\n", " end\n", " [ Note:There are some mistake done by the llm to generate the above pseudo code:\n", " 1. the value `50`,`100`, `5`, `240` and `-240` should be enclosed in brackets `(50)`,`(100)`, `(5)`, `(240)` and `(-240)` which is missing here.\n", " 2. the reportor block such as `(x position)` when used as nesting as seen `if <(x position) > 240> then` and if <(x position) < -240> it should enclosed in `()` the correct use if <((x position)) < (-240)> and `if <((x position)) < (-240)>`\n", " 3. correct use of options [Game Over v],[Cat v] \n", " ]" ] }, { "cell_type": "code", "execution_count": 10, "id": "1ee313ee", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. Example for the logic_0:\n", "when green flag clicked\n", " switch backdrop to [blue sky v]\n", " set [score v] to 0\n", " show variable [score v]\n", " broadcast [Game Start v]\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_1:\n", "when I receive [Game Over v]\n", " if <(score) > (High Score)> then\n", " set [High Score v] to (score)\n", " end\n", " switch backdrop to [Game Over v]\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_2:\n", "when I receive [Level Up v]\n", " change [level v] by 1\n", " set [ballSpeed v] to (ballSpeed * 1.1)\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_3:\n", "when green flag clicked\n", " go to x: 0 y: -100\n", " forever\n", " if then\n", " jump\n", " end\n", " move 5 steps\n", " if then\n", " broadcast [Game Over v]\n", " end\n", " end\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_4:\n", "when [space v] key pressed\n", " change y by 10\n", " wait 0.2 seconds\n", " change y by -10\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_5:\n", "when green flag clicked\n", " go to x: 0 y: 100\n", " forever\n", " glide 2 seconds to x: pick random (-240, 240) y: 100\n", " if then\n", " broadcast [Game Over v]\n", " end\n", " end\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_6:\n", "when green flag clicked\n", " set [score v] to 0\n", " set [health v] to 1\n", " set [speed v] to 1\n", " switch backdrop to [blue sky v]\n", " broadcast [Game Start v]\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_7:\n", "when I receive [Game Over v]\n", " if <(score) > (High Score)> then\n", " set [High Score v] to (score)\n", " end\n", " switch backdrop to [Game Over v]\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_8:\n", "when green flag clicked\n", " go to x: 0 y: -130\n", " set [shield v] to 0\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_9:\n", "when [space v] key pressed\n", " if <(y position) = -130> then\n", " repeat (10)\n", " change y by 20\n", " wait (0.1) seconds\n", " change y by -20\n", " end\n", " end\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_10:\n", "when I receive [Power-Up v]\n", " if <(Power-Up) = [Shield v]> then\n", " set [shield v] to 1\n", " end\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic_11:\n", "when green flag clicked\n", " go to x: 50 y: 100\n", " forever\n", " change x by 5\n", " if <(x position) > 240> then\n", " set x to -240\n", " end\n", " if <(x position) < -240> then\n", " set x to 240\n", " end\n", " if then\n", " broadcast [Game Over v]\n", " end\n", " end\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n" ] } ], "source": [ "i=1\n", "for key, value in all_logics.items():\n", " print(f\"{1}. Example for the {key}:\\n{str(value)}\\n [Note:This generated json is ]\\n1.\\n2.\\n3.\\n\\n\")\n", " i += 1\n", "# txt = 'when green flag clicked\\n go to x: 50 y: 100\\n forever\\n change x by 5\\n if <(x position) > 240> then\\n set x to -240\\n end\\n if <(x position) < -240> then\\n set x to 240\\n end\\n if then\\n broadcast [Game Over v]\\n end\\n end'\n", "# print(str(txt))" ] }, { "cell_type": "code", "execution_count": 1, "id": "4700799d", "metadata": {}, "outputs": [], "source": [ "all_logics={\"logic1\": \"when green flag clicked\\n set [score v] to (0)\\n set [lives v] to (3)\\n show variable [score v]\\n show variable [lives v]\\n broadcast [Game Start v]\",\n", "\"logic2\": \"when I receive [Game Over v]\\n broadcast [Reset Game v]\\n set [score v] to (0)\\n set [lives v] to (3)\",\n", "\"logic3\": \"when green flag clicked\\n go to x: (0) y: (-100)\\n set [speed v] to (0)\",\n", "\"logic4\": \"when [right arrow v] key pressed\\n change x by (10)\",\n", "\"logic5\": \"when [left arrow v] key pressed\\n change x by (-10)\",\n", "\"logic6\": \"when [space v] key pressed\\n if <((y position)) = (-100)> then \\n forever\\n change y by (10)\\n wait (0.1) seconds\\n change y by (-10)\\n wait (0.1) seconds\\n end\\n end\",\n", "\"logic7\": \"when green flag clicked\\n go to x: (0) y: (100)\\n set [ballSpeed v] to (5)\\n forever\\n glide (2) seconds to x: (-240) y: (100)\\n if <(x position) < (-235)> then\\n set x to (240)\\n end\\n if then\\n broadcast [Game Over v]\\n stop [all v]\\n end\\n end\",}\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4016c96d", "metadata": {}, "outputs": [], "source": [ "\"logic\": \"when green flag clicked\\n set [score v] to (0)\\n set [lives v] to (3)\\n show variable [score v]\\n show variable [lives v]\\n broadcast [Game Start v]\",\n", "\"logic\": \"when I receive [Game Over v]\\n broadcast [Reset Game v]\\n set [score v] to (0)\\n set [lives v] to (3)\",\n", "\"logic\": \"when green flag clicked\\n go to x: (0) y: (-100)\\n set [speed v] to (0)\",\n", "\"logic\": \"when [right arrow v] key pressed\\n change x by (10)\",\n", "\"logic\": \"when [left arrow v] key pressed\\n change x by (-10)\",\n", "\"logic\": \"when [space v] key pressed\\n if <((y position)) = (-100)> then \\n forever\\n change y by (10)\\n wait (0.1) seconds\\n change y by (-10)\\n wait (0.1) seconds\\n end\\n end\",\n", "\"logic\": \"when green flag clicked\\n go to x: (0) y: (100)\\n set [ballSpeed v] to (5)\\n forever\\n glide (2) seconds to x: (-240) y: (100)\\n if <(x position) < (-235)> then\\n set x to (240)\\n end\\n if then\\n broadcast [Game Over v]\\n stop [all v]\\n end\\n end\"," ] }, { "cell_type": "code", "execution_count": 15, "id": "2f66b647", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['logic1', 'logic2', 'logic3', 'logic4', 'logic5', 'logic6', 'logic7'])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all_logics.keys()" ] }, { "cell_type": "code", "execution_count": 2, "id": "edff93c3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. Example for the logic1:\n", "when green flag clicked\n", " set [score v] to (0)\n", " set [lives v] to (3)\n", " show variable [score v]\n", " show variable [lives v]\n", " broadcast [Game Start v]\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic2:\n", "when I receive [Game Over v]\n", " broadcast [Reset Game v]\n", " set [score v] to (0)\n", " set [lives v] to (3)\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic3:\n", "when green flag clicked\n", " go to x: (0) y: (-100)\n", " set [speed v] to (0)\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic4:\n", "when [right arrow v] key pressed\n", " change x by (10)\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic5:\n", "when [left arrow v] key pressed\n", " change x by (-10)\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic6:\n", "when [space v] key pressed\n", " if <((y position)) = (-100)> then \n", " forever\n", " change y by (10)\n", " wait (0.1) seconds\n", " change y by (-10)\n", " wait (0.1) seconds\n", " end\n", " end\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n", "1. Example for the logic7:\n", "when green flag clicked\n", " go to x: (0) y: (100)\n", " set [ballSpeed v] to (5)\n", " forever\n", " glide (2) seconds to x: (-240) y: (100)\n", " if <(x position) < (-235)> then\n", " set x to (240)\n", " end\n", " if then\n", " broadcast [Game Over v]\n", " stop [all v]\n", " end\n", " end\n", " [Note:This generated json is ]\n", "1.\n", "2.\n", "3.\n", "\n", "\n" ] } ], "source": [ "i=1\n", "for key, value in all_logics.items():\n", " print(f\"{1}. Example for the {key}:\\n{str(value)}\\n [Note:This generated json is ]\\n1.\\n2.\\n3.\\n\\n\")\n", " i += 1" ] }, { "cell_type": "code", "execution_count": null, "id": "a396a819", "metadata": {}, "outputs": [], "source": [ "pseudo_code=\"\"\"\n", "when green flag clicked\n", "set [score v] to (0)\n", "set [lives v] to (3)\n", "show variable [score v]\n", "show variable [lives v]\n", "broadcast [Game Start v]\n", "\"\"\"\n", "opcode_counts=[\n", " {\n", " \"opcode\": \"event_whenflagclicked\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"data_setvariableto\",\n", " \"count\": 2\n", " },\n", " {\n", " \"opcode\": \"data_showvariable\",\n", " \"count\": 2\n", " },\n", " {\n", " \"opcode\": \"event_broadcast\",\n", " \"count\": 1\n", " }\n", " ]\n", "pseudo_code=\"\"\"\n", "when I receive [Game Over v]\n", "broadcast [Reset Game v]\n", "set [score v] to (0)\n", "set [lives v] to (3)\n", "\"\"\"\n", "opcode_counts=[\n", " {\n", " \"opcode\": \"event_whenbroadcastreceived\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"event_broadcast\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"data_setvariableto\",\n", " \"count\": 2\n", " }\n", " ]\n", "\n", "pseudo_code=\"\"\"\n", "when green flag clicked\n", " go to x: (0) y: (-100)\n", " set [speed v] to (0)\n", "\"\"\"\n", "opcode_counts=[\n", " {\n", " \"opcode\": \"event_whenflagclicked\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_gotoxy\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"data_setvariableto\",\n", " \"count\": 1\n", " }\n", " ]\n", "\n", "\n", "pseudo_code=\"\"\"\n", "when [right arrow v] key pressed\n", " change x by (10)\n", "\"\"\"\n", "opcode_counts=[\n", " {\n", " \"opcode\": \"event_whenkeypressed\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_changexby\",\n", " \"count\": 1\n", " }\n", " ]\n", "\n", "\n", "\n", "pseudo_code=\"\"\"\n", "when [left arrow v] key pressed\n", " change x by (-10)\n", "\"\"\"\n", "opcode_counts=[\n", " {\n", " \"opcode\": \"event_whenkeypressed\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_changexby\",\n", " \"count\": 1\n", " }\n", " ]\n", "\n", "\n", "pseudo_code=\"\"\"\n", "when [space v] key pressed\n", " if <((y position)) = (-100)> then \n", " forever\n", " change y by (10)\n", " wait (0.1) seconds\n", " change y by (-10)\n", " wait (0.1) seconds\n", " end\n", " end\n", "\"\"\"\n", "opcode_counts=[\n", " {\n", " \"opcode\": \"event_whenkeypressed\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_if\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_forever\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_changeyby\",\n", " \"count\": 2\n", " }\n", " ]\n", "\n", "pseudo_code=\"\"\"\n", "when green flag clicked\n", " go to x: (0) y: (100)\n", " set [ballSpeed v] to (5)\n", " forever\n", " glide (2) seconds to x: (-240) y: (100)\n", " if <(x position) < (-235)> then\n", " set x to (240)\n", " end\n", " if then\n", " broadcast [Game Over v]\n", " stop [all v]\n", " end\n", " end\n", "\"\"\"\n", "opcode_counts=[\n", " {\n", " \"opcode\": \"event_whenflagclicked\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_gotoxy\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_glidesecstoxy\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_xposition\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_setx\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_forever\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_if\",\n", " \"count\": 2\n", " },\n", " {\n", " \"opcode\": \"operator_lt\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"sensing_istouching\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"event_broadcast\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_stop\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"data_setvariableto\",\n", " \"count\": 1\n", " }\n", " ]\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1ac34c30", "metadata": {}, "outputs": [], "source": [ "{\n", " \"Stage\": {\n", " \"description\": \"Background and global game state management, including broadcasts, rewards, and score.\",\n", " \"plans\": [\n", " {\n", " \"event\": \"event_whenflagclicked\",\n", " \"logic\": \"when green flag clicked\\n set [score v] to (0)\\n set [lives v] to (3)\\n show variable [score v]\\n show variable [lives v]\\n broadcast [Game Start v]\",\n", " \"motion\": [],\n", " \"control\": [],\n", " \"operator\": [],\n", " \"sensing\": [],\n", " \"looks\": [],\n", " \"sounds\": [],\n", " \"events\": [\n", " \"event_broadcast\"\n", " ],\n", " \"data\": [\n", " \"data_setvariableto\",\n", " \"data_showvariable\"\n", " ],\n", " \"opcode_counts\": [\n", " {\n", " \"opcode\": \"event_whenflagclicked\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"data_setvariableto\",\n", " \"count\": 2\n", " },\n", " {\n", " \"opcode\": \"data_showvariable\",\n", " \"count\": 2\n", " },\n", " {\n", " \"opcode\": \"event_broadcast\",\n", " \"count\": 1\n", " }\n", " ]\n", " },\n", " {\n", " \"event\": \"event_whenbroadcastreceived\",\n", " \"logic\": \"when I receive [Game Over v]\\n broadcast [Reset Game v]\\n set [score v] to (0)\\n set [lives v] to (3)\",\n", " \"motion\": [],\n", " \"control\": [],\n", " \"operator\": [],\n", " \"sensing\": [],\n", " \"looks\": [],\n", " \"sounds\": [],\n", " \"events\": [],\n", " \"data\": [\n", " \"data_setvariableto\"\n", " ],\n", " \"opcode_counts\": [\n", " {\n", " \"opcode\": \"event_whenbroadcastreceived\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"event_broadcast\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"data_setvariableto\",\n", " \"count\": 2\n", " }\n", " ]\n", " }\n", " ]\n", " },\n", " \"Cat\": {\n", " \"description\": \"Main character (cat) actions\",\n", " \"plans\": [\n", " {\n", " \"event\": \"event_whenflagclicked\",\n", " \"logic\": \"when green flag clicked\\n go to x: (0) y: (-100)\\n set [speed v] to (0)\",\n", " \"motion\": [\n", " \"motion_gotoxy\"\n", " ],\n", " \"control\": [],\n", " \"operator\": [],\n", " \"sensing\": [],\n", " \"looks\": [],\n", " \"sounds\": [],\n", " \"events\": [],\n", " \"data\": [],\n", " \"opcode_counts\": [\n", " {\n", " \"opcode\": \"event_whenflagclicked\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_gotoxy\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"data_setvariableto\",\n", " \"count\": 1\n", " }\n", " ]\n", " },\n", " {\n", " \"event\": \"event_whenkeypressed\",\n", " \"logic\": \"when [right arrow v] key pressed\\n change x by (10)\",\n", " \"motion\": [\n", " \"motion_changexby\"\n", " ],\n", " \"control\": [],\n", " \"operator\": [],\n", " \"sensing\": [],\n", " \"looks\": [],\n", " \"sounds\": [],\n", " \"events\": [],\n", " \"data\": [],\n", " \"opcode_counts\": [\n", " {\n", " \"opcode\": \"event_whenkeypressed\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_changexby\",\n", " \"count\": 1\n", " }\n", " ]\n", " },\n", " {\n", " \"event\": \"event_whenkeypressed\",\n", " \"logic\": \"when [left arrow v] key pressed\\n change x by (-10)\",\n", " \"motion\": [\n", " \"motion_changexby\"\n", " ],\n", " \"control\": [],\n", " \"operator\": [],\n", " \"sensing\": [],\n", " \"looks\": [],\n", " \"sounds\": [],\n", " \"events\": [],\n", " \"data\": [],\n", " \"opcode_counts\": [\n", " {\n", " \"opcode\": \"event_whenkeypressed\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_changexby\",\n", " \"count\": 1\n", " }\n", " ]\n", " },\n", " {\n", " \"event\": \"event_whenkeypressed\",\n", " \"logic\": \"when [space v] key pressed\\n if <((y position)) = (-100)> then \\n forever\\n change y by (10)\\n wait (0.1) seconds\\n change y by (-10)\\n wait (0.1) seconds\\n end\\n end\",\n", " \"motion\": [\n", " \"motion_changeyby\"\n", " ],\n", " \"control\": [\n", " \"control_forever\",\n", " \"control_if\"\n", " ],\n", " \"operator\": [],\n", " \"sensing\": [],\n", " \"looks\": [],\n", " \"sounds\": [],\n", " \"events\": [],\n", " \"data\": [],\n", " \"opcode_counts\": [\n", " {\n", " \"opcode\": \"event_whenkeypressed\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_if\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_forever\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_changeyby\",\n", " \"count\": 2\n", " }\n", " ]\n", " }\n", " ]\n", " },\n", " \"ball\": {\n", " \"description\": \"Obstacle movement and interaction\",\n", " \"plans\": [\n", " {\n", " \"event\": \"event_whenflagclicked\",\n", " \"logic\": \"when green flag clicked\\n go to x: (0) y: (100)\\n set [ballSpeed v] to (5)\\n forever\\n glide (2) seconds to x: (-240) y: (100)\\n \n", " if <(x position) < (-235)> then\\n set x to (240)\\n end\\n if then\\n broadcast [Game Over v]\\n stop [all v]\\n end\\n end\",\n", " \"motion\": [\n", " \"motion_gotoxy\",\n", " \"motion_glidesecstoxy\",\n", " \"motion_xposition\",\n", " \"motion_setx\"\n", " ],\n", " \"control\": [\n", " \"control_forever\",\n", " \"control_if\",\n", " \"control_stop\"\n", " ],\n", " \"operator\": [\n", " \"operator_lt\"\n", " ],\n", " \"sensing\": [\n", " \"sensing_istouching\"\n", " ],\n", " \"looks\": [],\n", " \"sounds\": [],\n", " \"events\": [\n", " \"event_broadcast\"\n", " ],\n", " \"data\": [],\n", " \"opcode_counts\": [\n", " {\n", " \"opcode\": \"event_whenflagclicked\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_gotoxy\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_glidesecstoxy\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_xposition\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"motion_setx\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_forever\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_if\",\n", " \"count\": 2\n", " },\n", " {\n", " \"opcode\": \"operator_lt\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"sensing_istouching\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"event_broadcast\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"control_stop\",\n", " \"count\": 1\n", " },\n", " {\n", " \"opcode\": \"data_setvariableto\",\n", " \"count\": 1\n", " }\n", " ]\n", " }\n", " ]\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "id": "6a14fb61", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n", "None\n", "None\n" ] } ], "source": [ "def split_arithmetic(text: str):\n", " \"\"\"\n", " Splits `text` of the form \"(…) + (…)\" or \"a - b\" into (lhs_str, operator, rhs_str),\n", " ignoring any + - * / inside parentheses.\n", " Returns (None, None, None) if no top-level operator is found.\n", " \"\"\"\n", " depth = 0\n", " for idx, ch in enumerate(text):\n", " if ch == '(':\n", " depth += 1\n", " elif ch == ')':\n", " depth -= 1\n", " # If we find an operator at depth zero, that's our split point\n", " elif ch in '+-*/' and depth == 0:\n", " lhs = text[:idx].strip()\n", " op = ch\n", " rhs = text[idx+1:].strip()\n", " return lhs, op, rhs\n", " return None, None, None\n", "\n", "text = \"number 1) - (number 2\"\n", "lhs_str, operator_symbol, rhs_str = split_arithmetic(text)\n", "print(lhs_str)\n", "print(operator_symbol)\n", "print(rhs_str)" ] }, { "cell_type": "code", "execution_count": 37, "id": "07ffb848", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'lwrap': '(', 'left': '(x position', 'lwrap_close': ')', 'op': '*', 'rwrap': '(', 'right': 'x position) ) + ( (y position) * (y position) )', 'rwrap_close': ')'}\n", " then` or `(((x position)) * (1))`.\n", " - **Boolean blocks** in conditions must be inside `< >`, including nested ones: `>`, `< and >`,`< or >`.\n", " - **Other Boolean blocks** in conditions must be inside `< >`, including nested ones or values or variables: `<(block/value/variable) * (block/value/variable)>`,`<(block/value/variable) < (block/value/variable)>`, and example of another variable`<[apple v] contains [a v]?>`.\n", " - **Operator expressions** must use explicit Scratch operator blocks, e.g.:\n", " ```\n", " (([ballSpeed v]) * (1.1))\n", " ```\n", " - **Every hat block script must end** with a final `end` on its own line.\n", " \n", "3. **Pseudo‑code formatting**:\n", " - Represent each block or nested block on its own line.\n", " - Indent nested blocks by 4 spaces under their parent (`forever`, `if`, etc.).\n", " - No comments or explanatory text—just the block sequence.\n", " - a natural language breakdown of each step taken after the event, formatted as a multi-line string representing pseudo-code. Ensure clarity and granularity—each described action should map closely to a Scratch block or tight sequence.\n", "\n", "4. **Logic content**:\n", " - Build clear flow for mechanics (movement, jumping, flying, scoring, collisions).\n", " - Match each action closely to a Scratch block or tight sequence.\n", " - Do **NOT** include any justification or comments—only the raw logic.\n", "\n", "5. **Examples for reference**: \n", "**Correct** pattern for a simple start script:\n", " ```\n", " when green flag clicked\n", " switch backdrop to [blue sky v]\n", " set [score v] to (0)\n", " show variable [score v]\n", " broadcast [Game Start v]\n", " end\n", " ```\n", "**Correct** pattern for updating the high score variable handling:\n", " ```\n", " when I receive [Game Over v]\n", " if <((score)) > (([High Score v]))> then\n", " set [High Score v] to ([score v])\n", " end\n", " switch backdrop to [Game Over v]\n", " end\n", " ```\n", "**Correct** pattern for level up and increase difficulty use:\n", " ```\n", " when I receive [Level Up v]\n", " change [level v] by (1)\n", " set [ballSpeed v] to ((([ballSpeed v]) * (1.1)))\n", " end\n", " ```\n", "**Correct** pattern for jumping mechanics use:\n", " ```\n", " when [space v] key pressed\n", " if <((y position)) = (-100)> then\n", " repeat (5)\n", " change y by (100)\n", " wait (0.1) seconds\n", " change y by (-100)\n", " wait (0.1) seconds\n", " end\n", " end\n", " end\n", " ```\n", "**Correct** pattern for continuos moving objects use:\n", " ```\n", " when green flag clicked\n", " go to x: (240) y: (-100)\n", " set [speed v] to (-5)\n", " show variable [speed v]\n", " forever\n", " change x by ([speed v])\n", " if <((x position)) < (-240)> then\n", " go to x: (240) y: (-100)\n", " end\n", " end\n", " end\n", " ```\n", "**Correct** pattern for continuos moving objects use:\n", " ```\n", " when green flag clicked\n", " go to x: (240) y: (-100)\n", " set [speed v] to (-5)\n", " show variable [speed v]\n", " forever\n", " change x by ([speed v])\n", " if <((x position)) < (-240)> then\n", " go to x: (240) y: (-100)\n", " end\n", " end\n", " end\n", " ```\n", "6. **Do not** add any explanation of logic or comments to justify or explain just put the logic content in the JSON.\n", "7. **Output**: \n", "Return **only** a JSON object, using double quotes everywhere, where the key for the pseudo-code will be the target name closest to the \"Script for:\" value. If no code-blocks are found, return `{\"refined_logic\": \"No Code-blocks\"}`.\n", "\n", "```json\n", "{\n", "\"refined_logic\":{\n", " \"[Target name similar to script for]\": {\n", " \"plan\":[\n", " {\"pseudocode\":\"…your fully‑formatted pseudo‑code here…\"},\n", " {\"pseudocode\":\"…your fully‑formatted pseudo‑code here…\"}\n", " ]\n", " }\n", "}\n", "}\n", "```\n" ] } ], "source": [ "\n", "test=\"You are an expert in Scratch 3.0 game development, specializing in understanding block relationships (stacked, nested).\\n\\\"Analyze the Scratch code-block image and generate Pseudo-Code for what this logic appears to be doing.\\\"\\nFrom Image, you also have to detect a value of Key given in Text form \\\"Script for: \\\". Below is the example\\nExample: \\\"Script for: Bear\\\", \\\"Script for:\\\" is a key and \\\"Bear\\\" is value and check if there is related target name available.\\n\\n**Targets in Game (Sprites and Stage) available in project_json:** {', '.join(target_names)}\\n\\n--- Scratch 3.0 Block Reference ---\\n\\n### Hat Blocks\\nDescription: {hat_description}\\nBlocks:\\n{hat_opcodes_functionalities}\\n\\n### Boolean Blocks\\nDescription: {boolean_description}\\nBlocks:\\n{boolean_opcodes_functionalities}\\n\\n### C Blocks\\nDescription: {c_description}\\nBlocks:\\n{c_opcodes_functionalities}\\n\\n### Cap Blocks\\nDescription: {cap_description}\\nBlocks:\\n{cap_opcodes_functionalities}\\n\\n### Reporter Blocks\\nDescription: {reporter_description}\\nBlocks:\\n{reporter_opcodes_functionalities}\\n\\n### Stack Blocks\\nDescription: {stack_description}\\nBlocks:\\n{stack_opcodes_functionalities}\\n-----------------------------------\\n\\nYour task is to:\\nIf you don't find any \\\"Code-Blocks\\\" then,\\n **Don't generate Pseudo Code, and pass the message \\\"No Code-blocks\\\"\\nIf you find any \\\"Code-Blocks\\\" then,\\n1. **Refine the 'logic'**: Make it precise, accurate, and fully aligned with the Game Description. Use Scratch‑consistent verbs and phrasing. **Do NOT** use raw double‑quotes inside the logic string.\\n\\n2. **Structural requirements**:\\n - **Numeric values** `(e.g., 0, 5, 0.2, -130)` **must** be in parentheses: `(0)`, `(5)`, `(0.2)`, `(-130)`.\\n - **AlphaNumeric values** `(e.g., hello, say 5, 4, hi!)` **must** be in parentheses: `(hello)`, `(say 5)`, `(4)`, `(hi!)`.\\n - **Variables** must be in the form `[variable v]` (e.g., `[score v]`), even when used inside expressions two example use `set [score v] to (1)` or `show variable ([speed v])`.\\n - **Dropdown options** must be in the form `[option v]` (e.g., `[Game Start v]`, `[blue sky v]`). example use `when [space v] key pressed`.\\n - **Reporter blocks** used as inputs must be double‑wrapped: `((x position))`, `((y position))`. example use `if <((y position)) = (-130)> then` or `(((x position)) * (1))`.\\n - **Boolean blocks** in conditions must be inside `< >`, including nested ones: `>`, `< and >`,`< or >`.\\n - **Other Boolean blocks** in conditions must be inside `< >`, including nested ones or values or variables: `<(block/value/variable) * (block/value/variable)>`,`<(block/value/variable) < (block/value/variable)>`, and example of another variable`<[apple v] contains [a v]?>`.\\n - **Operator expressions** must use explicit Scratch operator blocks, e.g.:\\n ```\\n (([ballSpeed v]) * (1.1))\\n ```\\n - **Every hat block script must end** with a final `end` on its own line.\\n \\n3. **Pseudo‑code formatting**:\\n - Represent each block or nested block on its own line.\\n - Indent nested blocks by 4 spaces under their parent (`forever`, `if`, etc.).\\n - No comments or explanatory text—just the block sequence.\\n - a natural language breakdown of each step taken after the event, formatted as a multi-line string representing pseudo-code. Ensure clarity and granularity—each described action should map closely to a Scratch block or tight sequence.\\n\\n4. **Logic content**:\\n - Build clear flow for mechanics (movement, jumping, flying, scoring, collisions).\\n - Match each action closely to a Scratch block or tight sequence.\\n - Do **NOT** include any justification or comments—only the raw logic.\\n\\n5. **Examples for reference**: \\n**Correct** pattern for a simple start script:\\n ```\\n when green flag clicked\\n switch backdrop to [blue sky v]\\n set [score v] to (0)\\n show variable [score v]\\n broadcast [Game Start v]\\n end\\n ```\\n**Correct** pattern for updating the high score variable handling:\\n ```\\n when I receive [Game Over v]\\n if <((score)) > (([High Score v]))> then\\n set [High Score v] to ([score v])\\n end\\n switch backdrop to [Game Over v]\\n end\\n ```\\n**Correct** pattern for level up and increase difficulty use:\\n ```\\n when I receive [Level Up v]\\n change [level v] by (1)\\n set [ballSpeed v] to ((([ballSpeed v]) * (1.1)))\\n end\\n ```\\n**Correct** pattern for jumping mechanics use:\\n ```\\n when [space v] key pressed\\n if <((y position)) = (-100)> then\\n repeat (5)\\n change y by (100)\\n wait (0.1) seconds\\n change y by (-100)\\n wait (0.1) seconds\\n end\\n end\\n end\\n ```\\n**Correct** pattern for continuos moving objects use:\\n ```\\n when green flag clicked\\n go to x: (240) y: (-100)\\n set [speed v] to (-5)\\n show variable [speed v]\\n forever\\n change x by ([speed v])\\n if <((x position)) < (-240)> then\\n go to x: (240) y: (-100)\\n end\\n end\\n end\\n ```\\n**Correct** pattern for continuos moving objects use:\\n ```\\n when green flag clicked\\n go to x: (240) y: (-100)\\n set [speed v] to (-5)\\n show variable [speed v]\\n forever\\n change x by ([speed v])\\n if <((x position)) < (-240)> then\\n go to x: (240) y: (-100)\\n end\\n end\\n end\\n ```\\n6. **Do not** add any explanation of logic or comments to justify or explain just put the logic content in the JSON.\\n7. **Output**: \\nReturn **only** a JSON object, using double quotes everywhere, where the key for the pseudo-code will be the target name closest to the \\\"Script for:\\\" value. If no code-blocks are found, return `{\\\"refined_logic\\\": \\\"No Code-blocks\\\"}`.\\n\\n```json\\n{\\n\\\"refined_logic\\\":{\\n \\\"[Target name similar to script for]\\\": {\\n \\\"plan\\\":[\\n {\\\"pseudocode\\\":\\\"…your fully‑formatted pseudo‑code here…\\\"},\\n {\\\"pseudocode\\\":\\\"…your fully‑formatted pseudo‑code here…\\\"}\\n ]\\n }\\n}\\n}\\n```\"\n", "print(test)" ] }, { "cell_type": "code", "execution_count": 47, "id": "e3fac594", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "BASE_DIR = Path.cwd() \n", "#BASE_DIR = Path(__file__).parent\n", "BLOCKS_DIR = BASE_DIR / \"blocks\"\n", "STATIC_DIR = BASE_DIR / \"static\"\n", "GEN_PROJECT_DIR = BASE_DIR / \"generated_projects\"\n", "BACKDROP_DIR = BLOCKS_DIR / \"Backdrops\"\n", "SPRITE_DIR = BLOCKS_DIR / \"sprites\"\n", "OUTPUT_DIR = BASE_DIR / \"OUTPUTS\"\n", "DETECTED_IMAGE_DIR = OUTPUT_DIR / \"DETECTED_IMAGE\"\n", "IMAGE_DIR = OUTPUT_DIR / \"SCANNED_IMAGE\"\n", "JSON_DIR = OUTPUT_DIR / \"EXTRACTED_JSON\"\n", "\n", "folder_image_paths = folder_image_paths = [\n", " #backdrops\n", " BACKDROP_DIR / \"badroom3.sb3\" / \"8cc0b88d53345b3e337e8f028a32a4e7.png\",\n", " BACKDROP_DIR / \"baseball2.sb3\" / \"7be1f5b3e682813dac1f297e52ff7dca.png\",\n", " BACKDROP_DIR / \"beach_malibu.sb3\" / \"050615fe992a00d6af0e664e497ebf53.png\",\n", " BACKDROP_DIR / \"castle2.sb3\" / \"951765ee7f7370f120c9df20b577c22f.png\",\n", " BACKDROP_DIR / \"hall.sb3\" / \"ea86ca30b346f27ca5faf1254f6a31e3.png\",\n", " BACKDROP_DIR / \"jungle.sb3\" / \"f4f908da19e2753f3ed679d7b37650ca.png\",\n", " #sprite\n", " SPRITE_DIR / \"Batter.sprite3\" / \"baseball_sprite_motion_1.png\",\n", " SPRITE_DIR / \"Bear.sprite3\" / \"bear_motion_2.png\",\n", " SPRITE_DIR / \"Beetle.sprite3\" / \"46d0dfd4ae7e9bfe3a6a2e35a4905eae.png\",\n", " SPRITE_DIR / \"cat\" / \"cat_motion_1.png\",\n", " SPRITE_DIR / \"Centaur.sprite3\" / \"2373556e776cad3ba4d6ee04fc34550b.png\",\n", " SPRITE_DIR / \"Crab.sprite3\" / \"bear_element.png\",\n", " SPRITE_DIR / \"Soccer Ball.sprite3\" / \"cat_football.png\",\n", " ]" ] }, { "cell_type": "code", "execution_count": 48, "id": "ee3c8d01", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/Backdrops/badroom3.sb3/8cc0b88d53345b3e337e8f028a32a4e7.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/Backdrops/baseball2.sb3/7be1f5b3e682813dac1f297e52ff7dca.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/Backdrops/beach_malibu.sb3/050615fe992a00d6af0e664e497ebf53.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/Backdrops/castle2.sb3/951765ee7f7370f120c9df20b577c22f.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/Backdrops/hall.sb3/ea86ca30b346f27ca5faf1254f6a31e3.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/Backdrops/jungle.sb3/f4f908da19e2753f3ed679d7b37650ca.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/sprites/Batter.sprite3/baseball_sprite_motion_1.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/sprites/Bear.sprite3/bear_motion_2.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/sprites/Beetle.sprite3/46d0dfd4ae7e9bfe3a6a2e35a4905eae.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/sprites/cat/cat_motion_1.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/sprites/Centaur.sprite3/2373556e776cad3ba4d6ee04fc34550b.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/sprites/Crab.sprite3/bear_element.png'),\n", " WindowsPath('d:/DEV PATEL/2025/scratch_VLM/scratch_agent/utils/blocks/sprites/Soccer Ball.sprite3/cat_football.png')]" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "folder_image_paths" ] }, { "cell_type": "code", "execution_count": null, "id": "9475e0ac", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "scratch_env", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.11" } }, "nbformat": 4, "nbformat_minor": 5 }