Constellations/js/Solver/Hypothesis.js

45 lines
1 KiB
JavaScript
Raw Permalink Normal View History

import getRange from Solver.SingleCell;
import CellSet from Geometry.CellSet;
import * as State from Solver.State;
import * as Strategy from Solver.Strategy;
return {
find: find
};
function find(solvingState) {
return Strategy.tryEach(
solvingState.missing.map(findContradiction(solvingState))
);
}
function findContradiction(solvingState) {
return function(cell) {
return function() {
var forked = State.fork(solvingState);
State.setCells(forked, false, getRange(forked, cell));
return getContradiction(solvingState, forked, cell);
};
};
}
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);
}
}
}
}