Constellations/js/Solver.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-08-10 21:12:39 +02:00
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;
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)
]);
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(function(cell) {
set(solvingState.constellation, cell, attribute == 'star');
forget(solvingState, cell);
});
2022-07-31 16:32:24 +02:00
}
});
};
2022-07-31 16:32:24 +02:00
}
function forget(solvingState, cell) {
State.getCellSets(solvingState).concat(solvingState.missing)
.forEach(function(cellSet) {cellSet.remove(cell);});
2022-07-31 16:32:24 +02:00
}