Constellations/js/Solver.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

import set from Grid.Util;
import * as Hypothesis from Solver.Hypothesis;
import * as Inclusion from Solver.Inclusion;
import * as SingleCell from Solver.SingleCell;
import * as State from Solver.State;
import * as Strategy from Solver.Strategy;
2022-07-31 16:32:24 +02:00
return {
rate: rate,
solve: solve,
findNextStep: findNextStep
2022-07-31 16:32:24 +02:00
};
function solve(coloring) {
var solvingState = State.start(coloring);
var stuck = false;
while(!stuck && solvingState.missing.size() > 0) {
Strategy.execute(
findNextStep(solvingState),
applyStep(solvingState),
function() {console.log('Solver is stuck'); stuck = true;}
2022-07-31 16:32:24 +02:00
);
}
return solvingState.constellation;
2022-07-31 16:32:24 +02:00
}
function rate(coloring) {
2022-07-31 16:32:24 +02:00
}
function findNextStep(solvingState) {
return Strategy.tryEach([
Inclusion.find(solvingState),
SingleCell.find(solvingState),
Hypothesis.find(solvingState)
]);
2022-07-31 16:32:24 +02:00
}
function applyStep(solvingState) {
return function(step) {
console.log(step);
['empty', 'star'].forEach(function(attribute) {
if(step[attribute] != undefined) {
step[attribute].iter(State.setCell(solvingState, attribute == 'star'));
2022-07-31 16:32:24 +02:00
}
});
};
2022-07-31 16:32:24 +02:00
}