Constellations/js/Solver/SingleCell.js

52 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2022-08-30 22:39:09 +02:00
import * as CellSet from Geometry.CellSet;
2022-08-19 08:19:50 +02:00
import * as Strategy from Solver.Strategy;
import * as State from Solver.State;
2022-08-30 22:39:09 +02:00
import {diagonal, getColumn, getRow, plus} from Geometry.Vector;
2022-08-19 08:19:50 +02:00
return {
find: find,
getRange: getRange
2022-08-19 08:19:50 +02:00
};
2022-08-30 22:39:09 +02:00
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))
);
})
2022-08-19 08:19:50 +02:00
);
}
function getSingleCellIn(cellSet) {
return function() {
if(cellSet.size() == 1) {
2022-08-30 22:39:09 +02:00
return cellSet.toList()[0];
2022-08-19 08:19:50 +02:00
}
};
}
function stepOfCell(solvingState, name) {
return function(cell) {
return {
reason: 'singleCell',
set: name,
empty: getRange(solvingState, cell),
star: new CellSet.CellSet(cell)
};
2022-08-19 08:19:50 +02:00
};
}
function getRange(solvingState, cell) {
2022-08-30 22:39:09 +02:00
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)
2022-08-19 08:19:50 +02:00
]);
2022-08-30 22:39:09 +02:00
union.remove(cell);
return union;
2022-08-19 08:19:50 +02:00
}