2022-10-23 23:13:49 +02:00
|
|
|
import getRange from Solver.SingleCell;
|
|
|
|
import CellSet from Geometry.CellSet;
|
2022-10-02 22:22:23 +02:00
|
|
|
import * as State from Solver.State;
|
2022-10-23 23:13:49 +02:00
|
|
|
import * as Strategy from Solver.Strategy;
|
2022-10-02 22:22:23 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
find: find
|
|
|
|
};
|
|
|
|
|
|
|
|
function find(solvingState) {
|
2022-10-23 23:13:49 +02:00
|
|
|
return Strategy.tryEach(
|
|
|
|
solvingState.missing.map(findContradiction(solvingState))
|
|
|
|
);
|
|
|
|
}
|
2022-10-02 22:22:23 +02:00
|
|
|
|
2022-10-23 23:13:49 +02:00
|
|
|
function findContradiction(solvingState) {
|
2022-10-02 22:22:23 +02:00
|
|
|
return function(cell) {
|
2022-10-23 23:13:49 +02:00
|
|
|
return function() {
|
|
|
|
var forked = State.fork(solvingState);
|
|
|
|
State.setCells(forked, false, getRange(forked, cell));
|
|
|
|
return getContradiction(solvingState, forked, cell);
|
|
|
|
};
|
|
|
|
};
|
2022-10-02 22:22:23 +02:00
|
|
|
}
|
|
|
|
|
2022-10-23 23:13:49 +02:00
|
|
|
function stepOfContradiction(cell, name, i) {
|
|
|
|
var step = {
|
|
|
|
reason: 'noStarLeft',
|
|
|
|
empty: new CellSet(cell),
|
|
|
|
};
|
|
|
|
step['in_' + name] = i;
|
|
|
|
return step;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContradiction(beforeState, afterState, cell) {
|
|
|
|
for(var name in beforeState.zones) {
|
|
|
|
var zones = beforeState.zones[name];
|
|
|
|
for(var i = 0; i < zones.length; i++) {
|
|
|
|
if(zones[i].size() > 0 && afterState.zones[name][i].size() < 1) {
|
|
|
|
return stepOfContradiction(cell, name, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-02 22:22:23 +02:00
|
|
|
}
|