xxxxxxxxxx
86
const DIMENSION = 15;
const TILE_SIZE = 15;
const dusa = new Dusa(`
#builtin INT_MINUS minus
#builtin INT_PLUS plus
size is ${DIMENSION}.
dim N :- size is N.
dim (minus N 1) :- dim N, N != 1.
delta 0 1.
delta 0 -1.
delta 1 0.
delta -1 0.
parent 1 1 is none.
parent X2 Y2 is { pair X1 Y1? } :-
parent X1 Y1 is _,
delta DX DY,
X2 == plus X1 DX, dim X2,
Y2 == plus Y1 DY, dim Y2.
after N N is none :- size is N.
after X1 Y1 is (pair X2 Y2) :-
after X2 Y2 is _,
parent X2 Y2 is (pair X1 Y1).
`);
let iteration = 0;
const iterator = dusa.solutions;
function setup() {
createCanvas(
Math.max(DIMENSION * TILE_SIZE, 150),
DIMENSION * TILE_SIZE + 30
);
noLoop();
}
function touchStarted() {
draw();
}
function draw() {
const { value: solution } = iterator.next();
erase();
rect(0, 0, Math.max(DIMENSION * TILE_SIZE, 150), 30);
noErase();
fill("black");
if (!solution && iteration == 0) {
text("There are no solutions", 0, 20);
return;
}
if (!solution) {
text(`Solution ${iteration} out of ${iteration}`, 0, 20);
return;
}
iteration += 1;
text(`Solution ${iteration} out of ?`, 0, 20);
for (let col = 1; col <= DIMENSION; col++) {
for (let row = 1; row <= DIMENSION; row++) {
const x = (col-1) * TILE_SIZE;
const y = (row-1) * TILE_SIZE + 30;
if (solution.get('after', col, row)) {
fill("lightgreen");
} else {
fill("white")
}
rect(x, y, TILE_SIZE, TILE_SIZE);
}
}
for (const [col1, row1, value] of solution.lookup('parent')) {
if (value.name !== 'pair') continue;
const [col2, row2] = value.args;
const x1 = (Number(col1)-0.5) * TILE_SIZE;
const x2 = (Number(col2)-0.5) * TILE_SIZE;
const y1 = (Number(row1)-0.5) * TILE_SIZE + 30;
const y2 = (Number(row2)-0.5) * TILE_SIZE + 30;
line(x1,y1,x2,y2)
}
}