@logic-pad/core
Version:
49 lines (48 loc) • 1.95 kB
JavaScript
import BTModule, { BTTile, IntArray2D, createOneTileResult, } from '../data.js';
export default class FocusBTModule extends BTModule {
instr;
cachedCheckResult;
constructor(instr) {
super();
this.instr = instr;
}
checkGlobal(grid) {
const tile = grid.getTile(this.instr.x, this.instr.y);
if (tile === BTTile.Empty)
return createOneTileResult(grid, { x: this.instr.x, y: this.instr.y });
let gray = 0;
let same = 0;
for (let y = this.instr.y - 1; y <= this.instr.y + 1; y++) {
for (let x = this.instr.x - 1; x <= this.instr.x + 1; x++) {
if (y !== this.instr.y && x !== this.instr.x)
continue;
if (!grid.isInBound(x, y) || (x === this.instr.x && y === this.instr.y))
continue;
const checkTile = grid.getTile(x, y);
if (checkTile === BTTile.Empty)
gray++;
else if (checkTile === tile)
same++;
}
}
if (same > this.instr.number || same + gray < this.instr.number)
return false;
this.cachedCheckResult ??= this.buildCheckAndRating(grid);
return this.cachedCheckResult;
}
buildCheckAndRating(grid) {
const tilesNeedCheck = IntArray2D.create(grid.width, grid.height);
const ratings = [];
for (let y = this.instr.y - 1; y <= this.instr.y + 1; y++) {
for (let x = this.instr.x - 1; x <= this.instr.x + 1; x++) {
if (y !== this.instr.y && x !== this.instr.x)
continue;
if (!grid.isInBound(x, y) || (x === this.instr.x && y === this.instr.y))
continue;
tilesNeedCheck.set(x, y, 1);
ratings.push({ pos: { x, y }, score: 1 });
}
}
return { tilesNeedCheck, ratings };
}
}