{
"id": "112",
"prompt": "Snake on a 16x16 grid. Steer relatively: a tap on the left half of the screen turns the snake left, the right half turns right; it advances one cell every 6 frames. Eat the red food to grow (food cell from an LCG hash of a round counter), die on walls or your own body, then show a game-over screen with the score and tap to restart. There are no persistent arrays - keep the body as packed x*16+y cells in a ring of state slots.",
"solution_rl": "fn hash(seed: i32) -> i32 {\n let mut h: i32 = seed * 747796405 + 2891336453;\n h = (h >> 16) ^ h;\n if h < 0 { h = 0 - h; }\n h\n}\n\nfn frame(t: i32) {\n let w: i32 = host::display::width();\n let cs: i32 = w / 16;\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n host::display::state_set(1, 0);\n host::display::state_set(2, 2);\n host::display::state_set(3, 3);\n host::display::state_set(4, 1);\n host::display::state_set(5, 0);\n host::display::state_set(6, 0);\n host::display::state_set(20, 3 * 16 + 8);\n host::display::state_set(21, 4 * 16 + 8);\n host::display::state_set(22, 5 * 16 + 8);\n }\n let down: i32 = host::display::pointer_down();\n let tap: bool = down == 1 && host::display::state_get(7) == 0;\n host::display::state_set(7, down);\n let phase: i32 = host::display::state_get(6);\n if phase == 1 {\n host::display::clear(0x180404);\n host::display::draw_string(130, 190, \"GAME OVER\", 0xff4040, 4);\n host::display::draw_string(160, 260, \"ATE\", 0x808080, 2);\n host::display::draw_number(230, 260, host::display::state_get(5), 0xffffff, 2);\n host::display::draw_string(140, 320, \"TAP TO RESTART\", 0x808080, 2);\n if tap { host::display::state_set(0, 0); }\n } else {\n let mut dir: i32 = host::display::state_get(1);\n if tap {\n if host::display::pointer_x() < w / 2 {\n dir = (dir + 3) % 4;\n } else {\n dir = (dir + 1) % 4;\n }\n host::display::state_set(1, dir);\n }\n let mut hp: i32 = host::display::state_get(2);\n let mut len: i32 = host::display::state_get(3);\n if t % 6 == 0 {\n let head: i32 = host::display::state_get(20 + hp);\n let mut hx: i32 = head / 16;\n let mut hy: i32 = head % 16;\n if dir == 0 { hx = hx + 1; }\n if dir == 1 { hy = hy + 1; }\n if dir == 2 { hx = hx - 1; }\n if dir == 3 { hy = hy - 1; }\n if hx < 0 || hx > 15 || hy < 0 || hy > 15 {\n host::display::state_set(6, 1);\n } else {\n let np: i32 = hx * 16 + hy;\n let mut s: i32 = 0;\n let mut dead: i32 = 0;\n while s < len - 1 {\n let slot: i32 = 20 + (((hp - s) % 40 + 40) % 40);\n if host::display::state_get(slot) == np { dead = 1; }\n s = s + 1;\n }\n if dead == 1 {\n host::display::state_set(6, 1);\n } else {\n hp = (hp + 1) % 40;\n host::display::state_set(20 + hp, np);\n host::display::state_set(2, hp);\n let fr: i32 = host::display::state_get(4);\n let f: i32 = hash(fr);\n if hx == f % 16 && hy == (f / 16) % 16 {\n host::display::state_set(4, fr + 1);\n host::display::state_set(5, host::display::state_get(5) + 1);\n if len < 39 { len = len + 1; }\n host::display::state_set(3, len);\n }\n }\n }\n }\n host::display::clear(0x001008);\n let f2: i32 = hash(host::display::state_get(4));\n host::display::fill_rect((f2 % 16) * cs + 4, ((f2 / 16) % 16) * cs + 4, cs - 8, cs - 8, 0xff3344);\n hp = host::display::state_get(2);\n len = host::display::state_get(3);\n let mut d: i32 = 0;\n while d < len {\n let slot2: i32 = 20 + (((hp - d) % 40 + 40) % 40);\n let p: i32 = host::display::state_get(slot2);\n let mut col: i32 = 0x00cc66;\n if d == 0 { col = 0x88ffbb; }\n host::display::fill_rect((p / 16) * cs + 2, (p % 16) * cs + 2, cs - 4, cs - 4, col);\n d = d + 1;\n }\n host::display::draw_number(8, 4, host::display::state_get(5), 0xffffff, 2);\n }\n host::display::present();\n}\n",
"tags": [
"game",
"state",
"prng",
"loops"
]
}