UNPKG

@logic-pad/core

Version:
141 lines (140 loc) 4.36 kB
import { Color } from '../../primitives.js'; export var BTTile; (function (BTTile) { BTTile[BTTile["Empty"] = 0] = "Empty"; BTTile[BTTile["Dark"] = 1] = "Dark"; BTTile[BTTile["Light"] = 2] = "Light"; BTTile[BTTile["NonExist"] = 3] = "NonExist"; })(BTTile || (BTTile = {})); export class BTGridData { constructor(tiles, connections, modules, width, height) { Object.defineProperty(this, "tiles", { enumerable: true, configurable: true, writable: true, value: tiles }); Object.defineProperty(this, "connections", { enumerable: true, configurable: true, writable: true, value: connections }); Object.defineProperty(this, "modules", { enumerable: true, configurable: true, writable: true, value: modules }); Object.defineProperty(this, "width", { enumerable: true, configurable: true, writable: true, value: width }); Object.defineProperty(this, "height", { enumerable: true, configurable: true, writable: true, value: height }); this.tiles = tiles; this.connections = connections; this.modules = modules; this.width = width; this.height = height; } getTile(x, y) { return this.tiles[y][x]; } setTileWithConnection(x, y, tile) { for (const pos of this.connections[y][x]) { this.tiles[pos.y][pos.x] = tile; } } isInBound(x, y) { return x >= 0 && x < this.width && y >= 0 && y < this.height; } getEdges(pos) { const positions = []; if (pos.x > 0) { if (this.getTile(pos.x - 1, pos.y) !== BTTile.NonExist) positions.push({ x: pos.x - 1, y: pos.y }); } if (pos.x + 1 < this.width) { if (this.getTile(pos.x + 1, pos.y) !== BTTile.NonExist) positions.push({ x: pos.x + 1, y: pos.y }); } if (pos.y > 0) { if (this.getTile(pos.x, pos.y - 1) !== BTTile.NonExist) positions.push({ x: pos.x, y: pos.y - 1 }); } if (pos.y + 1 < this.height) { if (this.getTile(pos.x, pos.y + 1) !== BTTile.NonExist) positions.push({ x: pos.x, y: pos.y + 1 }); } return positions; } clone() { return new BTGridData(this.tiles.map(arr => [...arr]), this.connections, this.modules, this.width, this.height); } } export class IntArray2D { constructor(array, width, height) { Object.defineProperty(this, "array", { enumerable: true, configurable: true, writable: true, value: array }); Object.defineProperty(this, "width", { enumerable: true, configurable: true, writable: true, value: width }); Object.defineProperty(this, "height", { enumerable: true, configurable: true, writable: true, value: height }); this.array = array; this.width = width; this.height = height; } static create(width, height) { return new IntArray2D(new Uint8Array(width * height), width, height); } set(x, y, value) { this.array[y * this.width + x] = value; } get(x, y) { return this.array[y * this.width + x]; } clone() { return new IntArray2D(new Uint8Array(this.array), this.width, this.height); } } export default class BTModule { checkLocal(grid, _) { return this.checkGlobal(grid); } } export function getOppositeColor(color) { return color === BTTile.Dark ? BTTile.Light : BTTile.Dark; } export function colorToBTTile(color) { if (color === Color.Gray) return BTTile.Empty; else if (color === Color.Light) return BTTile.Light; else return BTTile.Dark; } export function createOneTileResult(grid, pos, score = 1) { const tilesNeedCheck = IntArray2D.create(grid.width, grid.height); tilesNeedCheck.set(pos.x, pos.y, 1); const ratings = [{ pos, score }]; return { tilesNeedCheck, ratings }; }