fix: anchor trainer entrance and generate variable exits

This commit is contained in:
nyan404 committed 2026-11-17 16:44:04 +03:00
1 parent 30d012de80
commit 87a9a5934a
12 files changed
+117 -53

No files matched your search

+43 -14
View File
@@ -1,6 +1,6 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { Training, generateField, neighbors, hintChance, START, EXIT } from '../../web/lu-trainer/engine.js';
import { Training, generateField, neighbors, hintChance, START, coordinate } from '../../web/lu-trainer/engine.js';
function seeded(seed = 19) {
return () => { seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; return seed / 2 ** 32; };
@@ -21,17 +21,46 @@ function openReachableSafe(game, field) {
}
}
test('entrance is always x1 y1 and every other cell can host the exit', () => {
assert.equal(START, 0);
assert.equal(coordinate(START), 'x1 y1');
assert.equal(coordinate(59), 'x10 y6');
const exits = new Set();
for (let id = 1; id < 60; id++) {
const random = () => (id - .5) / 59;
const field = generateField(options, random);
assert.equal(field.start, 0);
assert.equal(field.exit, id);
exits.add(field.exit);
assert(!field.hazards.has(field.start));
assert(!field.hazards.has(field.exit));
assert(!field.base.includes(field.start));
assert(!field.base.includes(field.exit));
const game = new Training(options, random);
const view = game.view();
assert.equal(view.position, 0);
assert.equal(view.start, field.start);
assert.equal(view.exit, field.exit);
assert.equal(view.cells.filter(cell => cell.entrance).length, 1);
assert.equal(view.cells.filter(cell => cell.exit).length, 1);
assert.equal(view.cells.filter(cell => cell.transition).length, 2);
assert.equal(game.jump(field.exit).code, 'transition');
assert.equal(game.mark(field.exit).code, 'transition');
}
assert.equal(exits.size, 59);
});
test('generation covers every settings combination with safe transitions, base and connecting route', () => {
for (let height = 1; height <= 5; height++) for (const growth of [12,15,18,22,25]) for (const season of [-3,0,5]) {
const field = generateField({...options, height, growth, season}, seeded(height * growth + season));
assert.equal(field.values.length, 60);
assert(!field.hazards.has(START) && !field.hazards.has(EXIT));
assert(!field.hazards.has(START) && !field.hazards.has(field.exit));
for (const id of field.base) assert(!field.hazards.has(id));
assert.equal(field.base.length, height === 1 ? 5 : 3);
assert.equal(field.hazards.size, Math.min(Math.max(5, growth + season + 2 * (height - 1)), 60 - field.reserved.size));
const reached = new Set([START]);
for (const id of reached) for (const next of neighbors(id)) if (!field.hazards.has(next)) reached.add(next);
assert(reached.has(EXIT));
assert(reached.has(field.exit));
for (const id of field.base) assert(reached.has(id));
for (let id = 0; id < 60; id++) assert.equal(field.values[id], neighbors(id).filter(n => field.hazards.has(n)).length);
}
@@ -44,24 +73,24 @@ test('edge neighborhoods do not wrap rows', () => {
test('only base or neighbors of any opened cell are available; transitions never count', () => {
const {game} = fixture();
assert.equal(game.jump(START).code, 'transition');
assert.equal(game.jump(EXIT).code, 'transition');
assert.equal(game.jump(game.view().exit).code, 'transition');
assert.equal(game.jump(25).code, 'unreachable');
assert.equal(game.mark(START).code, 'transition');
assert.equal(game.jump(0).code, 'opened');
assert.equal(game.jump(1).code, 'opened');
game.jump(4); // last position differs: adjacency still belongs to any open cell
assert(game.view().cells[10].available);
assert.equal(game.view().position, 4);
});
test('notes hide no facts, block jumps, clear independently and reject invalid IDs', () => {
const {game} = fixture();
game.jump(0); game.mark(1);
game.jump(1); game.mark(2);
const before = game.view();
assert.equal(game.jump(1).code, 'marked');
assert.equal(before.cells[1].value, null);
assert(!Object.hasOwn(before.cells[1], 'hazard'));
assert.equal(game.mark(0).code, 'opened');
assert.equal(game.jump(2).code, 'marked');
assert.equal(before.cells[2].value, null);
assert(!Object.hasOwn(before.cells[2], 'hazard'));
assert.equal(game.mark(1).code, 'opened');
game.clearMarks();
assert(game.view().cells[0].opened);
assert(game.view().cells[1].opened);
assert.equal(game.view().marks, 0);
assert.equal(game.view().position, before.position);
assert(game.view().history.length > before.history.length);
@@ -69,10 +98,10 @@ test('notes hide no facts, block jumps, clear independently and reject invalid I
});
test('all and only hazards marked wins; terminal actions cannot change state', () => {
const {game,field} = fixture();
game.mark(0);
game.mark(1);
for (const id of field.hazards) game.mark(id);
assert.notEqual(game.view().status, 'won');
game.mark(0);
game.mark(1);
assert.equal(game.view().status, 'won');
const ended = game.view();
for (const cell of ended.cells) assert.equal(typeof cell.hazard, 'boolean');
@@ -89,7 +118,7 @@ test('opening all safe cells wins without entering either transition', () => {
});
test('hazard ends attempt, reveals wrong notes and preserves last safe position', () => {
const {game,field} = fixture();
game.mark(4); game.jump(0);
game.mark(4); game.jump(1);
while (game.view().status !== 'lost') {
const cell = game.view().cells.find(c => c.available && !c.opened && !c.transition && !c.marked);
assert(cell); game.jump(cell.id);