135 lines
6.0 KiB
JavaScript
135 lines
6.0 KiB
JavaScript
// 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 = 50;
|
|
export const EXIT = 39;
|
|
export const DEFAULTS = Object.freeze({ height: 1, lu: 5, growth: 12, season: 0 });
|
|
|
|
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 `${'АБВГДЕ'[Math.floor(id / COLS)]}${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 base = Array.from({length: options.height === 1 ? 5 : 3}, (_,i)=>i);
|
|
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);
|
|
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 };
|
|
}
|
|
|
|
export class Training {
|
|
#field;
|
|
#opened = new Set();
|
|
#marks = new Set();
|
|
#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 === START || id === 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'};
|
|
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 = this.#opened.has(id) ? 'revisited' : 'opened';
|
|
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 (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};
|
|
}
|
|
clearMarks() {
|
|
if (this.#finished()) return {code: 'finished'};
|
|
if (!this.#marks.size) return {code: 'empty'};
|
|
this.#marks.clear(); this.#history.push({type: 'cleared'});
|
|
return {code: 'cleared'};
|
|
}
|
|
view() {
|
|
const ended = this.#finished();
|
|
return {
|
|
options: {...this.#options}, 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),
|
|
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,
|
|
...(ended ? {hazard: this.#field.hazards.has(id), wrong: this.#marks.has(id) && !this.#field.hazards.has(id)} : {}),
|
|
})),
|
|
};
|
|
}
|
|
}
|