{
  "format": "tokbit",
  "version": 1,
  "name": "Clock with lunch",
  "main": [
    {
      "id": "ho13",
      "kind": "call",
      "call": "cls()"
    },
    {
      "id": "ho14",
      "kind": "output",
      "values": "\"Today is \" + str(day()) + \"/\" + str(month()) + \"/\" + str(year())"
    },
    {
      "id": "ho15",
      "kind": "output",
      "values": "greeting(hour())"
    },
    {
      "id": "ho16",
      "kind": "output",
      "values": "\"Space (Action) stops the clock.\""
    },
    {
      "id": "ho23",
      "kind": "call",
      "call": "dial()"
    },
    {
      "id": "ho17",
      "kind": "declare",
      "name": "k",
      "type": "str",
      "init": "\"\""
    },
    {
      "id": "ho22",
      "kind": "while",
      "condition": "k != \" \"",
      "body": [
        {
          "id": "ho24",
          "kind": "call",
          "call": "hands()"
        },
        {
          "id": "ho18",
          "kind": "call",
          "call": "at(4, 12)"
        },
        {
          "id": "ho19",
          "kind": "output",
          "values": "two_digits(hour()) + \":\" + two_digits(minute()) + \":\" + two_digits(second())",
          "newline": false
        },
        {
          "id": "ho20",
          "kind": "call",
          "call": "sleep(200)"
        },
        {
          "id": "ho21",
          "kind": "assign",
          "target": "k",
          "value": "key()"
        }
      ],
      "comment": "A live clock: the hands and the time are redrawn until you press Space."
    }
  ],
  "functions": [
    {
      "name": "two_digits",
      "params": [
        {
          "name": "n",
          "type": "int"
        }
      ],
      "returnType": "str",
      "blocks": [
        {
          "id": "ho2",
          "kind": "if",
          "condition": "n < 10",
          "then": [
            {
              "id": "ho1",
              "kind": "return",
              "value": "\"0\" + str(n)"
            }
          ],
          "else": []
        },
        {
          "id": "ho3",
          "kind": "return",
          "value": "str(n)"
        }
      ],
      "comment": "A number with two digits: 7 → \"07\"."
    },
    {
      "name": "greeting",
      "params": [
        {
          "name": "h",
          "type": "int"
        }
      ],
      "returnType": "str",
      "blocks": [
        {
          "id": "ho5",
          "kind": "if",
          "condition": "h < 5 or h >= 22",
          "then": [
            {
              "id": "ho4",
              "kind": "return",
              "value": "\"Good night!\""
            }
          ],
          "else": []
        },
        {
          "id": "ho7",
          "kind": "if",
          "condition": "h < 9",
          "then": [
            {
              "id": "ho6",
              "kind": "return",
              "value": "\"Good morning!\""
            }
          ],
          "else": []
        },
        {
          "id": "ho9n",
          "kind": "if",
          "condition": "h >= 11 and h < 13",
          "then": [
            {
              "id": "ho8n",
              "kind": "return",
              "value": "\"Enjoy your meal!\""
            }
          ],
          "else": [],
          "comment": "Noon must come before the morning, otherwise “h < 12” would win."
        },
        {
          "id": "ho9",
          "kind": "if",
          "condition": "h < 12",
          "then": [
            {
              "id": "ho8",
              "kind": "return",
              "value": "\"Good morning!\""
            }
          ],
          "else": []
        },
        {
          "id": "ho11",
          "kind": "if",
          "condition": "h < 18",
          "then": [
            {
              "id": "ho10",
              "kind": "return",
              "value": "\"Good afternoon!\""
            }
          ],
          "else": []
        },
        {
          "id": "ho12",
          "kind": "return",
          "value": "\"Good evening!\""
        }
      ],
      "comment": "A greeting by the hour: night before 5 and from 22, morning until 12, afternoon until 18, then evening."
    },
    {
      "name": "ray",
      "params": [
        {
          "name": "angle",
          "type": "float"
        },
        {
          "name": "start",
          "type": "int"
        },
        {
          "name": "length",
          "type": "int"
        }
      ],
      "returnType": null,
      "blocks": [
        {
          "id": "ho30",
          "kind": "declare",
          "name": "a",
          "type": "float",
          "init": "radians(angle)"
        },
        {
          "id": "ho31",
          "kind": "call",
          "call": "line(240 + round(start * sin(a)), 96 - round(start * cos(a)), 240 + round(length * sin(a)), 96 - round(length * cos(a)))",
          "comment": "sin gives the step to the right, cos the step up; screen y grows downwards, so it is 96 minus."
        }
      ],
      "comment": "A line in direction angle (in degrees, 0 = up, clockwise) from the clock centre (240, 96): from distance start to length."
    },
    {
      "name": "dial",
      "params": [],
      "returnType": null,
      "blocks": [
        {
          "id": "ho32",
          "kind": "call",
          "call": "color(WHITE)"
        },
        {
          "id": "ho33",
          "kind": "call",
          "call": "circle(240, 96, 44)"
        },
        {
          "id": "ho35",
          "kind": "for",
          "variable": "mark",
          "range": "12",
          "body": [
            {
              "id": "ho34",
              "kind": "call",
              "call": "ray(mark * 30, 39, 43)"
            }
          ],
          "comment": "Twelve marks, 30° apart."
        },
        {
          "id": "ho36",
          "kind": "call",
          "call": "color(LIGHT_BLUE)"
        }
      ],
      "comment": "The dial: a circle and the hour marks."
    },
    {
      "name": "hands",
      "params": [],
      "returnType": null,
      "blocks": [
        {
          "id": "ho37",
          "kind": "call",
          "call": "color(BLUE)"
        },
        {
          "id": "ho38",
          "kind": "call",
          "call": "circle(240, 96, 37, True)",
          "comment": "Clears the old hands: a filled circle in the background colour."
        },
        {
          "id": "ho39",
          "kind": "call",
          "call": "color(WHITE)"
        },
        {
          "id": "ho40",
          "kind": "call",
          "call": "ray(hour() % 12 * 30 + minute() / 2, 0, 22)",
          "comment": "The hour hand: 30° per hour and half a degree per minute."
        },
        {
          "id": "ho41",
          "kind": "call",
          "call": "ray(minute() * 6, 0, 32)"
        },
        {
          "id": "ho42",
          "kind": "call",
          "call": "color(LIGHT_RED)"
        },
        {
          "id": "ho43",
          "kind": "call",
          "call": "ray(second() * 6, 0, 36)",
          "comment": "The second hand: 6° per second."
        },
        {
          "id": "ho44",
          "kind": "call",
          "call": "color(LIGHT_BLUE)"
        }
      ],
      "comment": "The hands at the current time."
    }
  ],
  "comment": "Solution to the task “Enjoy your meal”: At noon (from 11 to 13 o’clock) the program wishes “Enjoy your meal!” instead of the greeting. Where in the function does the condition go?"
}
