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

+9 -5
View File
@@ -4,6 +4,11 @@ export const ROWS = 6;
export const COLS = 10;
export const START = 0;
export const DEFAULTS = Object.freeze({ height: 1, lu: 5, growth: 12, season: 0 });
const EXIT_CELLS = Array.from({length: ROWS * COLS}, (_, id) => id)
.filter(id => {
const x = id % COLS + 1, y = Math.floor(id / COLS) + 1;
return (x === COLS && y >= 2) || (y === ROWS && x >= 6);
});
export function neighbors(id) {
const row = Math.floor(id / COLS), col = id % COLS, result = [];
@@ -28,10 +33,9 @@ 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 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 exit = EXIT_CELLS[Math.floor(random() * EXIT_CELLS.length)];
// The safe strip starts next to the entrance; exits never occupy the top row.
const base = Array.from({length: options.height === 1 ? 5 : 3}, (_, i) => i + 1);
const reserved = new Set([START, exit, ...base]);
function connect(from, to) {
while (from !== to) {
@@ -69,7 +73,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 === this.#field.exit; }
#available(id) { return this.#field.base.includes(id) || neighbors(id).some(n => this.#opened.has(n)); }
#available(id) { return !this.#transition(id) && distance(this.#position, id) === 1; }
#guard(id) {
if (this.#finished()) return {code: 'finished'};
if (!this.#valid(id)) return {code: 'invalid'};