@logic-pad/core
Version:
96 lines (95 loc) • 3.62 kB
JavaScript
import { ConfigType } from '../config.js';
import GridData from '../grid.js';
import GridZones from '../gridZones.js';
import { Color, State } from '../primitives.js';
import CellCountPerZoneRule from './cellCountPerZoneRule.js';
export default class SameCountPerZoneRule extends CellCountPerZoneRule {
color;
title = 'Equal Count Per Zone';
static CONFIGS = Object.freeze([
{
type: ConfigType.Color,
default: Color.Light,
allowGray: true,
field: 'color',
description: 'Color',
configurable: true,
},
]);
static EXAMPLE_GRID_LIGHT = Object.freeze(GridData.create(['bwbbb', 'wbbwb', 'bbbwb', 'bwbwb'])
.withZones(new GridZones([
{ x1: 0, y1: 1, x2: 0, y2: 2 },
{ x1: 1, y1: 1, x2: 1, y2: 2 },
{ x1: 2, y1: 1, x2: 2, y2: 2 },
{ x1: 3, y1: 1, x2: 3, y2: 2 },
{ x1: 4, y1: 1, x2: 4, y2: 2 },
{ x1: 1, y1: 0, x2: 2, y2: 0 },
{ x1: 1, y1: 1, x2: 2, y2: 1 },
{ x1: 2, y1: 2, x2: 3, y2: 2 },
{ x1: 2, y1: 3, x2: 3, y2: 3 },
]))
.addRule(new SameCountPerZoneRule(Color.Light)));
static EXAMPLE_GRID_DARK = Object.freeze(SameCountPerZoneRule.EXAMPLE_GRID_LIGHT.withTiles(tiles => tiles.map(row => row.map(tile => tile.withColor(tile.color === Color.Dark ? Color.Light : Color.Dark)))));
static EXAMPLE_GRID_GRAY = Object.freeze(SameCountPerZoneRule.EXAMPLE_GRID_LIGHT.withTiles(tiles => tiles.map(row => row.map(tile => tile.withColor(tile.color === Color.Light ? Color.Gray : tile.color)))));
static SEARCH_VARIANTS = [
new SameCountPerZoneRule(Color.Light).searchVariant(),
new SameCountPerZoneRule(Color.Dark).searchVariant(),
];
/**
* **Zones of the same size have the same number of <color> cells.**
*
* @param color - The color of the cells to count.
*/
constructor(color) {
super(color);
this.color = color;
}
get id() {
return `zone_cell_count`;
}
get explanation() {
return `Zones of the same size have the same number of ${this.color} cells`;
}
get configs() {
return SameCountPerZoneRule.CONFIGS;
}
createExampleGrid() {
if (this.color === Color.Light) {
return SameCountPerZoneRule.EXAMPLE_GRID_LIGHT;
}
else if (this.color === Color.Dark) {
return SameCountPerZoneRule.EXAMPLE_GRID_DARK;
}
else {
return SameCountPerZoneRule.EXAMPLE_GRID_GRAY;
}
}
get searchVariants() {
return SameCountPerZoneRule.SEARCH_VARIANTS;
}
validateGrid(grid) {
const { zones, complete } = this.getZoneCounts(grid);
if (zones.length <= 1) {
return { state: complete ? State.Satisfied : State.Incomplete };
}
else {
const errorZone = zones.find(z => zones.some(zz => zz !== z &&
zz.positions.length === z.positions.length &&
(zz.completed > z.completed + z.possible ||
zz.completed + zz.possible < z.completed)));
if (errorZone) {
return {
state: State.Error,
positions: errorZone.positions,
};
}
else {
return { state: complete ? State.Satisfied : State.Incomplete };
}
}
}
copyWith({ color }) {
return new SameCountPerZoneRule(color ?? this.color);
}
}
export const instance = new SameCountPerZoneRule(Color.Light);