markuz89's picture
MiniJEV v2 model export
026f63e verified
Raw History Blame Contribute Delete
7.14 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MiniJEV Snake</title>
<style>
:root { --bg:#0f1115; --panel:#171a21; --line:#262b36; --text:#e6e8ee; --muted:#8b93a7;
--snake:#4ade80; --head:#22c55e; --food:#f43f5e; --accent:#60a5fa; --cell:#12151b; }
* { box-sizing: border-box; }
body { margin:0; background:var(--bg); color:var(--text); font:14px/1.4 system-ui, sans-serif; }
main { display:flex; gap:20px; padding:20px; flex-wrap:wrap; justify-content:center; }
canvas { background:var(--cell); border:1px solid var(--line); border-radius:8px; max-width:100%; }
.panel { background:var(--panel); border:1px solid var(--line); border-radius:8px; padding:16px; width:320px; }
h1 { font-size:16px; margin:0 0 12px; letter-spacing:.02em; }
h1 span { color:var(--muted); font-weight:400; }
.stats { display:grid; grid-template-columns: 1fr 1fr; gap:8px 16px; margin-bottom:16px; }
.stat .k { color:var(--muted); font-size:12px; }
.stat .v { font-size:18px; font-variant-numeric: tabular-nums; }
.bar { display:grid; grid-template-columns: 56px 1fr 52px; align-items:center; gap:8px; margin:6px 0; }
.bar .track { height:14px; background:var(--cell); border-radius:4px; overflow:hidden; }
.bar .fill { height:100%; background:var(--accent); width:0; transition: width .08s linear; }
.bar.chosen .fill { background:var(--snake); }
.bar .pct { text-align:right; font-variant-numeric: tabular-nums; }
.row { display:flex; gap:6px; margin:10px 0; flex-wrap:wrap; }
button, select { background:var(--cell); color:var(--text); border:1px solid var(--line); border-radius:6px;
padding:6px 10px; cursor:pointer; font:inherit; }
button.on { border-color:var(--accent); color:var(--accent); }
.muted { color:var(--muted); font-size:12px; }
</style>
</head>
<body>
<main>
<canvas id="board" width="560" height="560"></canvas>
<div class="panel">
<h1>MiniJEV <span>· Snake decision demo</span></h1>
<div class="stats">
<div class="stat"><div class="k">Score</div><div class="v" id="score">0</div></div>
<div class="stat"><div class="k">Step</div><div class="v" id="step">0</div></div>
<div class="stat"><div class="k">Decision latency</div><div class="v" id="lat">–</div></div>
<div class="stat"><div class="k">Sim steps/s · FPS</div><div class="v" id="fps">–</div></div>
<div class="stat"><div class="k">Games · avg</div><div class="v" id="games">0</div></div>
<div class="stat"><div class="k">Best</div><div class="v" id="best">0</div></div>
</div>
<div class="muted">Question: “Choose the next move” — choice: <b id="choice">–</b>,
confidence <b id="conf">–</b></div>
<div id="bars"></div>
<div class="row" id="speeds">
<button data-speed="realtime">Realtime</button>
<button data-speed="fast">Fast</button>
<button data-speed="max">Maximum</button>
</div>
<div class="row">
<select id="agent"></select>
<select id="size"><option>10</option><option>15</option><option>20</option></select>
<button id="reset">New game</button>
</div>
<div class="muted" id="info"></div>
<div class="muted" id="last"></div>
</div>
</main>
<script>
const ACTIONS = ["UP", "DOWN", "LEFT", "RIGHT"];
const $ = id => document.getElementById(id);
const cv = $("board"), ctx = cv.getContext("2d");
const css = n => getComputedStyle(document.documentElement).getPropertyValue(n).trim();
$("bars").innerHTML = ACTIONS.map(a =>
`<div class="bar" id="bar-${a}"><span>${a}</span><div class="track"><div class="fill"></div></div><span class="pct">–</span></div>`).join("");
const post = body => fetch("/api/control", {method: "POST", body: JSON.stringify(body)});
document.querySelectorAll("#speeds button").forEach(b => b.onclick = () => post({speed: b.dataset.speed}));
$("reset").onclick = () => post({reset: true});
$("agent").onchange = e => post({agent: e.target.value});
$("size").onchange = e => post({board_size: +e.target.value});
const KEYS = {ArrowUp: "UP", ArrowDown: "DOWN", ArrowLeft: "LEFT", ArrowRight: "RIGHT", w: "UP", s: "DOWN", a: "LEFT", d: "RIGHT"};
addEventListener("keydown", e => { if (KEYS[e.key]) { post({key: KEYS[e.key]}); e.preventDefault(); } });
let frames = 0, fps = 0, fpsT = performance.now(), agentsFilled = false;
function draw(s) {
const n = Math.max(s.height, s.width), cell = Math.floor(Math.min(cv.width, cv.height) / n);
ctx.fillStyle = css("--cell"); ctx.fillRect(0, 0, cv.width, cv.height);
ctx.strokeStyle = "#1b1f28";
for (let i = 0; i <= n; i++) {
ctx.beginPath(); ctx.moveTo(i * cell, 0); ctx.lineTo(i * cell, n * cell); ctx.stroke();
ctx.beginPath(); ctx.moveTo(0, i * cell); ctx.lineTo(n * cell, i * cell); ctx.stroke();
}
if (s.food) { ctx.fillStyle = css("--food"); ctx.beginPath();
ctx.arc(s.food[1] * cell + cell / 2, s.food[0] * cell + cell / 2, cell * 0.36, 0, 7); ctx.fill(); }
s.body.forEach(([r, c], i) => {
const t = i / Math.max(1, s.body.length - 1);
ctx.fillStyle = i === 0 ? css("--head") : `rgba(74,222,128,${1 - 0.55 * t})`;
const p = i === 0 ? 1 : 2;
ctx.fillRect(c * cell + p, r * cell + p, cell - 2 * p, cell - 2 * p);
});
}
function update(s) {
draw(s);
$("score").textContent = s.score; $("step").textContent = s.step;
$("lat").textContent = s.decision.latency_ms != null ? s.decision.latency_ms.toFixed(2) + " ms" : "–";
$("fps").textContent = `${Math.round(s.sim_steps_per_sec)} · ${fps}`;
$("games").textContent = s.games_played + (s.avg_score != null ? " · " + s.avg_score.toFixed(1) : "");
$("best").textContent = s.best_score;
$("choice").textContent = s.decision.choice || "–";
$("conf").textContent = s.decision.confidence != null ? (100 * s.decision.confidence).toFixed(1) + "%" : "n/a (scripted)";
ACTIONS.forEach(a => {
const p = s.decision.scores[a] ?? 0, el = $("bar-" + a);
el.querySelector(".fill").style.width = (100 * p) + "%";
el.querySelector(".pct").textContent = (100 * p).toFixed(p < 0.1 ? 1 : 0) + "%";
el.classList.toggle("chosen", a === s.decision.choice);
});
document.querySelectorAll("#speeds button").forEach(b => b.classList.toggle("on", b.dataset.speed === s.speed));
if (!agentsFilled) {
$("agent").innerHTML = s.agents.map(a => `<option ${a === s.agent ? "selected" : ""}>${a}</option>`).join("");
$("size").value = String(s.height); agentsFilled = true;
}
$("info").textContent = `agent: ${s.agent}` + (s.model ? ` · model: ${s.model}` : "") +
(s.agent === "human" ? " · use arrow keys / WASD" : "");
if (s.last_game) $("last").textContent = `last game: ${s.last_game.score} food, ${s.last_game.steps} steps, ended by ${s.last_game.reason}`;
}
async function loop() {
try { update(await (await fetch("/api/state")).json()); } catch (e) {}
frames++; const now = performance.now();
if (now - fpsT > 1000) { fps = Math.round(frames * 1000 / (now - fpsT)); frames = 0; fpsT = now; }
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>