code wiki / _hdl_build / nx_maze_gate.nx
nx_maze_gate.nx source
↩ module page · 68 lines · 4760 B
1// nx_maze_gate.nx -- GATE for the provably-solvable maze rung (procgen-solvable + gate-solvable + rules-engine).
2// Proves: a generated maze is a SPANNING TREE (every cell reachable BY CONSTRUCTION), the solver finds a
3// start->exit path, the solver DETECTS an unsolvable level (NEG-CONTROL: seal the exit -> UNSOLVABLE, so the
4// solvability gate is real, not a rubber-stamp -- the property that makes autonomous emission safe), generation
5// is DETERMINISTIC (same seed -> identical) and VARIES (different seed -> different maze), and the render is
6// sovereign (pure ASCII, 0 third-party). Emits knowledge/staging/game/maze.txt. license_tier: ORIGINAL expect_exit: 0
7import "nx_maze_gen.nx"
8import "nx_ad_serve.nx"
9import "nx_syscalls.nx"
10
11func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
12func g_i(v: i64) -> i64 { let t: *u8 = sys_mmap(24); var m: i64 = v; var k: i64 = 0; if m == 0 { t[0]=48 as u8; k=1 } while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0 { o[w]=t[q]; w=w+1; q=q-1 } sys_write(1,o,w); return 0 }
13func eqbytes(a: *u8, b: *u8, n: i64) -> i64 { var i: i64 = 0; while i < n { if a[i] != b[i] { return 0 } i = i + 1 } return 1 }
14
15func main(argc: i64, argv: *i64) -> i64 {
16 g_p("=== nx_maze_gate (procgen-solvable + gate-solvable + rules-engine; pure-integer, sovereign) ===\n" as *u8)
17 let W: i64 = 8; let H: i64 = 8; let N: i64 = 64
18 let start: i64 = 0; let exit_c: i64 = N - 1
19 let cells: *u8 = sys_mmap(N); let cellsB: *u8 = sys_mmap(N); let cellsC: *u8 = sys_mmap(N)
20 let dist: *i64 = sys_mmap(8*N) as *i64; let queue: *i64 = sys_mmap(8*N) as *i64
21 var pass: i64 = 0; var tot: i64 = 0
22
23 // T1 SPANNING TREE: every cell reachable by construction
24 mz_gen(7, W, H, cells)
25 mz_solve(cells, W, H, start, exit_c, dist, queue)
26 let reach: i64 = mz_reachable(dist, N)
27 tot = tot + 1
28 if reach == N { pass = pass + 1; g_p("PASS T1 procgen-solvable: maze is a spanning tree -- all 64/64 cells reachable BY CONSTRUCTION\n" as *u8) } else { g_p("FAIL T1 reachable=" as *u8); g_i(reach); g_p("/64\n" as *u8) }
29
30 // T2 SOLVE: start->exit path exists with a real length
31 let d: i64 = mz_solve(cells, W, H, start, exit_c, dist, queue)
32 tot = tot + 1
33 if d >= 14 { pass = pass + 1; g_p("PASS T2 gate-solvable: solver found a start->exit path, length=" as *u8); g_i(d); g_p(" (>=14 manhattan floor)\n" as *u8) } else { g_p("FAIL T2 d=" as *u8); g_i(d); g_p("\n" as *u8) }
34
35 // T3 NEG-CONTROL: seal the exit -> solver MUST report UNSOLVABLE
36 mz_gen(7, W, H, cells)
37 mz_seal(cells, W, H, exit_c)
38 let dn: i64 = mz_solve(cells, W, H, start, exit_c, dist, queue)
39 tot = tot + 1
40 if dn == (0 - 1) { pass = pass + 1; g_p("PASS T3 neg-control: sealed exit -> solver reports UNSOLVABLE (-1) -- the solvability gate is REAL\n" as *u8) } else { g_p("FAIL T3 dn=" as *u8); g_i(dn); g_p(" (should be -1)\n" as *u8) }
41
42 // T4 DETERMINISM: same seed -> identical maze
43 mz_gen(7, W, H, cells); mz_gen(7, W, H, cellsB)
44 tot = tot + 1
45 if eqbytes(cells, cellsB, N) == 1 { pass = pass + 1; g_p("PASS T4 determinism: seed 7 -> byte-identical maze (reproducible emission)\n" as *u8) } else { g_p("FAIL T4\n" as *u8) }
46
47 // T5 VARIETY: different seed -> different maze (real generation)
48 mz_gen(99, W, H, cellsC)
49 tot = tot + 1
50 if eqbytes(cells, cellsC, N) == 0 { pass = pass + 1; g_p("PASS T5 variety: seed 7 vs 99 -> different mazes (real generation, not a constant)\n" as *u8) } else { g_p("FAIL T5 mazes identical\n" as *u8) }
51
52 // T6 SOVEREIGN RENDER: ASCII maze -> file; markers present; 0 third-party
53 mz_gen(7, W, H, cells)
54 let buf: *u8 = sys_mmap(2048)
55 let blen: i64 = mz_ascii(cells, W, H, start, exit_c, buf)
56 sys_mkdir("knowledge/staging/game" as *u8, 0x1ed)
57 let fd: i64 = sys_openat_wr("knowledge/staging/game/maze.txt" as *u8, 420); if fd >= 0 { sys_write(fd, buf, blen); sys_close(fd) }
58 tot = tot + 1; var ok6: i64 = 1
59 if as_contains(buf, blen, "S" as *u8) != 1 { ok6 = 0 }
60 if as_contains(buf, blen, "E" as *u8) != 1 { ok6 = 0 }
61 if as_contains(buf, blen, "#" as *u8) != 1 { ok6 = 0 }
62 if as_has_thirdparty_js(buf, blen) != 0 { ok6 = 0 }
63 if ok6 == 1 { pass = pass + 1; g_p("PASS T6 sovereign render: ASCII maze (S/E/# present) -> knowledge/staging/game/maze.txt, 0 third-party\n" as *u8) } else { g_p("FAIL T6\n" as *u8) }
64
65 g_p("nx_maze_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot)
66 if pass == tot { g_p(" verdict=GREEN (provably-solvable procedural puzzle rung; safe to AUTO-EMIT)\n" as *u8); sys_exit(0); return 0 }
67 g_p(" verdict=RED\n" as *u8); sys_exit(1); return 1
68}