@logic-pad/core
Version:
152 lines (151 loc) • 4.63 kB
JavaScript
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 {
tiles;
connections;
modules;
width;
height;
constructor(tiles, connections, modules, width, height) {
this.tiles = tiles;
this.connections = connections;
this.modules = modules;
this.width = width;
this.height = 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 {
array;
width;
height;
constructor(array, width, height) {
this.array = array;
this.width = width;
this.height = 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 };
}
export function checkSubtilePlacement(grid, pos) {
const minX = Math.floor(pos.x);
const minY = Math.floor(pos.y);
if (minX === pos.x && minY === pos.y)
return undefined;
const maxX = Math.ceil(pos.x);
const maxY = Math.ceil(pos.y);
let color = null;
let complete = true;
for (let i = 0; i < 4; i++) {
const x = i % 2 === 0 ? minX : maxX;
const y = i < 2 ? minY : maxY;
if (!grid.isInBound(x, y))
return false;
const tile = grid.getTile(x, y);
if (tile === BTTile.NonExist)
return false;
if (tile !== BTTile.Empty) {
if (color !== null && tile !== color)
return false;
color = tile;
}
else {
complete = false;
}
}
if (complete) {
return undefined;
}
else {
const tilesNeedCheck = IntArray2D.create(grid.width, grid.height);
const ratings = [];
for (let i = 0; i < 4; i++) {
const x = i % 2 === 0 ? minX : maxX;
const y = i < 2 ? minY : maxY;
if (grid.getTile(x, y) === BTTile.Empty) {
tilesNeedCheck.set(x, y, 1);
ratings.push({ pos: { x, y }, score: 1 });
}
}
return { tilesNeedCheck, ratings };
}
}