@logic-pad/core
Version:
106 lines (105 loc) • 3.63 kB
JavaScript
import { ConfigType } from '../config.js';
import GridData from '../grid.js';
import { array, move } from '../dataHelper.js';
import { Color, DIRECTIONS } from '../primitives.js';
import NumberSymbol from './numberSymbol.js';
class ViewpointSymbol extends NumberSymbol {
/**
* **Viewpoint Numbers count visible cells in the four directions**
* @param x - The x-coordinate of the symbol.
* @param y - The y-coordinate of the symbol.
* @param number - The viewpoint number.
*/
constructor(x, y, number) {
super(x, y, number);
}
get id() {
return `viewpoint`;
}
get placementStep() {
return 1;
}
get explanation() {
return '*Viewpoint Numbers* count visible cells in the four directions';
}
get configs() {
return ViewpointSymbol.CONFIGS;
}
createExampleGrid() {
return ViewpointSymbol.EXAMPLE_GRID;
}
countForColor(grid, color, pos) {
let minSize = 1;
let maxSize = 1;
const visitedColored = array(grid.width, grid.height, (x, y) => x === pos.x && y === pos.y);
for (const direction of DIRECTIONS) {
grid.iterateDirection(move(pos, direction), direction, tile => tile.color === color, () => {
minSize++;
}, visitedColored);
}
const visitedAll = array(grid.width, grid.height, (x, y) => x === pos.x && y === pos.y);
for (const direction of DIRECTIONS) {
grid.iterateDirection(move(pos, direction), direction, tile => tile.color === color || tile.color === Color.Gray, () => {
maxSize++;
}, visitedAll);
}
return { completed: minSize, possible: maxSize };
}
countTiles(grid) {
if (Math.floor(this.x) !== this.x || Math.floor(this.y) !== this.y)
return { completed: 0, possible: Number.MAX_SAFE_INTEGER };
const pos = { x: this.x, y: this.y };
const color = grid.getTile(this.x, this.y).color;
if (color === Color.Gray) {
const dark = this.countForColor(grid, Color.Dark, pos);
const light = this.countForColor(grid, Color.Light, pos);
return {
completed: Math.min(dark.completed, light.completed),
possible: Math.max(dark.possible, light.possible),
};
}
return this.countForColor(grid, color, pos);
}
copyWith({ x, y, number, }) {
return new ViewpointSymbol(x ?? this.x, y ?? this.y, number ?? this.number);
}
withNumber(number) {
return this.copyWith({ number });
}
}
Object.defineProperty(ViewpointSymbol, "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(ViewpointSymbol, "EXAMPLE_GRID", {
enumerable: true,
configurable: true,
writable: true,
value: Object.freeze(GridData.create(['bbbbb', 'wwwwb', 'bwwbb', 'bbwww']).addSymbol(new ViewpointSymbol(1, 1, 5)))
});
export default ViewpointSymbol;
export const instance = new ViewpointSymbol(0, 0, 1);