Files
vela-snake/tests/round-history.cjs

31 lines
1.6 KiB
JavaScript

const assert=require('node:assert/strict')
const c=require('../src/common/snake.js'),a=c.ARENA
let seed=408;const random=()=>{seed=(Math.imul(seed,1664525)+1013904223)>>>0;return seed/4294967296};Math.random=random
const dirs=[{x:1,y:0},{x:0,y:1},{x:-1,y:0},{x:0,y:-1}],names=['e','s','w','n'],opp={e:'w',w:'e',n:'s',s:'n'}
const side=d=>names[dirs.findIndex(v=>v.x===d.x&&v.y===d.y)]
let moves=0,seams=0
for(const mode of ['classic','wrap'])for(let game=0;game<1000;game++){
let model=c.initialSnake(),arrivals=model.map(()=>dirs[0]),dir=dirs[0],food=c.spawnFood(model,a)
for(let tick=0;tick<250;tick++){
dir=c.turn(dir,dirs[Math.floor(random()*4)])
const result=c.step(model,dir,food,a,mode)
if(!result.alive)break
if(Math.abs(result.snake[0].x-model[0].x)+Math.abs(result.snake[0].y-model[0].y)>1)seams++
model=result.snake;arrivals.unshift(dir);if(!result.ate)arrivals.pop()
assert.equal(new Set(model.map(p=>p.x+','+p.y)).size,model.length)
for(const p of model)assert.ok(a.rows[p.y].includes(p.x))
const expected=model.map((p,i)=>{
if(i===0)return 'head_'+side(dir)
const from=side(arrivals[i-1]);if(i===model.length-1)return 'tail_'+opp[from]
const to=opp[side(arrivals[i])]
if((from==='e'||from==='w')&&(to==='e'||to==='w'))return 'body_h'
if((from==='n'||from==='s')&&(to==='n'||to==='s'))return 'body_v'
return 'corner_'+[from,to].sort().join('')
})
assert.deepEqual(c.renderPieces(model,dir,mode==='wrap'?a:0),expected)
moves++
if(result.ate){food=c.spawnFood(model,a);if(!food)break}
}
}
console.log(JSON.stringify({roundMovesChecked:moves,wrapSeamsChecked:seams,historyBasedTextureClassification:'passed'}))