{
  "format": "tokbit",
  "version": 1,
  "name": "Slower Simon",
  "main": [
    {
      "id": "u58",
      "kind": "call",
      "call": "draw_field()"
    },
    {
      "id": "u59",
      "kind": "declare",
      "name": "sequence",
      "type": "list[int]",
      "init": "[]",
      "comment": "The sequence of fields the player has to remember."
    },
    {
      "id": "u60",
      "kind": "declare",
      "name": "score",
      "type": "int",
      "init": "0"
    },
    {
      "id": "u61",
      "kind": "declare",
      "name": "playing",
      "type": "bool",
      "init": "True"
    },
    {
      "id": "u62",
      "kind": "while",
      "condition": "playing",
      "body": [
        {
          "id": "u63",
          "kind": "call",
          "call": "sequence.append(choice([1, 2, 3, 4]))"
        },
        {
          "id": "u64",
          "kind": "call",
          "call": "show_status(\"Listen...\", score)"
        },
        {
          "id": "u65",
          "kind": "call",
          "call": "sleep(500)"
        },
        {
          "id": "u66",
          "kind": "call",
          "call": "play_back(sequence, max(300, 450 - len(sequence) * 25))"
        },
        {
          "id": "u67",
          "kind": "call",
          "call": "show_status(\"Repeat!\", score)"
        },
        {
          "id": "u68",
          "kind": "assign",
          "target": "playing",
          "value": "repeat_back(sequence)"
        },
        {
          "id": "u69",
          "kind": "if",
          "condition": "playing",
          "then": [
            {
              "id": "u70",
              "kind": "assign",
              "target": "score",
              "value": "len(sequence)"
            },
            {
              "id": "u71",
              "kind": "call",
              "call": "beep(1200, 60)"
            }
          ],
          "else": []
        }
      ],
      "comment": "The game loop: every round adds one field, plays the sequence and the player repeats it."
    },
    {
      "id": "u72",
      "kind": "call",
      "call": "game_over(score)"
    }
  ],
  "functions": [
    {
      "name": "draw_pads",
      "params": [
        {
          "name": "pad",
          "type": "int"
        },
        {
          "name": "lit",
          "type": "bool"
        }
      ],
      "returnType": null,
      "blocks": [
        {
          "id": "u1",
          "kind": "declare",
          "name": "columns",
          "type": "list[int]",
          "init": "[16, 6, 26, 16]"
        },
        {
          "id": "u2",
          "kind": "declare",
          "name": "lines",
          "type": "list[int]",
          "init": "[4, 9, 9, 14]"
        },
        {
          "id": "u3",
          "kind": "declare",
          "name": "dark",
          "type": "list[int]",
          "init": "[RED, GREEN, BLUE, BROWN]"
        },
        {
          "id": "u4",
          "kind": "declare",
          "name": "light",
          "type": "list[int]",
          "init": "[LIGHT_RED, LIGHT_GREEN, LIGHT_BLUE, YELLOW]"
        },
        {
          "id": "u5",
          "kind": "if",
          "condition": "lit",
          "then": [
            {
              "id": "u6",
              "kind": "call",
              "call": "color(light[pad - 1])"
            }
          ],
          "else": [
            {
              "id": "u7",
              "kind": "call",
              "call": "color(dark[pad - 1])"
            }
          ]
        },
        {
          "id": "u8",
          "kind": "for",
          "variable": "r",
          "range": "4",
          "body": [
            {
              "id": "u9",
              "kind": "call",
              "call": "at(columns[pad - 1], lines[pad - 1] + r)"
            },
            {
              "id": "u10",
              "kind": "output",
              "values": "\"########\"",
              "newline": false
            }
          ]
        },
        {
          "id": "u11",
          "kind": "call",
          "call": "color(WHITE)"
        },
        {
          "id": "u12",
          "kind": "call",
          "call": "at(columns[pad - 1] + 3, lines[pad - 1] + 1)"
        },
        {
          "id": "u13",
          "kind": "output",
          "values": "\" \" + str(pad) + \" \"",
          "newline": false
        }
      ],
      "comment": "Draws one of the four fields from characters; a lit field in a lighter colour. Field 1 top, 2 left, 3 right, 4 bottom."
    },
    {
      "name": "light_up",
      "params": [
        {
          "name": "pad",
          "type": "int"
        },
        {
          "name": "ms",
          "type": "int"
        }
      ],
      "returnType": null,
      "blocks": [
        {
          "id": "u14",
          "kind": "declare",
          "name": "tones",
          "type": "list[str]",
          "init": "[\"C4\", \"E4\", \"G4\", \"C5\"]"
        },
        {
          "id": "u15",
          "kind": "call",
          "call": "draw_pads(pad, True)"
        },
        {
          "id": "u16",
          "kind": "call",
          "call": "note(tones[pad - 1], ms)",
          "comment": "Each field has its own tone; note waits until it has played."
        },
        {
          "id": "u17",
          "kind": "call",
          "call": "draw_pads(pad, False)"
        }
      ],
      "comment": "Lights a field and plays its tone."
    },
    {
      "name": "draw_field",
      "params": [],
      "returnType": null,
      "blocks": [
        {
          "id": "u18",
          "kind": "call",
          "call": "background(BLACK)"
        },
        {
          "id": "u19",
          "kind": "call",
          "call": "border(DARK_GREY)"
        },
        {
          "id": "u20",
          "kind": "call",
          "call": "cls()"
        },
        {
          "id": "u21",
          "kind": "call",
          "call": "color(YELLOW)"
        },
        {
          "id": "u22",
          "kind": "call",
          "call": "at(15, 1)"
        },
        {
          "id": "u23",
          "kind": "output",
          "values": "\"S I M O N\"",
          "newline": false
        },
        {
          "id": "u24",
          "kind": "for",
          "variable": "p",
          "range": "4",
          "body": [
            {
              "id": "u25",
              "kind": "call",
              "call": "draw_pads(p + 1, False)"
            }
          ]
        },
        {
          "id": "u26",
          "kind": "call",
          "call": "color(GREY)"
        },
        {
          "id": "u27",
          "kind": "call",
          "call": "at(1, 21)"
        },
        {
          "id": "u28",
          "kind": "output",
          "values": "\"Repeat with keys 1 to 4 or arrows.\"",
          "newline": false
        }
      ],
      "comment": "The start screen: title, four fields and a hint."
    },
    {
      "name": "show_status",
      "params": [
        {
          "name": "text",
          "type": "str"
        },
        {
          "name": "score",
          "type": "int"
        }
      ],
      "returnType": null,
      "blocks": [
        {
          "id": "u29",
          "kind": "call",
          "call": "color(WHITE)"
        },
        {
          "id": "u30",
          "kind": "call",
          "call": "at(1, 23)"
        },
        {
          "id": "u31",
          "kind": "output",
          "values": "text + \"   \" + \"Score: \" + str(score) + \"      \"",
          "newline": false
        }
      ],
      "comment": "The game status at the bottom: what is happening and the score."
    },
    {
      "name": "play_back",
      "params": [
        {
          "name": "sequence",
          "type": "list[int]"
        },
        {
          "name": "ms",
          "type": "int"
        }
      ],
      "returnType": null,
      "blocks": [
        {
          "id": "u32",
          "kind": "for",
          "variable": "i",
          "range": "len(sequence)",
          "body": [
            {
              "id": "u33",
              "kind": "call",
              "call": "light_up(sequence[i], ms)"
            },
            {
              "id": "u34",
              "kind": "call",
              "call": "sleep(ms // 3)"
            }
          ]
        }
      ],
      "comment": "Plays the whole sequence; ms is the length of one flash (it gets shorter, the game speeds up)."
    },
    {
      "name": "pad_from_key",
      "params": [
        {
          "name": "k",
          "type": "str"
        }
      ],
      "returnType": "int",
      "blocks": [
        {
          "id": "u35",
          "kind": "declare",
          "name": "keys",
          "type": "list[str]",
          "init": "[\"1\", \"2\", \"3\", \"4\"]"
        },
        {
          "id": "u36",
          "kind": "declare",
          "name": "arrows",
          "type": "list[str]",
          "init": "[\"up\", \"left\", \"right\", \"down\"]"
        },
        {
          "id": "u37",
          "kind": "for",
          "variable": "i",
          "range": "4",
          "body": [
            {
              "id": "u38",
              "kind": "if",
              "condition": "k == keys[i] or k == arrows[i]",
              "then": [
                {
                  "id": "u39",
                  "kind": "return",
                  "value": "i + 1"
                }
              ],
              "else": []
            }
          ]
        },
        {
          "id": "u40",
          "kind": "return",
          "value": "0"
        }
      ],
      "comment": "The field number for a key (1–4 or an arrow), any other key gives 0."
    },
    {
      "name": "wait_for_pad",
      "params": [],
      "returnType": "int",
      "blocks": [
        {
          "id": "u41",
          "kind": "declare",
          "name": "pad",
          "type": "int",
          "init": "0"
        },
        {
          "id": "u42",
          "kind": "while",
          "condition": "pad == 0",
          "body": [
            {
              "id": "u43",
              "kind": "assign",
              "target": "pad",
              "value": "pad_from_key(key())"
            },
            {
              "id": "u44",
              "kind": "call",
              "call": "sleep(20)"
            }
          ],
          "comment": "key() does not wait: we keep asking until the player presses a valid key."
        },
        {
          "id": "u45",
          "kind": "return",
          "value": "pad"
        }
      ],
      "comment": "Waits until the player picks a field."
    },
    {
      "name": "repeat_back",
      "params": [
        {
          "name": "sequence",
          "type": "list[int]"
        }
      ],
      "returnType": "bool",
      "blocks": [
        {
          "id": "u46",
          "kind": "for",
          "variable": "i",
          "range": "len(sequence)",
          "body": [
            {
              "id": "u47",
              "kind": "declare",
              "name": "pad",
              "type": "int",
              "init": "wait_for_pad()"
            },
            {
              "id": "u48",
              "kind": "call",
              "call": "light_up(pad, 200)"
            },
            {
              "id": "u49",
              "kind": "if",
              "condition": "pad != sequence[i]",
              "then": [
                {
                  "id": "u50",
                  "kind": "call",
                  "call": "beep(110, 500)"
                },
                {
                  "id": "u51",
                  "kind": "return",
                  "value": "False"
                }
              ],
              "else": [],
              "comment": "Comparing input with memory: the i-th key must match the i-th item of the sequence."
            }
          ]
        },
        {
          "id": "u52",
          "kind": "return",
          "value": "True"
        }
      ],
      "comment": "The player repeats the sequence; returns False at the first mistake."
    },
    {
      "name": "game_over",
      "params": [
        {
          "name": "score",
          "type": "int"
        }
      ],
      "returnType": null,
      "blocks": [
        {
          "id": "u53",
          "kind": "call",
          "call": "color(LIGHT_RED)"
        },
        {
          "id": "u54",
          "kind": "call",
          "call": "at(1, 23)"
        },
        {
          "id": "u55",
          "kind": "output",
          "values": "\"Wrong! You remembered a sequence of\" + \" \" + str(score) + \".\"",
          "newline": false
        },
        {
          "id": "u56",
          "kind": "for",
          "variable": "i",
          "range": "4",
          "body": [
            {
              "id": "u57",
              "kind": "call",
              "call": "beep(440 - i * 80, 120)"
            }
          ]
        }
      ],
      "comment": "Game over: the score and falling tones."
    }
  ],
  "comment": "Solution to the task “A slower game”: Make the game never speed up below 300 ms per field. Find the place where the light-up length is computed."
}
