2022-10-02 22:17:27 +02:00
|
|
|
import id from UnitJS.Fun;
|
2022-09-01 21:51:48 +02:00
|
|
|
import size from Config;
|
2022-10-02 22:17:27 +02:00
|
|
|
import {asAFunction, iter, map, set, square} from Grid.Util;
|
2022-09-01 21:51:48 +02:00
|
|
|
import {diagonal, zero} from Geometry.Vector;
|
2022-08-30 22:39:09 +02:00
|
|
|
import * as CellSet from Geometry.CellSet;
|
|
|
|
|
|
|
|
return {
|
2022-10-23 23:13:49 +02:00
|
|
|
fork: fork,
|
|
|
|
setCells: setCells,
|
2022-10-02 22:17:27 +02:00
|
|
|
start: start,
|
2022-10-23 23:13:49 +02:00
|
|
|
zoneNames: zoneNames
|
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)),
|
2022-10-02 22:17:27 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-02 22:17:27 +02:00
|
|
|
function getColors(grid) {
|
|
|
|
var colors = Array.from({length: size});
|
2022-08-30 22:39:09 +02:00
|
|
|
iter(grid, function(color, cell) {
|
2022-10-02 22:17:27 +02:00
|
|
|
if(colors[color] == undefined) {
|
|
|
|
colors[color] = new CellSet.CellSet();
|
2022-08-30 22:39:09 +02:00
|
|
|
}
|
2022-10-02 22:17:27 +02:00
|
|
|
colors[color].add(cell);
|
|
|
|
});
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fork(state) {
|
|
|
|
var zones = {};
|
2022-10-23 23:13:49 +02:00
|
|
|
for(var name in state.zones) {
|
2022-10-02 22:17:27 +02:00
|
|
|
zones[name] = state.zones[name].map(function(s) {return s.copy();});
|
2022-10-23 23:13:49 +02:00
|
|
|
}
|
2022-10-02 22:17:27 +02:00
|
|
|
return {
|
|
|
|
constellation: map(state.constellation, id),
|
|
|
|
getColor: state.getColor,
|
|
|
|
missing: state.missing.copy(),
|
|
|
|
zones: zones
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-23 23:13:49 +02:00
|
|
|
function setCells(solvingState, value, cellSet) {
|
|
|
|
var forgetList = allZones(solvingState).concat(solvingState.missing);
|
|
|
|
cellSet.iter(function(cell) {
|
2022-10-02 22:17:27 +02:00
|
|
|
set(solvingState.constellation, cell, value);
|
2022-10-23 23:13:49 +02:00
|
|
|
forgetList.forEach(function(solverSet) {
|
|
|
|
solverSet.remove(cell);
|
|
|
|
});
|
|
|
|
});
|
2022-08-30 22:39:09 +02:00
|
|
|
}
|
|
|
|
|
2022-10-23 23:13:49 +02:00
|
|
|
function zoneNames(solvingState) {
|
|
|
|
return Object.keys(solvingState.zones);
|
|
|
|
}
|
|
|
|
|
|
|
|
function allZones(solvingState) {
|
|
|
|
return zoneNames(solvingState).flatMap(function(name) {
|
|
|
|
return solvingState.zones[name];
|
|
|
|
});
|
2022-08-30 22:39:09 +02:00
|
|
|
}
|