UNPKG

@logic-pad/core

Version:
46 lines (45 loc) 1.75 kB
import { move } from '../../../dataHelper.js'; import BTModule, { BTTile, IntArray2D, createOneTileResult, getOppositeColor, } from '../data.js'; export default class DartBTModule extends BTModule { instr; 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 pos = move({ x: this.instr.x, y: this.instr.y }, this.instr.orientation); let completed = 0; let empty = 0; while (grid.isInBound(pos.x, pos.y)) { // Opposite tiles if (grid.getTile(pos.x, pos.y) === getOppositeColor(tile)) { completed += 1; if (completed > this.instr.number) return false; } // Empty tiles if (grid.getTile(pos.x, pos.y) === BTTile.Empty) empty += 1; pos = move(pos, this.instr.orientation); } if (completed + empty < this.instr.number) return false; return this.buildCheckAndRating(grid); } buildCheckAndRating(grid) { const tilesNeedCheck = IntArray2D.create(grid.width, grid.height); const ratings = []; let pos = { x: this.instr.x, y: this.instr.y }; while (grid.isInBound(pos.x, pos.y)) { if (grid.getTile(pos.x, pos.y) === BTTile.Empty) { tilesNeedCheck.set(pos.x, pos.y, 1); ratings.push({ pos, score: 1 }); } pos = move(pos, this.instr.orientation); } return { tilesNeedCheck, ratings }; } }