// TDD: piece classification for textured rendering. // renderPieces(snake, dir) -> array of texture names, one per segment (head first). const assert = require('assert/strict') const fs = require('fs') const vm = require('vm') const src = fs.readFileSync(__dirname + '/../src/common/snake.js', 'utf8') const sandbox = { module: { exports: {} } } vm.runInNewContext(src, sandbox) const snake = sandbox.module.exports // compass sides: e/w/n/s (screen coords, y grows downward) function pieces(list, dir) { return snake.renderPieces(list, dir) } // -- initial snake: head east, horizontal body, tail pointing west assert.deepEqual( pieces([{ x: 6, y: 7 }, { x: 5, y: 7 }, { x: 4, y: 7 }], { x: 1, y: 0 }), ['head_e', 'body_h', 'tail_w']) // -- vertical snake moving south assert.deepEqual( pieces([{ x: 3, y: 5 }, { x: 3, y: 4 }, { x: 3, y: 4 + 1 }], { x: 0, y: 1 }) .map((p, i) => i), // shape check below with explicit list instead [0, 1, 2]) assert.deepEqual( pieces([{ x: 3, y: 5 }, { x: 3, y: 6 }, { x: 3, y: 7 }], { x: 0, y: 1 }), ['head_s', 'body_v', 'tail_s']) // -- corner: head turned from east to south // snake [(6,8),(6,7),(5,7)]: neck connects south (to head) and west (to rest); // tail cell (5,7)'s neighbor is east, so its tip points west assert.deepEqual( pieces([{ x: 6, y: 8 }, { x: 6, y: 7 }, { x: 5, y: 7 }], { x: 0, y: 1 }), ['head_s', 'corner_sw', 'tail_w']) // -- corner: head turned from east to north assert.deepEqual( pieces([{ x: 6, y: 6 }, { x: 6, y: 7 }, { x: 5, y: 7 }], { x: 0, y: -1 }), ['head_n', 'corner_nw', 'tail_w']) // -- corner heading west turning south (es corner mirrored) // tail cell (5,7)'s neighbor is west, so its tip points east assert.deepEqual( pieces([{ x: 4, y: 8 }, { x: 4, y: 7 }, { x: 5, y: 7 }], { x: -1, y: 0 }), ['head_w', 'corner_es', 'tail_e']) // -- double corner (S shape): head moving south, west, then north — // hand-verified: every middle cell of this tight zigzag is a corner // [(6,8) head][ (6,7) sw ][ (5,7) en ][ (5,6) sw ][ (4,6) en ][ (4,5) tail ] assert.deepEqual( pieces([{ x: 6, y: 8 }, { x: 6, y: 7 }, { x: 5, y: 7 }, { x: 5, y: 6 }, { x: 4, y: 6 }, { x: 4, y: 5 }], { x: 0, y: 1 }), ['head_s', 'corner_sw', 'corner_en', 'corner_sw', 'corner_en', 'tail_n']) // -- every returned piece maps to a texture that must exist const TEX = ['head_e', 'head_w', 'head_n', 'head_s', 'body_h', 'body_v', 'corner_en', 'corner_es', 'corner_nw', 'corner_sw', 'tail_e', 'tail_w', 'tail_n', 'tail_s'] const dirTex = __dirname + '/../src/common/tex' for (const p of TEX) { assert.ok(fs.existsSync(dirTex + '/' + p + '.png'), 'missing texture ' + p) } // -- food + board textures assert.ok(fs.existsSync(dirTex + '/apple.png'), 'missing apple texture') assert.ok(fs.existsSync(dirTex + '/board.png'), 'missing board texture') console.log('PIECE CLASSIFICATION PASS')