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

+12 -10
View File
@@ -2,8 +2,7 @@
// No DOM, storage, clock or network: the UI consumes only view() and action results.
export const ROWS = 6;
export const COLS = 10;
export const START = 50;
export const EXIT = 39;
export const START = 0;
export const DEFAULTS = Object.freeze({ height: 1, lu: 5, growth: 12, season: 0 });
export function neighbors(id) {
@@ -16,7 +15,7 @@ export function neighbors(id) {
return result;
}
export function hintChance(lu) { return lu < 4 ? 0 : lu === 4 ? .1 : lu === 5 ? .2 : .3; }
export function coordinate(id) { return `${'АБВГДЕ'[Math.floor(id / COLS)]}${id % COLS + 1}`; }
export function coordinate(id) { return `x${id % COLS + 1} y${Math.floor(id / COLS) + 1}`; }
function settings(input) {
const value = { ...DEFAULTS, ...input };
if (!Number.isInteger(value.height) || value.height < 1 || value.height > 5 ||
@@ -29,8 +28,11 @@ function settings(input) {
function distance(a, b) { return Math.max(Math.abs(Math.floor(a / COLS) - Math.floor(b / COLS)), Math.abs(a % COLS - b % COLS)); }
export function generateField(input = DEFAULTS, random = Math.random) {
const options = settings(input);
const base = Array.from({length: options.height === 1 ? 5 : 3}, (_,i)=>i);
const reserved = new Set([START, EXIT, ...base]);
const exit = 1 + Math.floor(random() * (ROWS * COLS - 1));
// Transitions do not consume one of the 5/3 initially playable safe cells.
const base = Array.from({length: COLS - 1}, (_,i)=>i + 1)
.filter(id => id !== exit).slice(0, options.height === 1 ? 5 : 3);
const reserved = new Set([START, exit, ...base]);
function connect(from, to) {
while (from !== to) {
const steps = neighbors(from).filter(id => distance(id, to) < distance(from, to));
@@ -39,7 +41,7 @@ export function generateField(input = DEFAULTS, random = Math.random) {
}
}
connect(START, base[0]);
connect(base[Math.floor(random() * base.length)], EXIT);
connect(base[Math.floor(random() * base.length)], exit);
const candidates = Array.from({length: ROWS * COLS}, (_,id)=>id).filter(id => !reserved.has(id));
for (let i = candidates.length - 1; i > 0; i--) {
const j = Math.floor(random() * (i + 1));
@@ -48,7 +50,7 @@ export function generateField(input = DEFAULTS, random = Math.random) {
const count = Math.min(Math.max(5, options.growth + options.season + 2 * (options.height - 1)), candidates.length);
const hazards = new Set(candidates.slice(0, count));
const values = Array.from({length: ROWS * COLS}, (_,id)=>neighbors(id).filter(n => hazards.has(n)).length);
return { base, reserved, hazards, values };
return { start: START, exit, base, reserved, hazards, values };
}
export class Training {
@@ -66,7 +68,7 @@ export class Training {
}
#valid(id) { return Number.isInteger(id) && id >= 0 && id < ROWS * COLS; }
#finished() { return this.#status === 'won' || this.#status === 'lost'; }
#transition(id) { return id === START || id === EXIT; }
#transition(id) { return id === START || id === this.#field.exit; }
#available(id) { return this.#field.base.includes(id) || neighbors(id).some(n => this.#opened.has(n)); }
#guard(id) {
if (this.#finished()) return {code: 'finished'};
@@ -118,12 +120,12 @@ export class Training {
view() {
const ended = this.#finished();
return {
options: {...this.#options}, status: this.#status, position: this.#position, hit: this.#hit,
options: {...this.#options}, start: START, exit: this.#field.exit, status: this.#status, position: this.#position, hit: this.#hit,
hazards: this.#field.hazards.size, marks: this.#marks.size, opened: this.#opened.size,
safe: ROWS * COLS - 2 - this.#field.hazards.size,
history: this.#history.map(item => ({...item})),
cells: Array.from({length: ROWS * COLS}, (_,id) => ({
id, transition: this.#transition(id), base: this.#field.base.includes(id),
id, entrance: id === START, exit: id === this.#field.exit, transition: this.#transition(id), base: this.#field.base.includes(id),
opened: this.#opened.has(id), marked: this.#marks.has(id),
available: !ended && this.#available(id),
value: this.#opened.has(id) || ended ? this.#field.values[id] : null,