UNPKG

@logic-pad/core

Version:
89 lines (88 loc) 2.68 kB
import { ConfigType } from '../config.js'; import GridData from '../grid.js'; import { minBy } from '../dataHelper.js'; import { Color, State } from '../primitives.js'; import RegionShapeRule from './regionShapeRule.js'; class SameShapeRule extends RegionShapeRule { /** * **All &lt;color&gt; areas have the same shape and size** * * @param color - The color of the regions to compare. */ constructor(color) { super(color); } get id() { return `same_shape`; } get explanation() { return `All ${this.color} areas have the same shape and size`; } get configs() { return SameShapeRule.CONFIGS; } createExampleGrid() { return this.color === Color.Light ? SameShapeRule.EXAMPLE_GRID_LIGHT : SameShapeRule.EXAMPLE_GRID_DARK; } get searchVariants() { return SameShapeRule.SEARCH_VARIANTS; } validateGrid(grid) { const { regions, complete } = this.getShapeRegions(grid); if (regions.length > 1) { return { state: State.Error, positions: minBy(regions, island => island.count).positions, }; } else if (regions.length <= 1) { return { state: complete ? State.Satisfied : State.Incomplete }; } else { return { state: State.Incomplete }; // not reachable but the TS is not happy } } copyWith({ color }) { return new SameShapeRule(color ?? this.color); } } Object.defineProperty(SameShapeRule, "CONFIGS", { enumerable: true, configurable: true, writable: true, value: Object.freeze([ { type: ConfigType.Color, default: Color.Light, allowGray: false, field: 'color', description: 'Color', configurable: true, }, ]) }); Object.defineProperty(SameShapeRule, "EXAMPLE_GRID_LIGHT", { enumerable: true, configurable: true, writable: true, value: Object.freeze(GridData.create(['wwbww', 'wbwbw', 'wbwbw', 'bwwbb'])) }); Object.defineProperty(SameShapeRule, "EXAMPLE_GRID_DARK", { enumerable: true, configurable: true, writable: true, value: Object.freeze(GridData.create(['bbwbb', 'bwbwb', 'bwbwb', 'wbbww'])) }); Object.defineProperty(SameShapeRule, "SEARCH_VARIANTS", { enumerable: true, configurable: true, writable: true, value: [ new SameShapeRule(Color.Light).searchVariant(), new SameShapeRule(Color.Dark).searchVariant(), ] }); export default SameShapeRule; export const instance = new SameShapeRule(Color.Dark);