@logic-pad/core
Version:
86 lines (85 loc) • 2.56 kB
JavaScript
import { ConfigType } from '../config.js';
import GridData from '../grid.js';
import { Color, State } from '../primitives.js';
import RegionShapeRule from './regionShapeRule.js';
class UniqueShapeRule extends RegionShapeRule {
/**
* **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);
}
}
Object.defineProperty(UniqueShapeRule, "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(UniqueShapeRule, "EXAMPLE_GRID_LIGHT", {
enumerable: true,
configurable: true,
writable: true,
value: Object.freeze(GridData.create(['bwbww', 'wwbww', 'wbwbb', 'bwwbw']))
});
Object.defineProperty(UniqueShapeRule, "EXAMPLE_GRID_DARK", {
enumerable: true,
configurable: true,
writable: true,
value: Object.freeze(GridData.create(['wbwbb', 'bbwbb', 'bwbww', 'wbbwb']))
});
Object.defineProperty(UniqueShapeRule, "SEARCH_VARIANTS", {
enumerable: true,
configurable: true,
writable: true,
value: [
new UniqueShapeRule(Color.Light).searchVariant(),
new UniqueShapeRule(Color.Dark).searchVariant(),
]
});
export default UniqueShapeRule;
export const instance = new UniqueShapeRule(Color.Dark);