@logic-pad/core
Version:
83 lines (82 loc) • 3.4 kB
JavaScript
import BTModule, { BTTile, colorToBTTile, } from '../data.js';
export default class BanPatternBTModule extends BTModule {
constructor(instr) {
super();
Object.defineProperty(this, "instr", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.instr = instr;
}
checkGlobal(grid) {
for (const pattern of this.instr.cache) {
for (let y = 0; y <= grid.height - pattern.height; y++) {
for (let x = 0; x <= grid.width - pattern.width; x++) {
let match = true;
for (const tile of pattern.elements) {
const t = grid.getTile(x + tile.x, y + tile.y);
if (t !== colorToBTTile(tile.color)) {
match = false;
break;
}
}
if (match) {
return false;
}
}
}
}
return { tilesNeedCheck: null, ratings: null };
}
checkLocal(grid, positions) {
const ratings = [];
// TODO: Do not iterate positions
for (const { x, y } of positions) {
const tile = grid.getTile(x, y);
for (const shape of this.instr.cache) {
for (const origin of shape.elements) {
if (colorToBTTile(origin.color) !== tile)
continue;
if (origin.x > x ||
origin.y > y ||
shape.width - origin.x + x > grid.width ||
shape.height - origin.y + y > grid.height)
continue;
// We add a tile into ratings if that tile is the only mismatched tile
let match = true;
let mismatchPos = null;
for (const element of shape.elements) {
const eleTile = grid.getTile(element.x - origin.x + x, element.y - origin.y + y);
if (eleTile === BTTile.Empty) {
if (match) {
match = false;
mismatchPos = {
x: element.x - origin.x + x,
y: element.y - origin.y + y,
};
}
else {
mismatchPos = null;
break;
}
}
else if (eleTile !== colorToBTTile(element.color)) {
match = false;
mismatchPos = null;
break;
}
}
if (match)
return false;
if (mismatchPos)
ratings.push({ pos: mismatchPos, score: 10000 });
}
}
}
// TODO: Redesign API - This is not the best!
// Ratings refresh on every backtrack! Ratings should be appended on the previous result instead of over-writing!
return { tilesNeedCheck: null, ratings };
}
}