fix: limit trainer jumps and exit positions

This commit is contained in:
nyan404 committed 2026-11-22 16:33:30 +03:00
1 parent 27b3e0aaae
commit cc6bcb23c8
12 files changed
+174 -79

No files matched your search

+45 -34
View File
@@ -9,28 +9,17 @@ const options = {height: 1, lu: 5, growth: 12, season: 0};
function fixture(settings = options, seed = 19) {
return {field: generateField(settings, seeded(seed)), game: new Training(settings, seeded(seed))};
}
function openReachableSafe(game, field) {
let progress = true;
while (progress && !['won', 'lost'].includes(game.view().status)) {
progress = false;
for (const cell of game.view().cells) {
if (cell.available && !cell.opened && !cell.transition && !field.hazards.has(cell.id)) {
game.jump(cell.id); progress = true;
}
}
}
}
test('entrance is always x1 y1 and every other cell can host the exit', () => {
test('entrance is always x1 y1 and exits vary only on the allowed edges', () => {
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 allowed = [19,29,39,49,55,56,57,58,59];
for (let i = 0; i < allowed.length; i++) {
const random = () => (i + .5) / allowed.length;
const field = generateField(options, random);
assert.equal(field.start, 0);
assert.equal(field.exit, id);
assert(allowed.includes(field.exit));
exits.add(field.exit);
assert(!field.hazards.has(field.start));
assert(!field.hazards.has(field.exit));
@@ -47,7 +36,7 @@ test('entrance is always x1 y1 and every other cell can host the exit', () => {
assert.equal(game.jump(field.exit).code, 'transition');
assert.equal(game.mark(field.exit).code, 'transition');
}
assert.equal(exits.size, 59);
assert.deepEqual([...exits].sort((a,b)=>a-b), allowed);
});
test('generation covers every settings combination with safe transitions, base and connecting route', () => {
@@ -59,8 +48,10 @@ test('generation covers every settings combination with safe transitions, base a
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(field.exit));
for (const id of reached) for (const next of neighbors(id)) {
if (next !== field.exit && !field.hazards.has(next)) reached.add(next);
}
assert(neighbors(field.exit).some(id => reached.has(id)));
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);
}
@@ -70,16 +61,27 @@ test('edge neighborhoods do not wrap rows', () => {
assert.equal(neighbors(9).length, 3); assert.equal(neighbors(25).length, 8);
assert.deepEqual(neighbors(59).sort((a,b)=>a-b), [48,49,58]);
});
test('only base or neighbors of any opened cell are available; transitions never count', () => {
test('only the current position’s neighbors are available, even for safe or opened cells', () => {
const {game} = fixture();
assert.equal(game.jump(START).code, 'transition');
assert.equal(game.jump(game.view().exit).code, 'transition');
assert.equal(game.jump(25).code, 'unreachable');
assert.equal(game.jump(2).code, 'unreachable'); // safe upper-row cell, two columns away
assert.deepEqual(game.view().cells.filter(c => c.available).map(c => c.id), [1,10,11]);
assert.equal(game.mark(START).code, 'transition');
assert.equal(game.jump(1).code, 'opened');
game.jump(4); // last position differs: adjacency still belongs to any open cell
assert.equal(game.jump(1).code, 'unreachable');
assert(game.view().cells[10].available);
assert.equal(game.view().position, 4);
assert.equal(game.jump(2).code, 'opened');
assert(!game.view().cells[10].available); // still next to opened cell 1
assert.equal(game.jump(10).code, 'unreachable');
assert.equal(game.jump(3).code, 'opened');
assert.equal(game.jump(1).code, 'unreachable'); // previously opened
assert.equal(game.view().position, 3);
assert.equal(game.jump(2).code, 'revisited');
const diagonal = fixture();
assert(!diagonal.field.hazards.has(11));
assert.equal(diagonal.game.jump(11).code, 'opened');
});
test('notes hide no facts, block jumps, clear independently and reject invalid IDs', () => {
const {game} = fixture();
@@ -112,7 +114,17 @@ test('all and only hazards marked wins; terminal actions cannot change state', (
});
test('opening all safe cells wins without entering either transition', () => {
const {game,field} = fixture();
openReachableSafe(game, field);
function explore(position) {
for (const id of neighbors(position)) {
if (game.view().status === 'won') return;
const cell = game.view().cells[id];
if (cell.opened || cell.transition || field.hazards.has(id)) continue;
assert.equal(game.jump(id).code, 'opened');
explore(id);
if (game.view().status !== 'won' && position !== START) assert.equal(game.jump(position).code, 'revisited');
}
}
explore(START);
assert.equal(game.view().status, 'won');
assert.equal(game.view().opened, 58 - field.hazards.size);
});
@@ -130,20 +142,19 @@ test('hazard ends attempt, reveals wrong notes and preserves last safe position'
});
test('LU hint probability, repeated safe visits and hidden information boundary', () => {
assert.deepEqual(Array.from({length:10}, (_,lu)=>hintChance(lu)), [0,0,0,0,.1,.2,.3,.3,.3,.3]);
const {game,field} = fixture();
openReachableSafe(game, field);
// Separate active game: choose an available safe base cell with adjacent hazards.
const active = fixture();
const id = active.field.base.find(n => active.field.values[n] > 0);
assert.notEqual(id, undefined);
active.game.jump(id, () => 0);
const revisit = active.game.jump(id, () => 0);
const active = fixture(), noHints = fixture({...options, lu:0});
const route = [1,2,3,4,5,4,5];
for (const id of route.slice(0,-1)) {
active.game.jump(id, () => 1);
noHints.game.jump(id, () => 0);
}
const revisit = active.game.jump(5, () => 0);
assert.equal(revisit.code, 'revisited');
assert(active.field.hazards.has(revisit.hint));
assert.equal(active.game.view().cells[revisit.hint].value, null);
assert.equal(active.game.view().opened, 1);
assert.equal(active.game.view().history.length, 2);
assert.equal(new Training({...options,lu:0}, seeded()).jump(id, () => 0).hint, null);
assert.equal(active.game.view().opened, 5);
assert.equal(active.game.view().history.length, route.length);
assert.equal(noHints.game.jump(5, () => 0).hint, null);
});
test('invalid settings are rejected rather than corrupting generation', () => {
for (const bad of [{height:0},{lu:10},{growth:99},{season:7}]) assert.throws(()=>new Training({...options,...bad}), RangeError);