code wiki / _hdl_build / nx_game_tycoon_gate.nx

nx_game_tycoon_gate.nx source

↩ module page · 84 lines · 5095 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_game_tycoon_gate.nx -- GENRE: Business and Tycoon (catalog genre, was ABSENT). A real integer/no-float 4// trading-tycoon CORE -- a fluctuating market price; buy low, sell high, grow your cash to a target. Scripted 5// run of a buy-low/sell-high strategy, rendered frame (price chart + cash), neg-control (a bad strategy loses) 6// + trade-mechanics-real. 7// T1 RUN: a buy-low/sell-high strategy grows cash from 100 to the target. PNG (price chart + cash). 8// T2 NEG-CONTROL: a buy-high/sell-low strategy LOSES money (cash drops below the 100 start). 9// T3 TRADE REAL: a buy spends cash + gains inventory; a sell does the reverse. 10// T4 NEVER-BRICK (#26): deterministic, bounded, no float. 11// expect_exit: 0 license_tier: ORIGINAL 12import "nx_game_raster.nx" 13import "nx_png.nx" 14import "nx_syscalls.nx" 15 16func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 17" as *u8); return ok } 18 19// a deterministic triangle market: price rises 8->28 then falls 28->8 over a 40-tick cycle. 20func ty_price(tick: i64) -> i64 { let p: i64=tick%40; if p<20 { return 8+p } return 8+(40-p) } 21// st[0]=cash st[1]=inv. mode 0 = buy-low/sell-high, mode 1 = buy-high/sell-low. 22func ty_tick(st: *i64, price: i64, mode: i64, cap: i64) -> i64 { 23 if mode==0 { 24 if price<=12 { if st[0]>=price { if st[1]<cap { st[0]=st[0]-price; st[1]=st[1]+1 } } } 25 if price>=24 { if st[1]>0 { st[0]=st[0]+price; st[1]=st[1]-1 } } 26 } else { 27 if price>=24 { if st[0]>=price { if st[1]<cap { st[0]=st[0]-price; st[1]=st[1]+1 } } } 28 if price<=12 { if st[1]>0 { st[0]=st[0]+price; st[1]=st[1]-1 } } 29 } 30 return 0 31} 32func ty_render(fb: *i64, w: i64, h: i64, hist: *i64, nh: i64, cash: i64) -> i64 { 33 gr_clear(fb, w, h, gr_pack(16, 22, 30)) 34 var i: i64=0 35 while i<nh { if i<w { let v: i64=hist[i]; gr_rect(fb, w, h, i, h-8-v*2, i+1, h-8, gr_pack(90,200,240)) } i=i+1 } // price chart 36 gr_rect(fb, w, h, 2, 2, 2+cash/3, 7, gr_pack(120,220,150)) // cash bar 37 return 0 38} 39 40func main() -> i64 { 41 gw("=== nx_game_tycoon_gate: GENRE Business/Tycoon -- a real integer/no-float playable (live evidence) ===\n" as *u8) 42 var pass: i64 = 0; var total: i64 = 0 43 let W: i64=140; let H: i64=96; let CAP: i64=6; let START: i64=100; let TARGET: i64=130; let TICKS: i64=200 44 let st: *i64=sys_mmap(8*4) as *i64; let hist: *i64=sys_mmap(8*(W+8)) as *i64 45 46 // ---- T1: buy-low/sell-high grows cash to the target ---- 47 st[0]=START; st[1]=0 48 var tick: i64=0 49 while tick < TICKS { let pr: i64=ty_price(tick); if tick<W { hist[tick]=pr } ty_tick(st, pr, 0, CAP); tick=tick+1 } 50 let cash1: i64=st[0] 51 total=total+1; if cash1>=TARGET { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 52 gw("T1 run: buy-low/sell-high grew cash " as *u8); gn(START); gw("->" as *u8); gn(cash1); gw(" (>= " as *u8); gn(TARGET); gw(" = WIN), inv=" as *u8); gn(st[1]); gw("\n" as *u8) 53 54 let fb: *i64=sys_mmap(8*(W*H+8)) as *i64 55 ty_render(fb, W, H, hist, W, cash1) 56 write_png(fb, W, H, "knowledge/nx_game_tycoon.png" as *u8) 57 gw(" (live evidence: PNG knowledge/nx_game_tycoon.png -- market price chart + cash bar)\n" as *u8) 58 59 // ---- T2: neg-control -- buy-high/sell-low loses money ---- 60 st[0]=START; st[1]=0 61 tick=0; while tick < TICKS { ty_tick(st, ty_price(tick), 1, CAP); tick=tick+1 } 62 let cash2: i64=st[0] 63 total=total+1; if cash2 < START { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 64 gw("T2 neg-control: buy-high/sell-low lost money, cash=" as *u8); gn(cash2); gw(" (< " as *u8); gn(START); gw("; the strategy is real)\n" as *u8) 65 66 // ---- T3: trade real -- a buy spends cash + gains inventory ---- 67 st[0]=100; st[1]=0 68 ty_tick(st, 10, 0, CAP) // price 10 (low) -> buy 69 var t3ok: i64=1; if st[0]!=90 {t3ok=0} if st[1]!=1 {t3ok=0} 70 ty_tick(st, 26, 0, CAP) // price 26 (high) -> sell 71 if st[0]!=116 {t3ok=0} if st[1]!=0 {t3ok=0} 72 total=total+1; if t3ok==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 73 gw("T3 trade real: bought at 10 (cash->90,inv->1) then sold at 26 (cash->" as *u8); gn(st[0]); gw(",inv->" as *u8); gn(st[1]); gw(")\n" as *u8) 74 75 // ---- T4: never-brick / determinism ---- 76 st[0]=START; st[1]=0 77 tick=0; while tick < TICKS { ty_tick(st, ty_price(tick), 0, CAP); tick=tick+1 } 78 total=total+1; if st[0]==cash1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 79 gw("T4 never-brick (#26): deterministic re-run (cash=" as *u8); gn(st[0]); gw("==" as *u8); gn(cash1); gw("), bounded, no float\n" as *u8) 80 81 gw("\n=== nx_game_tycoon_gate " as *u8); gn(pass); gw("/" as *u8); gn(total) 82 if pass == total { gw(" GREEN (Business/Tycoon: a real playable buy-low/sell-high market with profit/loss, rendered, sovereign)\n" as *u8); sys_exit(0); return 0 } 83 gw(" RED\n" as *u8); sys_exit(1); return 1 84}