@logic-pad/core
Version:
66 lines (65 loc) • 2.13 kB
JavaScript
import { ConfigType } from '../config.js';
import GridData from '../grid.js';
import { Color, State } from '../primitives.js';
import RegionShapeRule from './regionShapeRule.js';
export default class UniqueShapeRule extends RegionShapeRule {
title = 'Unique 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(['bwbww', 'wwbww', 'wbwbb', 'bwwbw']));
static EXAMPLE_GRID_DARK = Object.freeze(GridData.create(['wbwbb', 'bbwbb', 'bwbww', 'wbbwb']));
static SEARCH_VARIANTS = [
new UniqueShapeRule(Color.Light).searchVariant(),
new UniqueShapeRule(Color.Dark).searchVariant(),
];
/**
* **No two <color> areas have the same shape and size**
*
* @param color - The color of the regions to compare.
*/
constructor(color) {
super(color);
}
get id() {
return `unique_shape`;
}
get explanation() {
return `No two ${this.color} areas have the same shape and size`;
}
get configs() {
return UniqueShapeRule.CONFIGS;
}
createExampleGrid() {
return this.color === Color.Light
? UniqueShapeRule.EXAMPLE_GRID_LIGHT
: UniqueShapeRule.EXAMPLE_GRID_DARK;
}
get searchVariants() {
return UniqueShapeRule.SEARCH_VARIANTS;
}
validateGrid(grid) {
const { regions, complete } = this.getShapeRegions(grid);
const errorRegion = regions.find(r => r.count > 1);
if (errorRegion) {
return {
state: State.Error,
positions: errorRegion.positions,
};
}
else {
return { state: complete ? State.Satisfied : State.Incomplete };
}
}
copyWith({ color }) {
return new UniqueShapeRule(color ?? this.color);
}
}
export const instance = new UniqueShapeRule(Color.Dark);