Models 44
Train snakeOne-line

gpt-oss-120b

by OpenAIReasoning1 console error
Open in new window

Running live in a sandboxed frame. Click inside it before using the keyboard.

How it went

Total time
31.5 s
Cost
$0.0004
Output tokens
2,202
Spent thinking
542 tokens
Speed
71.2 tok/s
Console errors
1
First token after
572 ms
First visible text after
8.8 s
Prompt tokens
425
Code size
5.4 KB
Finish reason
stop
Served by
AkashML
Model id
openai/gpt-oss-120b
Reasoning sent
{"effort":"medium"}
Max output
64,000 tokens
Generated
Nov 11, 2026

Console errors

Caught in the first 3 seconds after load, in a sandboxed iframe like the one above.

Cannot read properties of undefined (reading 'forEach')

What it was asked

The prompt
“make me a snake game where the snake is a little train with a locomotive and colorful wagons, picking up passengers waiting on the platform”
System prompt, sent with every prompt
You are generating a self-contained web demo. It will be rendered automatically in a sandboxed iframe and compared side by side with the output of other AI models given the exact same task.

Output rules:
- Reply with exactly ONE fenced code block containing the complete file: ```html (or ```svg when the task asks for an SVG). No text before or after the code block.
- Everything must be inline: CSS in <style>, JavaScript in <script>. No external files, images, fonts or network requests, except the CDN URLs explicitly allowed by the task.
- Do not use localStorage, sessionStorage, cookies, IndexedDB, alert(), confirm(), prompt() or window.open(): they are blocked in the sandbox and will throw.
- The page must look right in a 1280×800 viewport and adapt gracefully down to 375px wide. No horizontal scrollbar.
- Something meaningful must be visible on first paint: a screenshot is taken automatically about 3 seconds after load, without any interaction.
- Everything that makes the demo interesting must be visible within the first seconds: nothing hidden behind a high score, a later level or a long play session.
- Games must start in a self-playing demo mode (a simple AI plays) behind a "Click or press a key to start" message, so the game can be judged in action without playing.
- The iframe only receives keyboard events after the user clicks it: if the demo uses the keyboard, display a clear "Click or press a key to start" message.
- The code must run with zero errors in the console of the latest Chrome.

