// Original implementation of the training model described in LU_TRAINER_PROJECT.md. // 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 = 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 = []; for (let dr = -1; dr <= 1; dr++) for (let dc = -1; dc <= 1; dc++) { if (!dr && !dc) continue; const r = row + dr, c = col + dc; if (r >= 0 && r < ROWS && c >= 0 && c < COLS) result.push(r * COLS + c); } return result; } export function hintChance(lu) { return lu < 4 ? 0 : lu === 4 ? .1 : lu === 5 ? .2 : .3; } 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 || !Number.isInteger(value.lu) || value.lu < 0 || value.lu > 9 || ![12,15,18,22,25].includes(value.growth) || ![-3,0,5].includes(value.season)) { throw new RangeError('Недопустимые параметры тренировки'); } return Object.freeze(value); } 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 = 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) { const steps = neighbors(from).filter(id => distance(id, to) < distance(from, to)); from = steps[Math.floor(random() * steps.length)]; reserved.add(from); } } connect(START, base[0]); 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)); [candidates[i], candidates[j]] = [candidates[j], candidates[i]]; } const count = Math.min(Math.max(5, options.growth + options.season + 2 * (options.height - 1)), candidates.length); // The safe entrance and x2 y1 already cap x1 y1 at 2 and x1 y2 at 3. // Cap x1 y3 at 4 during placement, skipping only a fifth hazard in its neighborhood. const leftThirdNeighbors = neighbors(2 * COLS); const hazards = new Set(); let leftThirdHazards = 0; for (const id of candidates) { if (hazards.size === count) break; if (leftThirdNeighbors.includes(id)) { if (leftThirdHazards === 4) continue; leftThirdHazards++; } hazards.add(id); } const values = Array.from({length: ROWS * COLS}, (_,id)=>neighbors(id).filter(n => hazards.has(n)).length); return { start: START, exit, base, reserved, hazards, values }; } export class Training { #field; #opened = new Set(); #marks = new Set(); #ratings = new Map(); #position = START; #status = 'ready'; #hit = null; #history = []; #options; constructor(input = DEFAULTS, random = Math.random) { this.#options = settings(input); this.#field = generateField(this.#options, random); } #valid(id) { return Number.isInteger(id) && id >= 0 && id < ROWS * COLS; } #finished() { return this.#status === 'won' || this.#status === 'lost'; } #transition(id) { return id === this.#field.exit; } #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'}; if (this.#transition(id)) return {code: 'transition'}; return null; } #win() { if (this.#opened.size === ROWS * COLS - 2 - this.#field.hazards.size || (this.#marks.size === this.#field.hazards.size && [...this.#marks].every(id => this.#field.hazards.has(id)))) this.#status = 'won'; } jump(id, random = Math.random) { const rejected = this.#guard(id); if (rejected) return rejected; if (this.#marks.has(id)) return {code: 'marked'}; if (!this.#available(id)) return {code: 'unreachable'}; this.#status = 'playing'; if (this.#field.hazards.has(id)) { this.#hit = id; this.#status = 'lost'; this.#history.push({type: 'fall', id}); return {code: 'fall', id, hint: null}; } const code = id === START || this.#opened.has(id) ? 'revisited' : 'opened'; if (id !== START) this.#opened.add(id); this.#position = id; const value = this.#field.values[id]; this.#history.push({type: code, id, value}); this.#win(); const candidates = neighbors(id).filter(n => this.#field.hazards.has(n) && !this.#marks.has(n)); const hint = !this.#finished() && candidates.length && random() < hintChance(this.#options.lu) ? candidates[Math.floor(random() * candidates.length)] : null; return {code, id, value, hint}; } mark(id) { const rejected = this.#guard(id); if (rejected) return rejected; if (id === START || this.#opened.has(id)) return {code: 'opened'}; const marked = !this.#marks.has(id); if (marked) this.#marks.add(id); else this.#marks.delete(id); this.#history.push({type: marked ? 'marked' : 'unmarked', id}); this.#win(); return {code: marked ? 'marked' : 'unmarked', id}; } rate(id, rating) { const rejected = this.#guard(id); if (rejected) return rejected; if (id === START) return {code: 'opened'}; if (rating !== null && (!Number.isInteger(rating) || rating < 0 || rating > 7)) return {code: 'invalid-level'}; if ((this.#ratings.get(id) ?? null) === rating) return {code: 'unchanged'}; if (rating === null) this.#ratings.delete(id); else this.#ratings.set(id, rating); const code = rating === null ? 'unrated' : 'rated'; this.#history.push({type: code, id, rating}); return {code, id, rating}; } clearMarks() { if (this.#finished()) return {code: 'finished'}; if (!this.#marks.size && !this.#ratings.size) return {code: 'empty'}; this.#marks.clear(); this.#ratings.clear(); this.#history.push({type: 'cleared'}); return {code: 'cleared'}; } view() { const ended = this.#finished(); return { 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, ratings: this.#ratings.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, 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), rating: this.#ratings.get(id) ?? null, available: !ended && this.#available(id), value: id === START || this.#opened.has(id) || ended ? this.#field.values[id] : null, ...(ended ? {hazard: this.#field.hazards.has(id), wrong: this.#marks.has(id) && !this.#field.hazards.has(id)} : {}), })), }; } }