@logic-pad/core
Version:
60 lines (59 loc) • 2.32 kB
JavaScript
import BTModule, { BTTile, IntArray2D, createOneTileResult, } from '../data.js';
export default class FocusBTModule extends BTModule {
constructor(instr) {
super();
Object.defineProperty(this, "instr", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "cachedCheckResult", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
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;
if (!this.cachedCheckResult)
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 };
}
}