Code run-1.html · 198 lines

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Train Snake Demo</title>
<style>
  html,body{margin:0;height:100%;overflow:hidden;background:#111;color:#eee;font-family:sans-serif}
  #game{position:relative;width:100%;height:100%;display:flex;align-items:center;justify-content:center}
  canvas{background:#222;border:2px solid #555;box-sizing:border-box}
  #overlay{
    position:absolute;top:0;left:0;right:0;bottom:0;
    background:rgba(0,0,0,0.7);color:#fff;
    display:flex;flex-direction:column;align-items:center;justify-content:center;
    font-size:1.5rem;cursor:pointer;user-select:none;z-index:10;
  }
  #score{position:absolute;top:10px;left:10px;font-size:1rem;color:#fff;z-index:5}
</style>
</head>
<body>
<div id="game">
  <canvas id="c"></canvas>
  <div id="score">Score: 0</div>
  <div id="overlay">Click or press any key to start</div>
</div>
<script>
(() => {
  const canvas = document.getElementById('c');
  const ctx = canvas.getContext('2d');
  const overlay = document.getElementById('overlay');
  const scoreEl = document.getElementById('score');

  const COLS = 20, ROWS = 20;
  const SPEED = 120; // ms per tick
  const colors = ['#e74c3c','#f1c40f','#1abc9c','#9b59b6','#3498db'];
  let cellSize, width, height;
  let train, direction, nextDir, passengers, score, timer, aiActive;

  // ---- Utility ----
  const randPos = () => ({
    x: Math.floor(Math.random()*COLS),
    y: Math.floor(Math.random()*ROWS)
  });

  const distMan = (a,b)=>Math.abs(a.x-b.x)+Math.abs(a.y-b.y);

  // ---- Init ----
  function resize() {
    const size = Math.min(window.innerWidth, window.innerHeight) * 0.9;
    width = height = Math.floor(size);
    canvas.width = width;
    canvas.height = height;
    cellSize = width / COLS;
  }
  resize();
  window.addEventListener('resize', resize);

  function reset() {
    train = [{x:Math.floor(COLS/2), y:Math.floor(ROWS/2)}]; // locomotive
    direction = {x:0, y:-1};
    nextDir = direction;
    passengers = [randPos()];
    score = 0;
    scoreEl.textContent = 'Score: 0';
    aiActive = true;
  }

  // ---- Drawing ----
  function drawCell(x,y,color){
    ctx.fillStyle = color;
    ctx.fillRect(x*cellSize, y*cellSize, cellSize, cellSize);
    ctx.strokeStyle = '#111';
    ctx.strokeRect(x*cellSize, y*cellSize, cellSize, cellSize);
  }

  function render(){
    ctx.clearRect(0,0,width,height);
    // platform (border)
    ctx.strokeStyle = '#555';
    ctx.lineWidth = 4;
    ctx.strokeRect(0,0,width,height);
    // passengers
    passengers.forEach(p=>{
      drawCell(p.x,p.y,'#fff');
    });
    // train
    train.forEach((seg,i)=>{
      const isLocomotive = i===0;
      const col = isLocomotive? '#ff6600' : colors[(i-1)%colors.length];
      drawCell(seg.x, seg.y, col);
    });
  }

  // ---- Game Logic ----
  function step(){
    // AI decides direction
    if(aiActive){
      const head = train[0];
      const target = passengers[0];
      let cand = {x:0,y:0};
      if(head.x < target.x) cand={x:1,y:0};
      else if(head.x > target.x) cand={x:-1,y:0};
      else if(head.y < target.y) cand={x:0,y:1};
      else if(head.y > target.y) cand={x:0,y:-1};

      // avoid reversing onto own body
      const nxt = {x:head.x+ cand.x, y:head.y+ cand.y};
      if(train.length>1 && nxt.x===train[1].x && nxt.y===train[1].y){
        // try orthogonal move
        const alternatives = [{x:0,y:1},{x:0,y:-1},{x:1,y:0},{x:-1,y:0}];
        for(const alt of alternatives){
          const altPos={x:head.x+alt.x, y:head.y+alt.y};
          if(!(altPos.x===train[1].x && altPos.y===train[1].y)) {cand=alt;break;}
        }
      }
      direction = cand;
    }

    // apply manual override
    direction = nextDir;

    const head = train[0];
    const newHead = {x: head.x + direction.x, y: head.y + direction.y};

    // wall collision
    if(newHead.x<0||newHead.x>=COLS||newHead.y<0||newHead.y>=ROWS){
      gameOver();
      return;
    }
    // self collision
    if(train.some(seg=>seg.x===newHead.x && seg.y===newHead.y)){
      gameOver();
      return;
    }

    train.unshift(newHead);

    // eat passenger?
    if(passengers.some(p=>p.x===newHead.x && p.y===newHead.y)){
      score++;
      scoreEl.textContent='Score: '+score;
      // new passenger
      let np;
      do { np = randPos(); } while(train.some(seg=>seg.x===np.x && seg.y===np.y));
      passengers[0]=np;
    }else{
      train.pop(); // move forward
    }

    render();
  }

  function gameOver(){
    clearInterval(timer);
    overlay.textContent = 'Game Over – Click or press any key to restart';
    overlay.style.display='flex';
    aiActive = false;
  }

  // ---- Controls ----
  document.addEventListener('keydown', e=>{
    if(overlay.style.display!=='none'){
      startGame();
      return;
    }
    const key = e.key;
    const dirMap = {
      ArrowUp:    {x:0,y:-1},
      ArrowDown:  {x:0,y:1},
      ArrowLeft:  {x:-1,y:0},
      ArrowRight: {x:1,y:0},
      w: {x:0,y:-1},
      s: {x:0,y:1},
      a: {x:-1,y:0},
      d: {x:1,y:0}
    };
    if(dirMap[key]){
      const proposed = dirMap[key];
      // prevent reverse
      if(train.length>1 && proposed.x===-direction.x && proposed.y===-direction.y) return;
      nextDir = proposed;
      aiActive = false; // player takes over
    }
  });
  overlay.addEventListener('click', startGame);

  function startGame(){
    overlay.style.display='none';
    reset();
    render();
    timer = setInterval(step, SPEED);
  }

  // initial paint
  render();
})();
</script>
</body>
</html>
Reasoning trace (542 tokens)
We need to output a single HTML file with all inline CSS and JS. Should be a snake-game styled as a train with locomotive and colorful wagons, picking up passengers waiting on platform. Must show something on first paint. Have a start message to allow clicking/pressing any key, and then auto-play AI or demo mode. The instructions: "Games must start in a self-playing demo mode (a simple AI plays) behind a 'Click or press a key to start' message, so the game can be judged in action without playing."

Thus we need to show a start overlay with message. When user clicks, we start the game, and also start an AI that moves the train automatically to collect passengers.

Implement a grid-based snake (train). Use canvas for rendering. Represent train as array of positions; first element locomotive (different color). Wagons have cycles colors.

Passengers as items at random positions.

AI can be simple: always move towards nearest passenger using Manhattan path, using BFS or simple greedy.

Responsive: occupy full width? We'll set canvas size scaling based on viewport.

We must avoid external resources except allowed CDN, but we can use no CDN.

Implement clean code.

Let's design:

HTML: Canvas inside container.

CSS: body margin zero, flex center.

JS: Define grid size (e.g., 20 columns x 20 rows). Each cell size computed based on canvas width/height.

Game state: train = [{x, y}], direction = {x:0, y:-1}.

Will have function gameLoop with setInterval at 100ms.

When start overlay is clicked or keydown, hide overlay, start game loop and set AI active.

AI: compute target = nearest passenger. Determine direction to move: if target.x > head.x => move right, else if < => left, similarly for y. Ensure not reversing onto its own body (like snake). Could add simple check.

When train eats passenger: grow (add new segment), increase score, spawn new passenger.

When collision with self or wall: reset.

Make sure visible on first paint: show overlay plus some static background maybe.

Make sure no horizontal scrollbar: canvas fits.

Let's code.

We need to adapt canvas size to window.innerWidth while maintaining aspect ratio.

Let's set canvas width = min(600, window.innerWidth - 20) and height = width (square). Then scale cells accordingly.

Add event listeners for key arrows to allow manual control after start (but optional).

But AI will also run; maybe AI overrides key input? We'll let manual after start.

The overlay: a semi-transparent div with message.

We'll also add scoreboard.

Implement.

Let's craft final HTML.