@logic-pad/core
Version:
69 lines (68 loc) • 2.26 kB
JavaScript
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';
export default class SameShapeRule extends RegionShapeRule {
title = 'Same Shape Areas';
static CONFIGS = Object.freeze([
{
type: ConfigType.Color,
default: Color.Light,
allowGray: false,
field: 'color',
description: 'Color',
configurable: true,
},
]);
static EXAMPLE_GRID_LIGHT = Object.freeze(GridData.create(['wwbww', 'wbwbw', 'wbwbw', 'bwwbb']));
static EXAMPLE_GRID_DARK = Object.freeze(GridData.create(['bbwbb', 'bwbwb', 'bwbwb', 'wbbww']));
static SEARCH_VARIANTS = [
new SameShapeRule(Color.Light).searchVariant(),
new SameShapeRule(Color.Dark).searchVariant(),
];
/**
* **All <color> 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);
}
}
export const instance = new SameShapeRule(Color.Dark);