Constellations/js/Solver/State.js

80 lines
1.9 KiB
JavaScript
Raw Normal View History

import id from UnitJS.Fun;
import size from Config;
import {asAFunction, iter, map, set, square} from Grid.Util;
import {diagonal, zero} from Geometry.Vector;
2022-08-30 22:39:09 +02:00
import * as CellSet from Geometry.CellSet;
var zoneNames = ['colors', 'rows', 'columns'];
2022-08-30 22:39:09 +02:00
return {
setCell: setCell,
start: start,
fork: fork
2022-08-30 22:39:09 +02:00
};
function start(coloring) {
var empty = Array.from({length: size});
return {
constellation: square(size),
getColor: asAFunction(coloring),
missing: CellSet.rectangle(zero(), diagonal(size)),
zones: {
colors: getColors(coloring),
rows: empty.map(function(_, i) {return CellSet.row(i);}),
columns: empty.map(function(_, i) {return CellSet.column(i);})
}
2022-08-30 22:39:09 +02:00
};
}
function getColors(grid) {
var colors = Array.from({length: size});
2022-08-30 22:39:09 +02:00
iter(grid, function(color, cell) {
if(colors[color] == undefined) {
colors[color] = new CellSet.CellSet();
2022-08-30 22:39:09 +02:00
}
colors[color].add(cell);
});
return colors;
}
function fork(state) {
var zones = {};
zoneNames.forEach(function(name) {
zones[name] = state.zones[name].map(function(s) {return s.copy();});
2022-08-30 22:39:09 +02:00
});
return {
constellation: map(state.constellation, id),
getColor: state.getColor,
missing: state.missing.copy(),
zones: zones
};
}
function setCells(solvingState, value) {
return function(cellSet) {
for(var i = 0; i < zoneNames.length; i++) {
for(var j = 0; j < size; j++) {
f(solvingState.zones[field][j], cellSet)
}
}
var field = zoneNames[i];
var zone = solvingState.zones[field][j];
if(zone.size() > 0) {
var diff =
});
solvingState.colorZones
.concat(solvingState.rows)
.concat(solvingState.columns)
.concat(solvingState.missing)
.forEach(function(cellSet) {cellSet.remove(cell);});
solvingState.missing
set(solvingState.constellation, cell, value);
2022-08-30 22:39:09 +02:00
}
function f(zone, cellSet) {
if(zone.size() > 0) {
var diff = zone.difference(cellSet);
if(diff.
}
2022-08-30 22:39:09 +02:00
}