{
  "format": "tokbit",
  "version": 1,
  "name": "Arrows with a counter",
  "main": [
    {
      "id": "u1",
      "kind": "call",
      "call": "cls()"
    },
    {
      "id": "u2",
      "kind": "call",
      "call": "color(WHITE)"
    },
    {
      "id": "u3",
      "kind": "call",
      "call": "at(0, 0)"
    },
    {
      "id": "u4",
      "kind": "output",
      "values": "\"Arrows move the star, space to quit.\"",
      "newline": false
    },
    {
      "id": "u5",
      "kind": "call",
      "call": "color(YELLOW)"
    },
    {
      "id": "u6",
      "kind": "declare",
      "name": "x",
      "type": "int",
      "init": "20"
    },
    {
      "id": "u7",
      "kind": "declare",
      "name": "y",
      "type": "int",
      "init": "12"
    },
    {
      "id": "u8",
      "kind": "declare",
      "name": "k",
      "type": "str",
      "init": "\"\""
    },
    {
      "id": "u9",
      "kind": "declare",
      "name": "steps",
      "type": "int",
      "init": "0"
    },
    {
      "id": "u10",
      "kind": "while",
      "condition": "k != \" \"",
      "body": [
        {
          "id": "u11",
          "kind": "call",
          "call": "at(x, y)"
        },
        {
          "id": "u12",
          "kind": "output",
          "values": "\"*\"",
          "newline": false
        },
        {
          "id": "u13",
          "kind": "call",
          "call": "sleep(50)"
        },
        {
          "id": "u14",
          "kind": "assign",
          "target": "k",
          "value": "key()",
          "comment": "key() does not wait: it returns the pressed key or an empty string."
        },
        {
          "id": "u15",
          "kind": "call",
          "call": "at(x, y)",
          "comment": "Erases the old position with a space so the star leaves no trail."
        },
        {
          "id": "u16",
          "kind": "output",
          "values": "\" \"",
          "newline": false
        },
        {
          "id": "u17",
          "kind": "if",
          "condition": "k == \"left\" and x > 0",
          "then": [
            {
              "id": "u18",
              "kind": "assign",
              "target": "x",
              "value": "x - 1"
            },
            {
              "id": "u19",
              "kind": "assign",
              "target": "steps",
              "value": "steps + 1"
            }
          ],
          "else": []
        },
        {
          "id": "u20",
          "kind": "if",
          "condition": "k == \"right\" and x < 39",
          "then": [
            {
              "id": "u21",
              "kind": "assign",
              "target": "x",
              "value": "x + 1"
            },
            {
              "id": "u22",
              "kind": "assign",
              "target": "steps",
              "value": "steps + 1"
            }
          ],
          "else": []
        },
        {
          "id": "u23",
          "kind": "if",
          "condition": "k == \"up\" and y > 2",
          "then": [
            {
              "id": "u24",
              "kind": "assign",
              "target": "y",
              "value": "y - 1"
            },
            {
              "id": "u25",
              "kind": "assign",
              "target": "steps",
              "value": "steps + 1"
            }
          ],
          "else": []
        },
        {
          "id": "u26",
          "kind": "if",
          "condition": "k == \"down\" and y < 24",
          "then": [
            {
              "id": "u27",
              "kind": "assign",
              "target": "y",
              "value": "y + 1"
            },
            {
              "id": "u28",
              "kind": "assign",
              "target": "steps",
              "value": "steps + 1"
            }
          ],
          "else": []
        }
      ]
    },
    {
      "id": "u29",
      "kind": "call",
      "call": "color(WHITE)"
    },
    {
      "id": "u30",
      "kind": "call",
      "call": "at(0, 1)"
    },
    {
      "id": "u31",
      "kind": "output",
      "values": "\"Steps:\", steps"
    }
  ],
  "functions": [],
  "comment": "Solution to the task “Step counter”: Count how many times the star really moved (it does not move at the edge) and print the number of steps at the end."
}
