Constellations/js/Solver/SingleCell.js

44 lines
897 B
JavaScript

import * as CellSet from Geometry.CellSet;
import * as Strategy from Solver.Strategy;
import {diagonal, getColumn, getRow, plus} from Geometry.Vector;
return {
find: find,
perimeter: perimeter
};
function find(solvingState) {
return Strategy.map(
stepOfCell(solvingState.missing),
Strategy.tryEach(solvingState.colorZones.map(getSingleCellIn))
);
}
function getSingleCellIn(cellSet) {
return function() {
if(cellSet.size() == 1) {
return cellSet.toList()[0];
}
};
}
function stepOfCell(missing) {
return function(cell) {
return {
reason: 'singleCell',
empty: perimeter(cell).intersection(missing),
star: new CellSet.CellSet(cell)
};
};
}
function perimeter(cell) {
var union = CellSet.union([
CellSet.rectangle(plus(cell, diagonal(-1)), diagonal(3)),
CellSet.row(getRow(cell)),
CellSet.column(getColumn(cell))
]);
union.remove(cell);
return union;
}