[verified] Snake 0.8: circular arena and larger 32px tiles
This commit is contained in:
+42
-6
@@ -1,4 +1,18 @@
|
||||
// Snake core — pure functions, no Vela APIs. Exports: initialSnake, step, turn, spawnFood.
|
||||
// Shared geometry: 32px tiles wholly inside the 368px circular arena.
|
||||
const ARENA = { cell: 32, size: 11, offset: 57, radius: 184, cells: [], rows: [], columns: [] }
|
||||
for (let y = 0; y < ARENA.size; y++) {
|
||||
ARENA.rows[y] = []
|
||||
ARENA.columns[y] = []
|
||||
}
|
||||
for (let y = 0; y < ARENA.size; y++) for (let x = 0; x < ARENA.size; x++) {
|
||||
const dx = Math.abs((x + 0.5) * ARENA.cell - 176) + ARENA.cell / 2
|
||||
const dy = Math.abs((y + 0.5) * ARENA.cell - 176) + ARENA.cell / 2
|
||||
if (dx * dx + dy * dy <= ARENA.radius * ARENA.radius) {
|
||||
ARENA.cells.push({ x, y })
|
||||
ARENA.rows[y].push(x)
|
||||
ARENA.columns[x].push(y)
|
||||
}
|
||||
}
|
||||
function initialSnake() {
|
||||
return [
|
||||
{ x: 6, y: 7 },
|
||||
@@ -12,13 +26,22 @@ function step(snake, dir, food, N, mode) {
|
||||
const head = snake[0]
|
||||
let nx = head.x + dir.x
|
||||
let ny = head.y + dir.y
|
||||
if (mode === 'wrap') {
|
||||
if (typeof N === 'object') {
|
||||
const row = N.rows[ny]
|
||||
if (!row || row.indexOf(nx) === -1) {
|
||||
if (mode !== 'wrap') return { snake, ate: false, alive: false }
|
||||
const span = dir.x ? N.rows[head.y] : N.columns[head.x]
|
||||
const opposite = dir.x + dir.y > 0 ? span[0] : span[span.length - 1]
|
||||
if (dir.x) nx = opposite
|
||||
else ny = opposite
|
||||
}
|
||||
} else if (mode === 'wrap') {
|
||||
nx = (nx + N) % N
|
||||
ny = (ny + N) % N
|
||||
} else if (nx < 0 || ny < 0 || nx >= N || ny >= N) {
|
||||
return { snake, ate: false, alive: false }
|
||||
}
|
||||
const ate = (nx === food.x && ny === food.y)
|
||||
const ate = !!food && (nx === food.x && ny === food.y)
|
||||
// tail cell vacates unless we grew, so exclude it from self-collision check
|
||||
const body = ate ? snake : snake.slice(0, snake.length - 1)
|
||||
for (let i = 0; i < body.length; i++) {
|
||||
@@ -44,13 +67,22 @@ function renderPieces(snakeCells, dir, N) {
|
||||
const D = { '1,0': 'e', '-1,0': 'w', '0,-1': 'n', '0,1': 's' }
|
||||
const opp = { e: 'w', w: 'e', n: 's', s: 'n' }
|
||||
const delta = d => N && d === N - 1 ? -1 : N && d === 1 - N ? 1 : d
|
||||
// Circular seams join the endpoints of this row/column, not the bounding grid.
|
||||
const side = (s, neighbor) => {
|
||||
let dx = neighbor.x - s.x, dy = neighbor.y - s.y
|
||||
if (N && typeof N === 'object') {
|
||||
if (Math.abs(dx) > 1) dx = dx > 0 ? -1 : 1
|
||||
if (Math.abs(dy) > 1) dy = dy > 0 ? -1 : 1
|
||||
} else { dx = delta(dx); dy = delta(dy) }
|
||||
return D[dx + ',' + dy]
|
||||
}
|
||||
return snakeCells.map((s, i) => {
|
||||
if (i === 0) return 'head_' + D[dir.x + ',' + dir.y]
|
||||
const prev = snakeCells[i - 1]
|
||||
const next = snakeCells[i + 1]
|
||||
const from = D[delta(prev.x - s.x) + ',' + delta(prev.y - s.y)] // side toward head
|
||||
const from = side(s, prev) // side toward head
|
||||
if (!next) return 'tail_' + opp[from] // tip points away from body
|
||||
const to = D[delta(next.x - s.x) + ',' + delta(next.y - s.y)] // side toward tail tip
|
||||
const to = side(s, next) // side toward tail tip
|
||||
if ((from === 'e' || from === 'w') && (to === 'e' || to === 'w')) return 'body_h'
|
||||
if ((from === 'n' || from === 's') && (to === 'n' || to === 's')) return 'body_v'
|
||||
// corner: name = the two sides it connects; pairs are alphabetically sorted
|
||||
@@ -75,6 +107,10 @@ function rotateDir(dir, clockwise) {
|
||||
// Random empty cell, avoiding the snake.
|
||||
function spawnFood(snake, N) {
|
||||
const occupied = new Set(snake.map(s => s.x + ',' + s.y))
|
||||
if (typeof N === 'object') {
|
||||
const empty = N.cells.filter(c => !occupied.has(c.x + ',' + c.y))
|
||||
return empty.length ? empty[Math.floor(Math.random() * empty.length)] : null
|
||||
}
|
||||
if (occupied.size >= N * N) return null
|
||||
// Select an empty-cell rank uniformly; at most N*N checks, even almost full.
|
||||
let rank = Math.floor(Math.random() * (N * N - occupied.size))
|
||||
@@ -89,4 +125,4 @@ function spawnFood(snake, N) {
|
||||
return null
|
||||
}
|
||||
|
||||
module.exports = { initialSnake, step, turn, renderPieces, rotateDir, spawnFood }
|
||||
module.exports = { ARENA, initialSnake, step, turn, renderPieces, rotateDir, spawnFood }
|
||||
|
||||
Reference in New Issue
Block a user