Constellations/js/Solver.js

54 lines
1.3 KiB
JavaScript

import * as CellSet from Geometry.CellSet;
import set from Grid.Util;
import * as Inclusion from Solver.Inclusion;
import * as SingleCell from Solver.SingleCell;
import * as State from Solver.State;
import * as Strategy from Solver.Strategy;
return {
rate: rate,
solve: solve,
findNextStep: findNextStep
};
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;}
);
}
return solvingState.constellation;
}
function rate(coloring) {
}
function findNextStep(solvingState) {
return Strategy.tryEach([
Inclusion.find(solvingState),
SingleCell.find(solvingState)
]);
}
function applyStep(solvingState) {
return function(step) {
console.log(step);
['empty', 'star'].forEach(function(attribute) {
if(step[attribute] != undefined) {
step[attribute].iter(function(cell) {
set(solvingState.constellation, cell, attribute == 'star');
forget(solvingState, cell);
});
}
});
};
}
function forget(solvingState, cell) {
State.getCellSets(solvingState).concat(solvingState.missing)
.forEach(function(cellSet) {cellSet.remove(cell);});
}