@logic-pad/core
Version:
111 lines (110 loc) • 3.26 kB
JavaScript
import { ConfigType } from '../config.js';
import GridData from '../grid.js';
import { Color } from '../primitives.js';
import NumberSymbol from './numberSymbol.js';
const OFFSETS = [
[0, -1],
[1, 0],
[0, 1],
[-1, 0],
];
class FocusSymbol extends NumberSymbol {
/**
* **Focus Numbers count directly adjacent cells of the same color**
* @param x - The x-coordinate of the symbol.
* @param y - The y-coordinate of the symbol.
* @param number - The focus number.
*/
constructor(x, y, number) {
super(x, y, number);
}
get id() {
return `focus`;
}
get placementStep() {
return 1;
}
get explanation() {
return '*Focus Numbers* count directly adjacent cells of the same color';
}
get configs() {
return FocusSymbol.CONFIGS;
}
createExampleGrid() {
return FocusSymbol.EXAMPLE_GRID;
}
countTiles(grid) {
if (Math.floor(this.x) !== this.x || Math.floor(this.y) !== this.y)
return { completed: 0, possible: Number.MAX_SAFE_INTEGER };
const color = grid.getTile(this.x, this.y).color;
if (color === Color.Gray)
return { completed: 0, possible: Number.MAX_SAFE_INTEGER };
let gray = 0;
let same = 0;
const visited = [];
for (const [dx, dy] of OFFSETS) {
const x = this.x + dx;
const y = this.y + dy;
if (grid.wrapAround.value) {
const pos = grid.toArrayCoordinates(x, y);
if (visited.some(v => v.x === pos.x && v.y === pos.y))
continue;
visited.push(pos);
}
const tile = grid.getTile(x, y);
if (!tile.exists)
continue;
if (tile.color === Color.Gray)
gray++;
else if (tile.color === color)
same++;
}
return { completed: same, possible: same + gray };
}
copyWith({ x, y, number, }) {
return new FocusSymbol(x ?? this.x, y ?? this.y, number ?? this.number);
}
withNumber(number) {
return this.copyWith({ number });
}
}
Object.defineProperty(FocusSymbol, "CONFIGS", {
enumerable: true,
configurable: true,
writable: true,
value: Object.freeze([
{
type: ConfigType.Number,
default: 0,
field: 'x',
description: 'X',
configurable: false,
},
{
type: ConfigType.Number,
default: 0,
field: 'y',
description: 'Y',
configurable: false,
},
{
type: ConfigType.Number,
default: 1,
field: 'number',
description: 'Number',
configurable: true,
},
])
});
Object.defineProperty(FocusSymbol, "EXAMPLE_GRID", {
enumerable: true,
configurable: true,
writable: true,
value: Object.freeze(GridData.create(['wwwww', 'bbbbw', 'wwbbw', 'wwwww']).withSymbols([
new FocusSymbol(0, 0, 1),
new FocusSymbol(4, 1, 2),
new FocusSymbol(1, 3, 3),
]))
});
export default FocusSymbol;
export const instance = new FocusSymbol(0, 0, 1);