Constellations/js/Solver/SingleCell.js

45 lines
936 B
JavaScript
Raw Permalink Normal View History

2022-08-30 22:39:09 +02:00
import * as CellSet from Geometry.CellSet;
import getCellSets from Solver.State;
2022-08-19 08:19:50 +02:00
import * as Strategy from Solver.Strategy;
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
};
2022-08-30 22:39:09 +02:00
function find(solvingState) {
var cellSets = getCellSets(solvingState);
2022-08-19 08:19:50 +02:00
return Strategy.map(
stepOfCell(solvingState.missing),
2022-08-19 08:19:50 +02:00
Strategy.tryEach(cellSets.map(getSingleCellIn))
);
}
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(missing) {
return function(cell) {
return {
reason: 'singleCell',
empty: toEmpty(cell).intersection(missing),
star: new CellSet.CellSet(cell)
};
2022-08-19 08:19:50 +02:00
};
}
function toEmpty(cell) {
2022-08-30 22:39:09 +02:00
var union = CellSet.union([
CellSet.rectangle(plus(cell, diagonal(-1)), diagonal(3)),
CellSet.row(getRow(cell)),
CellSet.column(getColumn(cell))
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
}