Constellations/js/Solver/SingleCell.js

51 lines
1.1 KiB
JavaScript

import * as CellSet from Geometry.CellSet;
import * as Strategy from Solver.Strategy;
import * as State from Solver.State;
import {diagonal, getColumn, getRow, plus} from Geometry.Vector;
return {
find: find,
getRange: getRange
};
function find(solvingState) {
return Strategy.tryEach(
State.zoneNames(solvingState).map(function(name) {
return Strategy.map(
stepOfCell(solvingState, name),
Strategy.tryEach(solvingState.zones[name].map(getSingleCellIn))
);
})
);
}
function getSingleCellIn(cellSet) {
return function() {
if(cellSet.size() == 1) {
return cellSet.toList()[0];
}
};
}
function stepOfCell(solvingState, name) {
return function(cell) {
return {
reason: 'singleCell',
set: name,
empty: getRange(solvingState, cell),
star: new CellSet.CellSet(cell)
};
};
}
function getRange(solvingState, cell) {
var union = CellSet.union([
solvingState.zones.colors[solvingState.getColor(cell)],
solvingState.zones.rows[getRow(cell)],
solvingState.zones.columns[getColumn(cell)],
CellSet.rectangle(plus(cell, diagonal(-1)), diagonal(3))
.intersection(solvingState.missing)
]);
union.remove(cell);
return union;
}