File size: 2,839 Bytes
87713f8
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
{
  "id": "snake_variant",
  "prompt": "Напиши змейку в одном HTML-файле. Поле 14×14, управление WASD, шаг каждые 180 мс.",
  "answer": "<!doctype html>\n<html lang=\"ru\">\n<head>\n<meta charset=\"utf-8\">\n<title>игра, в которой змейка ест яблоки и растёт, поле 14×14</title>\n</head>\n<body>\n<h1>игра, в которой змейка ест яблоки и растёт, поле 14×14</h1>\n<canvas id=\"board\" width=\"288\" height=\"288\" style=\"background:#eee\"></canvas>\n<p>Очки: <output id=\"score\">0</output></p>\n<p id=\"status\"></p>\n<button id=\"restart\">Заново</button>\n<script>\nconst canvas = document.getElementById('board');\nconst ctx = canvas.getContext('2d');\nconst n = 14, step = canvas.width / n;\nconst score = document.getElementById('score');\nconst status = document.getElementById('status');\nconst keys = {w:[0,-1], a:[-1,0], s:[0,1], d:[1,0]};\nlet snake, food, dx, dy, next, timer;\nconst same = (a,b) => a.x===b.x && a.y===b.y;\n\nfunction draw() {\n  ctx.clearRect(0,0,canvas.width,canvas.height);\n  for (const [points,color] of [[snake,'green'],[[food],'tomato']]) {\n    ctx.fillStyle = color;\n    for (const {x,y} of points) ctx.fillRect(x*step,y*step,step-1,step-1);\n  }\n}\n\nfunction finish(message) {\n  clearInterval(timer);\n  status.textContent = message;\n}\n\nfunction placeFood() {\n  const free = [];\n  for (let y=0; y<n; y++) for (let x=0; x<n; x++) {\n    const p = {x,y};\n    if (!snake.some(s => same(s,p))) free.push(p);\n  }\n  return free[Math.floor(Math.random()*free.length)];\n}\n\nfunction tick() {\n  [dx,dy] = next;\n  const head = {x:snake[0].x+dx,y:snake[0].y+dy};\n  if (head.x<0 || head.y<0 || head.x>=n || head.y>=n) return finish('Игра окончена');\n  const eat = same(head,food);\n  const body = eat ? snake : snake.slice(0,-1);\n  if (body.some(p => same(p,head))) return finish('Игра окончена');\n  snake.unshift(head);\n  if (eat) {\n    score.textContent = snake.length-3;\n    if (snake.length === n*n) return finish('Победа!');\n    food = placeFood();\n  } else snake.pop();\n  draw();\n}\n\nfunction start() {\n  clearInterval(timer);\n  snake = [{x:4,y:4},{x:3,y:4},{x:2,y:4}];\n  food = {x:7,y:4};\n  [dx,dy] = next = [1,0];\n  score.textContent = '0';\n  status.textContent = '';\n  draw();\n  timer = setInterval(tick,180);\n}\nwindow.addEventListener('keydown', e => {\n  const dir = keys[e.key.toLowerCase()];\n  if (!dir) return;\n  e.preventDefault();\n  if (dir[0] !== -dx || dir[1] !== -dy) next = dir;\n});\ndocument.getElementById('restart').onclick = start;\nstart();\n</script>\n</body>\n</html>",
  "finish_reason": "stop",
  "pass": true,
  "detail": "действие выполнено в Chromium"
}