@logic-pad/core
Version:
45 lines (44 loc) • 1.61 kB
JavaScript
import BTModule, { IntArray2D, colorToBTTile, getOppositeColor, } from '../data.js';
export default class ConnectAllBTModule extends BTModule {
instr;
constructor(instr) {
super();
this.instr = instr;
}
checkGlobal(grid) {
const color = colorToBTTile(this.instr.color);
// Find all same cells
const sameCells = [];
for (let y = 0; y < grid.height; y++) {
for (let x = 0; x < grid.width; x++) {
if (grid.getTile(x, y) === color) {
sameCells.push({ x, y });
}
}
}
// If there are no same cells, return true
if (sameCells.length === 0)
return { tilesNeedCheck: null, ratings: null };
const queue = [sameCells[0]];
const visited = IntArray2D.create(grid.width, grid.height);
// Perform flood fill
visited.set(sameCells[0].x, sameCells[0].y, 1);
while (queue.length > 0) {
const curPos = queue.pop();
for (const edge of grid.getEdges(curPos)) {
if (visited.get(edge.x, edge.y) ||
grid.getTile(edge.x, edge.y) === getOppositeColor(color)) {
continue;
}
visited.set(edge.x, edge.y, 1);
queue.push(edge);
}
}
// Check if any same cell is not reachable
for (const cell of sameCells) {
if (!visited.get(cell.x, cell.y))
return false;
}
return { tilesNeedCheck: null, ratings: null };
}
}