Wave function collapse: Difference between revisions

m
m (→‎{{header|C}}: add a few (hopefully) orienting comments)
m (→‎{{header|Wren}}: Minor tidy)
 
(45 intermediate revisions by 7 users not shown)
Line 1:
{{draft task}}
{{Clarify task}}
The '''Wave Function Collapse''' algorithm is a heuristic for generating tiled images.
Write the solution for Wave Function Collapse based on the [https://youtu.be/rI_y2GAlQFM Coding Challenge 171: Wave Function Collapse] and create new map 8x8 tiles with fourth T-blocks with variously directions (┤ ┴ ┬ ├) or blank tiles (space).
 
The algorithm begins with a collection of equal sized image blocks and randomly places them, one at a time, within a grid subject to the tiling constraint and an entropy constraint, and it wraps (the top row of blocks in the grid is treated as ''adjacent'' to the bottom row of blocks, and similarly the left and right columns of blocks are treated as adjacent to each other).
 
The blocks are tiled within the grid. ''Tiled'' means they are placed with a one pixel overlap and the tiling constraint requires that the pixels overlapping border between two adjacent blocks match.
 
''Entropy'', here, means the number of blocks eligible to be placed in an unassigned grid location. The entropy constraint here is that each block is placed in a grid location with minimum entropy. (Placing a block may constrain the entropy of its four nearest neighbors -- up, down, left, right.)
 
For this task, we start with five blocks of 3x3 pixels and place them in an 8x8 grid to form a 17x17 tile. A tile is a block which may be tiled with itself. Here, we show these five blocks adjacent but not ''tiled'':
 
<table border="1px"><tr><td>
<table><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr></table>
</td><td>
<table><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr></table>
</td><td>
<table><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr></table>
</td><td>
<table><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr></table>
</td><td>
<table><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr><tr><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td><td style="background-color: black; height: 20px; width: 20px">&nbsp;</td><td style="background-color: white; height: 20px; width: 20px">&nbsp;</td></tr></table>
</td></tr></table>
 
 
Note that this algorithm sometimes does not succeed. If an unassigned grid location has an entropy of 0, the algorithm fails and returns an empty or null result. We'll ignore those failure cases for this task.
 
;Reference [https://www.boristhebrave.com/2020/04/13/wave-function-collapse-explained/ WFC explained] and [https://github.com/mxgmn/WaveFunctionCollapse another WFC explained]
 
=={{header|C}}==
{{trans|J}}<langsyntaxhighlight Clang="c">#include <assertstdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
#define XY(row, col, width) ((col)+(row)*(width))
char tiles[5][3][3]= {
#define XYZ(page, row, col, height, width) XY(XY(page, row, height), col, width)
char blocks[5][3][3]= {
{
{0, 0, 0},
Line 35 ⟶ 60:
}
};
 
/* avoid problems with slightly negative numbers and C's X%Y */
char *wfc(char *tiles, int *tdim /* 5,3,3 */, int *target /* 8,8 */) {
#define MOD(X,Y) ((Y)+(X))%(Y)
int N= target[0]*target[1], t0= target[0], t1= target[1];
int *adj= calloc(N*4, sizeof (int));
char *wfc(char *blocks, int *bdim /* 5,3,3 */, int *tdim /* 8,8 */) {
for (int i= 0; i<target[0]; i++) {
int N= tdim[0]*tdim[1], td0= tdim[0], td1= tdim[1];
for (int j=0; j<t1; j++) {
int *adj= calloc(N*4, sizeof (int)); /* indices in R of the four adjacent blocks */
int k= j+t1*i;
for (int i= 0; i<td0; i++) int m= 4*k;{
for (int adj[m ]j=0; j<td1; j + t1*((t0+i-1)%t0); /* above (1) */{
adj[m+1XYZ(i,j,0,td1,4)]= XY(t1+jMOD(i-1, td0)%t1 + t1*, MOD(j, td1), itd1); /* above (index 1 in a 3x3 /* left (3grid) */
adj[m+2XYZ(i,j,1,td1,4)]= XY(MOD(i, td0), MOD(j+-1, td1)%t1, +td1); t1/* left (index 3 i;in a 3x3 /* right (5grid) */
adj[m+3XYZ(i,j,2,td1,4)]= XY(MOD(i, td0), MOD(j+1, td1), td1); /* right + t1*((index 5 in i+1)%t0);a 3x3 /* below (7grid) */
adj[XYZ(i,j,3,td1,4)]= XY(MOD(i+1, td0), MOD(j, td1), td1); /* below (index 7 in a 3x3 grid) */
}
}
int td0bd0= tdimbdim[0], td1bd1= tdimbdim[1], td2bd2= tdimbdim[2];
char *horz= malloc(td0bd0*td0bd0); /* blocks which can sit next to each other horizontally */
for (int i= 0; i<td0bd0; i++) {
for (int j= 0; j<td0bd0; j++) {
horz[j+XY(i*td0,j,bd0)]= 1;
for (int k= 0; k<td1bd1; k++) {
if (tilesblocks[0+td2*XYZ(k+td1*i, k, 0, bd1, bd2)] != tilesblocks[XYZ(td2j, k, bd2-1)+td2*(k+td1*j, bd1, bd2)]) {
horz[j+XY(i*td0, j, bd0)]= 0;
break;
}
}
}
}
char *vert= malloc(td0bd0*td0bd0); /* blocks which can sit next to each other vertically */
for (int i= 0; i<td0bd0; i++) {
for (int j= 0; j<td0bd0; j++) {
vert[j+XY(i*td0,j,bd0)]= 1;
for (int k= 0; k<td2bd2; k++) {
if (tilesblocks[k+td2*XYZ(0+td1*i, 0, k, bd1, bd2)] != tilesblocks[k+td2*XYZ((td2j, bd1-1)+td1*j, k, bd1, bd2)]) {
vert[j+XY(i*td0, j, bd0)]= 0;
break;
}
Line 74 ⟶ 99:
}
}
char *allow= malloc(4*(bd0+1)*bd0); /* all block constraints, based on neighbors */
int stride= (td0+1)*td0;
char *memset(allow=, 1, malloc(4*stride(bd0+1)*bd0);
for (int i= 0; i<bd0; i++) {
memset(allow, 1, 4*stride);
for (int ij= 0; ij<td0bd0; ij++) {
for allow[XYZ(int0, i, j=, 0;bd0+1, bd0)]= vert[XY(j<td0, i, bd0)]; j++/* above (north) {*/
allow[XYZ(1, i, j, bd0+1, (i*td0bd0)+j]= verthorz[XY(j, i*td0, bd0)+j]; /* aboveleft (northwest) */
allow[XYZ(2, i, j, stridebd0+1, +(i*td0bd0)+j]= horz[XY(i*td0, j, bd0)+j]; /* left right (westeast) */
allow[XYZ(2*stride)+(3, i*td0)+, j, bd0+1, bd0)]= horzvert[XY(i, j*td0, bd0)+i]; /* rightbelow (eastsouth) */
allow[(3*stride)+(i*td0)+j]= vert[(j*td0)+i]; /* below (south) */
}
}
free(horz);
free(vert);
int *R= calloc(N, sizeof (int));
int *todo= calloc(N, sizeof (int));
char *wave= malloc(N*td0bd0);
int *entropy= calloc(N, sizeof (int));
int *indices= calloc(N, sizeof (int));
int min;
int *possible= calloc(td0bd0, sizeof (int));
int *R= calloc(N, sizeof (int)); /* tile expressed as list of block indices */
for (int i= 0; i<N; i++) R[i]= td0;
for (int i= 0; i<N; i++) R[i]= bd0;
while (1) {
int c= 0;
for (int i= 0; i<N; i++)
if (td0bd0==R[i])
todo[c++]= i;
if (!c) break;
min= td0bd0;
for (int i= 0; i<c; i++) {
entropy[i]= 0;
for (int j= 0; j<td0bd0; j++) {
int K= 4*todo[i];
char tentropy[i]+=
wave[XY(i*td0)+, j, bd0)]=
allow[XYZ(0, td0*R[adj[K XY(todo[i],0,4)]+j], &j, bd0+1, /*bd0)] above */&
allow[XYZ(1, stride +td0*R[adj[K+XY(todo[i],1,4)]], j, bd0+j1, bd0)] & /* left */
allow[XYZ(2*stride)+td0*, R[adj[K+XY(todo[i],2,4)]]+, j], &bd0+1, bd0)] /* right */&
allow[XYZ(3*stride)+td0*, R[adj[K+XY(todo[i],3,4)]]+, j]; , bd0+1, /* below */bd0)];
entropy[i]+= t;
}
if (entropy[i] < min) min= entropy[i];
Line 126 ⟶ 149:
}
int ndx= indices[random()%d];
int ind= ndx*td0bd0;
d= 0;
for (int i= 0; i<td0bd0; i++) {
if (wave[ind+i]) possible[d++]= i;
}
Line 141 ⟶ 164:
free(possible);
if (!R) return NULL;
char *tiledtile= malloc(t0*t1(1+td0*(td1bd1-1))*(td21+td1*(bd2-1)));
for (int i0= 0; i0<t0td0; i0++)
for (int i1= 0; i1<td1bd1; i1++)
for (int j0= 0; j0<t1td1; j0++)
for (int j1= 0; j1<td2bd2; j1++)
tiledtile[XY(XY(j0, j1, +bd2-1), XY(td2i0, i1, bd1-1)*(j0, 1+ t1td1*(i1 + (td1bd2-1)*i0))]=
tilesblocks[j1XYZ(R[XY(i0, +j0, td1*()], i1, +j1, td2*R[j0+t1*i0]bd1, bd2)];
free(R);
return tiledtile;
}
 
int main() {
int tdimsbdims[3]= {5,3,3};
int size[2]= {8,8};
time_t t;
srandom((unsigned) time(&t));
char *tiledtile= wfc((char*)tilesblocks, tdimsbdims, size);
if (!tiledtile) exit(0);
for (int i= 0; i<1617; i++) {
for (int j= 0; j<1617; j++) {
printf("%c ", " #"[tiledtile[j+XY(i*16, j, 17)]]);
}
printf("\n");
}
free(tile);
exit(0);
}</langsyntaxhighlight>
 
Note: here we use <code>R</code> where J used <code>i</code>, because we use i as an index/loop counter. Also(other than <code>m</code>, when<code>y</code> assemblingand <code>i</code>), the resultcomments aton [[#J|the end,j itimplementation]] wasshould morebe convenientdirectly torelevant drophere. theAlso, finalwhen rowassembling andthe columnresult ofat the smallend, tiles.it Hopefullywas noconvenient significantto translationtreat errorsthe haveblock beenoverlap introducedissue hereduring indexing.
 
For simplicity, we use <tt>char</tt> as our pixel datatype (and for truth values), and <tt>int</tt> for indices (C offers a variety of similar datatypes but nothing we are doing here is big enough for that to be a concern).
 
{{out}}
<pre> # # # # #
# # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # #
# # # # # #</pre>
# # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # #</pre>
 
=={{header|J}}==
 
Implementation:<langsyntaxhighlight Jlang="j">tilesblocks=: 0,(|.@|:)^:(i.4)0,1 1 1,:0 1 0
wfc=: {{
adj=.: y#.y|"1(y#:,i.y)+"1/<:3 3#:1 3 5 7
horz=.: ({."1 -:"1/ {:"1) m NB. horizontal tile pairs
vert=.: ({."2 -:"1/ {:"2) m NB. vertical tile pairs
north=.: 1,~|:vert NB. adj 1 constraint
south=.: 1,~|:vert NB. adj 7 constraint
west=.: 1,~|:horz NB. adj 3 constrint
east=.: 1,~|:horz NB. adj 5 constraint
allow=.: north,west,east,:south
i=. (?#m) (?#i)} i=.: ,y$_1
while. #todo=.: I._1=i do.
wave=.: */"2 ((todo{adj){i){"0 2"1 3 allow
entropy=.: +/"1 wave
min=.: <./ entropy
if. 0=min do. EMPTY return. end.
ndx=.: ({~ ?@#) I.min=entropy
i=.: (({~?@#)I.ndx{wave) (ndx{todo)} i
end.
,/"2,/0lap=. 2{{ 1 3|:(y$#~(+0=i.@#)-.;m$<n{1 .1 }."2 m}
({:y)lap({:$m)"1 ({.y)lap({:$m),/"2,/0 2 1 3|:(y$i){m
}}</lang>
}}</syntaxhighlight>
 
For this task, a "tile" represents a rectangular matrix of black or white pixels which is thought of a being potentially repeated arbitrarily in all directions. And, weWe work with two kinds of tiles: the 3x3 argumentpartial tiles, and the 16x16 larger 17x17 tile which we are randomly generating. (Conceptually,17x17 ofbecause theevery argument3x3 tiles overlaps with its neighbors by one pixel, thus each argument tile onlyblock contributes a 2x2 pixel regionpixels to the result. (Perhapsand wealong shoulda makehorizontal aand onevertical pixeledge exceptionrow forand twocolumn borders inof the larger tile, whichthe we3x3 areblocks assemblingcontribute --an creatingadditional arow 17x17and tile -- but if it's actually a tile, regularitycolumn of structure in its larger tiling space suggests that that would be a bad ideapixels.))
 
Here, <code>m</code> is the list of tilesargument blocks (which are the 3x3 blocks in this example), and <code>i</code> represents an 8x8 list of indexes into that list (or, conceptually, whatever dimensions were specified by <code>y</code>, the right argument to <code>wfc</code> -- but for this task <code>y</code> will always be <code>8 8</code>), with <code>_1</code> being a placeholder for the case where the index hasn't been choosen -- initially, we pick a random location in <code>i</code> and assign an arbitrarily picked tile to that location.
 
<code>adj</code> indexes into <code>i</code> -- for each item in <code>i</code>, <code>adj</code> selects that item, the item "above" it, the item to the "left" of it, the item to the "right" of it and the item "below" it (with scare quotes because the constructed tile represented by <code>i</code> "wraps around" on all sides). And, <code>allow</code> lists the allowable tilesblocks corresponding to each of those <code>adj</code> constraints (there's no particular order to the items in <code>allow</code> -- it must include all four directions, but it does not matter which direction we look at "first").
 
To build <code>allow</code> we first matched the left side of each tileblock with the right side of each tileblock (cartesian product) forming <code>horz</code> and similarly matched the tops and bottoms of the tiles forming <code>vert</code>. Then we build <code>north</code> which limits tiles based on the tile above it, and similarly for <code>west</code>, <code>east</code>, and <code>south</code> (when the adjacent tile is a <code>_1</code> tile, no limit is imposed).
 
Once we're set up, we drop into a loop: <code>todo</code> selects the unchosen tileblock locations, <code>wave</code>lists for each of the unchoosenunchosen tilesblock locations (for each <code>todo</code> value in <code>i</code> we select the tiles allowed by each of its adjacent locations and find the set intersection of all of those), <code>entropy</code> counts how many tiles are eligible for each of those location, and <code>min</code> is the smallest value in <code>entropy</code>. <code>ndx</code> is a randomly picked index into <code>todo</code> with minimal entropy and for that location we randomly pick one of the options and update <code>i</code> with it. (When there's only one option remaining, "randomly pick" here means we pick that option.)
 
Once we've assigned a tileblock to every location in <code>i</code>, we use those indices to assemble the result. Here,(the we3x3 dropblocks theoverlap firstat rowtheir andborders columnso ofwe eachintroduce ofa themechanism smallto tilesdiscard beforethe assemblingredundant thempixels).
 
For task purposes here, we will use space to represent a white pixel and "#" to represent a black pixel. Also, because characters are narrow, we will insert a space between each of these "pixels" to better approximate a square aspect ratio.
 
Task example: (the initial tilesblocks and three runs of wave function collapse (three, to illustrate randomness):<lang J> (<"2) 1j1#"1 ' #'{~ tiles
<syntaxhighlight lang="j"> (<"2) 1j1#"1 ' #'{~ blocks
┌──────┬──────┬──────┬──────┬──────┐
│ │ │ # │ # │ # │
Line 233 ⟶ 262:
└──────┴──────┴──────┴──────┴──────┘
 
task=: {{ 1j1#"1 ' #'{~ tilesblocks wfc 8 8}}
task&.>0 0 0
┌──────────────────────────────────┬──────────────────────────────────┬──────────────────────────────────┐
┌────────────────────────────────┬────────────────────────────────┬────────────────────────────────┐
# # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # │
│# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # │
# # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # #
# # # # # # # # # # # # # │# # # # # # # # # # # # # # # │# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
│# # # # # # # # # # # # # # # │# # # # # # # # # # # # │# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
│# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
│# # # # # # # # # # # # # # # │# # # # # # # # # # # │# # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # │# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # │# # # # # # # # # # # # # # # │# # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
│ # # # # │ # # # # # # │ # # # # # │
└────────────────────────────────┴────────────────────────────────┴────────────────────────────────┘</lang>
└──────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┘</syntaxhighlight>
 
=={{header|Nim}}==
{{trans|C}}
<syntaxhighlight lang="Nim">import std/[algorithm, math, random]
 
template XY(row, col, width: int): int =
col + row * width
 
template XYZ(page, row, col, height, width: int): int =
XY(XY(page, row, height), col, width)
 
const Blocks = @[byte 0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
1, 1, 1,
0, 1, 0,
0, 1, 0,
0, 1, 1,
0, 1, 0,
0, 1, 0,
1, 1, 1,
0, 0, 0,
0, 1, 0,
1, 1, 0,
0, 1, 0]
 
 
proc wfc(blocks: seq[byte]; bdim: (int, int, int); tdim: (int, int); ): seq[byte] =
let (td0, td1) = tdim
let n = td0 * td1
var adj = newSeq[int](n * 4) # Indices in R of the four adjacent blocks.
 
for i in 0..<td0:
for j in 0..<td1:
adj[XYZ(i, j, 0, td1, 4)]= XY(floorMod(i-1, td0), floorMod(j, td1), td1)
adj[XYZ(i, j, 1, td1, 4)]= XY(floorMod(i, td0), floorMod(j-1, td1), td1)
adj[XYZ(i, j, 2, td1, 4)]= XY(floorMod(i, td0), floorMod(j+1, td1), td1)
adj[XYZ(i, j, 3, td1, 4)]= XY(floorMod(i+1, td0), floorMod(j, td1), td1)
 
let (bd0, bd1, bd2) = bdim
 
var horz = newSeq[byte](bd0 * bd0)
for i in 0..<bd0:
for j in 0..<bd0:
horz[XY(i, j, bd0)]= 1
for k in 0..<bd1:
if blocks[XYZ(i, k, 0, bd1, bd2)] != blocks[XYZ(j, k, bd2-1, bd1, bd2)]:
horz[XY(i, j, bd0)]= 0
 
var vert = newSeq[byte](bd0 * bd0)
for i in 0..<bd0:
for j in 0..<bd0:
vert[XY(i, j, bd0)]= 1
for k in 0..<bd2:
if blocks[XYZ(i, 0, k, bd1, bd2)] != blocks[XYZ(j, bd1-1, k, bd1, bd2)]:
vert[XY(i, j, bd0)]= 0
break
 
var allow = newSeq[byte](4 * (bd0 + 1) * bd0)
allow.fill(1)
for i in 0..<bd0:
for j in 0..<bd0:
allow[XYZ(0, i, j, bd0+1, bd0)] = vert[XY(j, i, bd0)]
allow[XYZ(1, i, j, bd0+1, bd0)] = horz[XY(j, i, bd0)]
allow[XYZ(2, i, j, bd0+1, bd0)] = horz[XY(i, j, bd0)]
allow[XYZ(3, i, j, bd0+1, bd0)] = vert[XY(i, j, bd0)]
 
var
todo = newSeq[int](n)
wave = newSeq[byte](n * bd0)
entropy = newSeq[int](n)
indices = newSeq[int](n)
possible = newSeq[int](bd0)
var r = newSeq[int](n)
r.fill(bd0)
while true:
var c = 0
for i in 0..<n:
if bd0 == r[i]:
todo[c]= i
inc c
if c == 0: break
var min = bd0
for i in 0..<c:
entropy[i] = 0
for j in 0..<bd0:
let val = allow[XYZ(0, r[adj[XY(todo[i],0,4)]], j, bd0+1, bd0)] and
allow[XYZ(1, r[adj[XY(todo[i],1,4)]], j, bd0+1, bd0)] and
allow[XYZ(2, r[adj[XY(todo[i],2,4)]], j, bd0+1, bd0)] and
allow[XYZ(3, r[adj[XY(todo[i],3,4)]], j, bd0+1, bd0)]
wave[XY(i, j, bd0)] = val
entropy[i] += val.int
if entropy[i] < min: min = entropy[i]
if min == 0:
r.setLen(0)
break
var d = 0
for i in 0..<c:
if min == entropy[i]:
indices[d] = i
inc d
var ndx = indices[rand(d - 1)]
let ind = ndx * bd0
d = 0
for i in 0..<bd0:
if wave[ind + i] != 0:
possible[d] = i
inc d
r[todo[ndx]] = possible[rand(d - 1)];
 
if r.len == 0: return @[]
result = newSeq[byte]((1 + td0 * (bd1 - 1)) * (1 + td1 * (bd2 - 1)))
for i0 in 0..<td0:
for i1 in 0..<bd1:
for j0 in 0..<td1:
for j1 in 0..<bd2:
result[XY(XY(j0, j1, bd2-1), XY(i0, i1, bd1-1), 1+td1*(bd2-1))] =
blocks[XYZ(r[XY(i0, j0, td1)], i1, j1, bd1, bd2)]
 
const BDims = (5, 3, 3)
const Size = (8, 8)
randomize()
let tile = wfc(Blocks, BDims, Size)
if tile.len == 0: quit QuitSuccess
for i in 0..16:
for j in 0..16:
stdout.write " #"[tile[XY(i, j, 17)]], ' '
echo()
</syntaxhighlight>
 
{{out}}
<pre> # # # #
# # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # #
# # # # </pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use v5.36;
use experimental 'for_list';
 
my @Blocks = ( [ <0 0 0>, <0 0 0>, <0 0 0> ],
[ <0 0 0>, <1 1 1>, <0 1 0> ],
[ <0 1 0>, <0 1 1>, <0 1 0> ],
[ <0 1 0>, <1 1 1>, <0 0 0> ],
[ <0 1 0>, <1 1 0>, <0 1 0> ],
);
 
sub X($a,$b) { my @c; for my $aa (0..$a-1) { map { push @c, $aa, $_ } 0..$b-1 } @c }
 
sub XY( $row, $col, $width) { $col + $row * $width }
sub XYZ($page, $row, $col, $height, $width) { XY( XY($page, $row, $height), $col, $width) }
 
sub wfc($B, $bdim, $tdim) {
my ($td0,$td1) = @$tdim;
my $N = $td0 * $td1;
my @blocks = map @$_, @$B; # flatten
 
my @adj; # indices in R of the four adjacent blocks
for my($i,$j) (X $td0, $td1) {
$adj[XYZ($i, $j, 0, $td1, 4)] = XY(($i-1)%$td0, $j %$td1, $td1); # above (index 1)
$adj[XYZ($i, $j, 1, $td1, 4)] = XY( $i %$td0, ($j-1)%$td1, $td1); # left (index 3)
$adj[XYZ($i, $j, 2, $td1, 4)] = XY( $i %$td0, ($j+1)%$td1, $td1); # right (index 5)
$adj[XYZ($i, $j, 3, $td1, 4)] = XY(($i+1)%$td0, $j %$td1, $td1); # below (index 7)
}
 
my ($bd0,$bd1,$bd2) = @$bdim;
my @horz; # blocks which can sit next to each other horizontally
for my($i,$j) (X $bd0, $bd0) {
@horz[XY($i,$j,$bd0)] = 1;
for my $k (0..$bd1-1) {
$horz[XY($i, $j, $bd0)]= 0 if $blocks[XYZ($i, $k, 0, $bd1, $bd2)]
!= $blocks[XYZ($j, $k, $bd2-1, $bd1, $bd2)]
}
}
 
my @vert; # blocks which can sit next to each other vertically */
for my($i,$j) (X $bd0, $bd0) {
$vert[XY($i,$j,$bd0)] = 1;
for my $k (0..$bd2-1) {
if ($blocks[XYZ($i, 0, $k, $bd1, $bd2)] != $blocks[XYZ($j, $bd1-1, $k, $bd1, $bd2)]) {
$vert[XY($i, $j, $bd0)] = 0;
last
}
}
}
 
my @allow = (1) x (4*($bd0+1)*$bd0); # all block constraints, based on neighbors
for my($i,$j) (X $bd0, $bd0) {
$allow[XYZ(0, $i, $j, $bd0+1, $bd0)] = $vert[XY($j, $i, $bd0)]; # above (north)
$allow[XYZ(1, $i, $j, $bd0+1, $bd0)] = $horz[XY($j, $i, $bd0)]; # left (west)
$allow[XYZ(2, $i, $j, $bd0+1, $bd0)] = $horz[XY($i, $j, $bd0)]; # right (east)
$allow[XYZ(3, $i, $j, $bd0+1, $bd0)] = $vert[XY($i, $j, $bd0)]; # below (south)
}
 
my @R = ($bd0) x $N;
my (@todo, @wave, @entropy, @indices, $min, @possible);
 
while () {
my $c;
for (0..$N-1) { $todo[$c++] = $_ if $bd0 == $R[$_] }
last unless $c;
$min = $bd0;
for my $i (0..$c-1) {
$entropy[$i] = 0;
for my $j (0..$bd0-1) {
$entropy[$i] +=
$wave[XY($i, $j, $bd0)] =
$allow[XYZ(0, $R[ $adj[XY($todo[$i],0,4)] ], $j, $bd0+1, $bd0)] &
$allow[XYZ(1, $R[ $adj[XY($todo[$i],1,4)] ], $j, $bd0+1, $bd0)] &
$allow[XYZ(2, $R[ $adj[XY($todo[$i],2,4)] ], $j, $bd0+1, $bd0)] &
$allow[XYZ(3, $R[ $adj[XY($todo[$i],3,4)] ], $j, $bd0+1, $bd0)]
}
$min = $entropy[$i] if $entropy[$i] < $min
}
 
@R=[] and last unless $min;
 
my $d = 0;
for (0..$c-1) { $indices[$d++] = $_ if $min == $entropy[$_] }
my $ind = $bd0 * (my $ndx = $indices[ int rand $d ]);
$d = 0;
for (0..$bd0-1) { $possible[$d++] = $_ if $wave[$ind+$_] }
$R[$todo[$ndx]] = $possible[ int rand $d ];
}
 
return "DOES NOT COMPUTE" unless @R > 1;
 
my @tile;
for my($i0,$i1)(X $td0, $bd1) {
for my($j0,$j1) (X $td1, $bd2) {
$tile[XY(XY($j0, $j1, $bd2-1), XY($i0, $i1, $bd1-1), 1+$td1*($bd2-1))] =
(' ','#')[ $blocks[XYZ($R[XY($i0, $j0, $td1)], $i1, $j1, $bd1, $bd2)] ]
}
}
my $width = 2 * sqrt scalar @tile;
join(' ', @tile) =~ s/.{$width}\K(?=.)/\n/gr;
}
 
my @bdims = (5,3,3);
my @size = (8,8);
say wfc(\@Blocks, \@bdims, \@size);</syntaxhighlight>
{{out}}
<pre> # # # #
# # # # # # # # # #
# # #
# # # # # # # # # #
# # # #
# # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # #
# #
# # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # #
# # # #</pre>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/wfc.htm here].<br>
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\WaveFunctionCollapse.exw
-- =====================================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (do until, and a switch &lt;atom&gt; bugfix)</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">bOverlap</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (debug aids)</span>
<span style="color: #000000;">bSpat</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span> <span style="color: #000080;font-style:italic;">-- (show space as '@')</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">8</span> <span style="color: #000080;font-style:italic;">-- board size (nb must be even)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Wave Function Collapse"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">help_text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Press 'o' to toggle overlap (see note below).
Press '@' to toggle display spaces as '@'.
Press '-' to decrease board size (min 2x2).
Press '+' to increase board size (max 40x40).
Press ' ' to start afresh.
Note that it is not really possible to visually verify
that a pattern is correct unless overlap is turned off.
"""</span>
<span style="color: #000080;font-style:italic;">-- space, T, -|, iT, |- </span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tilem</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0b0000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b0111</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1011</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1110</span><span style="color: #0000FF;">},</span>
<span style="color: #000080;font-style:italic;">-- L, R, U, D</span>
<span style="color: #000000;">valid</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0b00101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b01001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b00011</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">0b11010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b01110</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b01001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b11100</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">0b11010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10110</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b11100</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">0b11010</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b01110</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10110</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b00011</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">0b00101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b01110</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b10110</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b11100</span><span style="color: #0000FF;">}}</span>
<span style="color: #000080;font-style:italic;">-- eg valid[1=space][4=D] means space or T can go below it,
-- with bits of each valid[][] being read right-to-left.</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">grid</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- -1 if unknown, else one of tilem</span>
<span style="color: #000000;">allowed</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- initially 0b11111 (all possible) -&gt; 1 bit set</span>
<span style="color: #000000;">entropy</span> <span style="color: #000080;font-style:italic;">-- count matching allowed (speedwise/simplicity)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">left</span> <span style="color: #000080;font-style:italic;">-- N*N..0, with 0=finished, -1=FAIL, -2=REDO</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">lowest_entropy</span><span style="color: #0000FF;">()</span>
<span style="color: #000080;font-style:italic;">-- returns a random tile from those with the lowest entropy</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">col</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">me</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">grid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- ignoring any already done</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">entropy</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">e</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">me</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">e</span><span style="color: #0000FF;"><</span><span style="color: #000000;">me</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">me</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">e</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">row</span><span style="color: #0000FF;">,</span><span style="color: #000000;">col</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">row</span><span style="color: #0000FF;">,</span><span style="color: #000000;">col</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pop_count</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- Kernigans bit counter:</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">p</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">&&=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">e</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">permitted</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- Given p, 0b00001..0b11111, a 1-5 bitmask,
-- calculate the permitted tiles in direction
-- d (1..4 for LRUD), eg a T(2) can have 2|3|4
-- on the right, and a 3 can have 1|5, so if
-- p is 0b01100 the result is 0b11111 (all),
-- that is, when d is 2 (ie right).
--</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">nm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">nm</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">or_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">valid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">d</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">nm</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">propagate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- Propagate the permitted tiles, given that only
-- those in p (0b00001..0b11111, a 1..5 bitmask)
-- are now allowed at [r][c]. Note this can fail,
-- especially for some ~2x3 enclosed spaces, and
-- in that case you want to undo everything, and
-- clear some initial permitted bit setting.
--</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span> <span style="color: #008080;">in</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}}</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">--LRUD</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">dr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dc</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">nr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dr</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">nc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dc</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">nr</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">nr</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">N</span> <span style="color: #008080;">and</span> <span style="color: #000000;">nc</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">nc</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">N</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">nm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">permitted</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">op</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">allowed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">nr</span><span style="color: #0000FF;">][</span><span style="color: #000000;">nc</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">op</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nm</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">np</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">op</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">np</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">allowed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">nr</span><span style="color: #0000FF;">][</span><span style="color: #000000;">nc</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">np</span>
<span style="color: #000000;">entropy</span><span style="color: #0000FF;">[</span><span style="color: #000000;">nr</span><span style="color: #0000FF;">][</span><span style="color: #000000;">nc</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pop_count</span><span style="color: #0000FF;">(</span><span style="color: #000000;">np</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">propagate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">np</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">wfc</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- wave function collapse: (iterative/one cell at a time, because
-- this was once on a timer, but now wfc_init() just loops.)
--
-- There is, I guess, around a 1 in 8000 chance of this failing,
-- which means 1 in 10 40x40 boards fail, presumbably because it
-- has surrounded an area and none of the edges will work out.
-- Setting left to -2 triggers the outer retry in wfc_init().
--</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lowest_entropy</span><span style="color: #0000FF;">(),</span>
<span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">grid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">allowed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- pick a random but valid tile:</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span> <span style="color: #008080;">in</span> <span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- in case propagation fails, make a backup</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">saveae</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">({</span><span style="color: #000000;">allowed</span><span style="color: #0000FF;">,</span><span style="color: #000000;">entropy</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">grid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tilem</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">allowed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">m</span>
<span style="color: #000000;">entropy</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">left</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">propagate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">grid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">g</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">allowed</span><span style="color: #0000FF;">,</span><span style="color: #000000;">entropy</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">saveae</span>
<span style="color: #000000;">saveae</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> <span style="color: #000080;font-style:italic;">-- kill refcounts</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">m</span> <span style="color: #000080;font-style:italic;">-- don't try this again!</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- printf(1,"panic: allowed[%d][%d] := 0!\n",{r,c})</span>
<span style="color: #000000;">left</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">2</span> <span style="color: #000080;font-style:italic;">-- trigger a restart</span>
<span style="color: #008080;">return</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pop_count</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">==</span><span style="color: #000000;">entropy</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">allowed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span>
<span style="color: #000000;">entropy</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">e</span>
<span style="color: #000000;">left</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">wfc_init</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">do</span>
<span style="color: #000000;">grid</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">),</span><span style="color: #000000;">N</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">allowed</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b11111</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">),</span><span style="color: #000000;">N</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">entropy</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">),</span><span style="color: #000000;">N</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">left</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">*</span><span style="color: #000000;">N</span>
<span style="color: #008080;">do</span>
<span style="color: #000000;">wfc</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">until</span> <span style="color: #000000;">left</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">0</span>
<span style="color: #008080;">until</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">!=-</span><span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000080;font-style:italic;">-- (the rest of this is all fairly standard code)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">cw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ih</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">N2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (nb forces N to be even)</span>
<span style="color: #000000;">N3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">N2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">N</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">d9</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">)/(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">N</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">d2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">d4</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">N2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">d</span>
<span style="color: #000000;">cw</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cw</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetLineWidth</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ry</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bOverlap</span><span style="color: #0000FF;">?</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">-(</span><span style="color: #000000;">row</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">N</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">d9</span>
<span style="color: #0000FF;">:</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">-(</span><span style="color: #000000;">row</span><span style="color: #0000FF;">-</span><span style="color: #000000;">N3</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">d</span><span style="color: #0000FF;">-</span><span style="color: #000000;">d2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">col</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rx</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bOverlap</span><span style="color: #0000FF;">?</span><span style="color: #000000;">cw</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">col</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">N</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">d9</span>
<span style="color: #0000FF;">:</span><span style="color: #000000;">cw</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">col</span><span style="color: #0000FF;">-</span><span style="color: #000000;">N3</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">d2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">grid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">row</span><span style="color: #0000FF;">][</span><span style="color: #000000;">col</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">entropy</span><span style="color: #0000FF;">[</span><span style="color: #000000;">row</span><span style="color: #0000FF;">][</span><span style="color: #000000;">col</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0b1111</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_RED</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetTextAlignment</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_CENTER</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasText</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ry</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"?"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">bSpat</span> <span style="color: #008080;">and</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0b0000</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">cdCanvasSetTextAlignment</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_CENTER</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasText</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ry</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"@"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">!=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dxy</span> <span style="color: #008080;">in</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}}</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- LURD</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">dr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dc</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dxy</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bOverlap</span><span style="color: #0000FF;">?</span><span style="color: #000000;">d9</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">:</span><span style="color: #000000;">d2</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">cdCanvasLine</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ry</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ry</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dr</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">o</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bOverlap</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">" (no overlap)"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">left</span><span style="color: #0000FF;">!=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">" FAIL"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s [%dx%d] %s%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">title</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">o</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">help_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandln</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupMessage</span><span style="color: #0000FF;">(</span><span style="color: #000000;">title</span><span style="color: #0000FF;">,</span><span style="color: #000000;">help_text</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bWrap</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_IGNORE</span> <span style="color: #000080;font-style:italic;">-- (don't open browser help!)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*dlg*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (standard practice for me)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F5</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (let browser reload work)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">help_cb</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">switch</span> <span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'c'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">wfc</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'o'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">bOverlap</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">not</span> <span style="color: #000000;">bOverlap</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'-'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">N</span><span style="color: #0000FF;">-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">fallthrough</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'+'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">N</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">40</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">fallthrough</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">' '</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">wfc_init</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'@'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">bSpat</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">not</span> <span style="color: #000000;">bSpat</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_IGNORE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGLCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=440x440"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="%s"`</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">title</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"KEY_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupSetAttributeHandle</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PARENTDIALOG"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">wfc_init</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=== trivial ditty ===
Translation of the Python ditty on the talk page:<br>
This trivial solution works because we're only ever adding left to right,<br>
and therefore only verifying L/U vs R/D, and it is not possible to have<br>
any two R/D for which no tile is valid. Were this to fill in each row/line<br>
more randomly, it would soon fail with no tile matching >=3 neighbours.<br>
(lowest entropy would also fail b/c it'd favour 2 no conn over 3 with)
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">unicode_console</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">unicode_console</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">trace</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">make_rows</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">conn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0000.1101.1110.0111.1011"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- RULD flags</span>
<span style="color: #000000;">tiles</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" ... ╠...═╩...═╣...═╦"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"..."</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">--[...aligned]</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">w</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{}}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">w</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">conn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">][</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">conn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[$]][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #000080;font-style:italic;">-- L=R</span>
<span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">conn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">conn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]][</span><span style="color: #000000;">4</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- U=D</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">x</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">))]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tiles</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">make_rows</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
 
=={{header|Raku}}==
{{trans|C}}
<syntaxhighlight lang="raku" line># 20220728 Raku programming solution
 
my @Blocks = ( [ <0 0 0>,
<0 0 0>,
<0 0 0> ],
[ <0 0 0>,
<1 1 1>,
<0 1 0> ],
[ <0 1 0>,
<0 1 1>,
<0 1 0> ],
[ <0 1 0>,
<1 1 1>,
<0 0 0> ],
[ <0 1 0>,
<1 1 0>,
<0 1 0> ], );
 
sub XY(\row, \col, \width) { col+row*width }
sub XYZ(\page, \row, \col, \height, \width) {
XY( XY(page, row, height), col, width)
}
 
sub wfc(@blocks, @bdim, @tdim) {
 
my \N = [*] my (\td0,\td1) = @tdim[0,1];
my @adj; # indices in R of the four adjacent blocks
for ^td0 X ^td1 -> (\i,\j) { # in a 3x3 grid
@adj[XYZ(i,j,0,td1,4)]= XY((i-1)%td0,j%td1,td1); # above (index 1)
@adj[XYZ(i,j,1,td1,4)]= XY(i%td0,(j-1)%td1,td1); # left (index 3)
@adj[XYZ(i,j,2,td1,4)]= XY(i%td0,(j+1)%td1,td1); # right (index 5)
@adj[XYZ(i,j,3,td1,4)]= XY((i+1)%td0,j%td1,td1); # below (index 7)
}
 
my (\bd0,\bd1,\bd2) = @bdim[0..2];
my @horz; # blocks which can sit next to each other horizontally
for ^bd0 X ^bd0 -> (\i,\j) {
@horz[XY(i,j,bd0)] = 1;
for ^bd1 -> \k {
@horz[XY(i, j, bd0)]= 0 if @blocks[XYZ(i, k, 0, bd1, bd2)] !=
@blocks[XYZ(j, k, bd2-1, bd1, bd2)]
}
}
my @vert; # blocks which can sit next to each other vertically */
for ^bd0 X ^bd0 -> (\i,\j) {
@vert[XY(i,j,bd0)] = 1;
for ^bd2 -> \k {
if @blocks[XYZ(i, 0, k, bd1, bd2)] !=
@blocks[XYZ(j, bd1-1, k, bd1, bd2)] {
@vert[XY(i, j, bd0)]= 0 andthen last;
}
}
}
 
my @allow = 1 xx 4*(bd0+1)*bd0; # all block constraints, based on neighbors
for ^bd0 X ^bd0 -> (\i,\j) {
@allow[XYZ(0, i, j, bd0+1, bd0)] = @vert[XY(j, i, bd0)]; # above (north)
@allow[XYZ(1, i, j, bd0+1, bd0)] = @horz[XY(j, i, bd0)]; # left (west)
@allow[XYZ(2, i, j, bd0+1, bd0)] = @horz[XY(i, j, bd0)]; # right (east)
@allow[XYZ(3, i, j, bd0+1, bd0)] = @vert[XY(i, j, bd0)]; # below (south)
}
 
my (@R, @todo, @wave, @entropy, @indices, $min, @possible) = bd0 xx N;
loop {
my $c = 0;
for ^N { @todo[$c++] = $_ if bd0 == @R[$_] }
last unless $c;
$min = bd0;
for ^$c -> \i {
@entropy[i]= 0;
for ^bd0 -> \j {
@entropy[i] +=
@wave[XY(i, j, bd0)] =
@allow[XYZ(0, @R[@adj[XY(@todo[i],0,4)]], j, bd0+1, bd0)] +&
@allow[XYZ(1, @R[@adj[XY(@todo[i],1,4)]], j, bd0+1, bd0)] +&
@allow[XYZ(2, @R[@adj[XY(@todo[i],2,4)]], j, bd0+1, bd0)] +&
@allow[XYZ(3, @R[@adj[XY(@todo[i],3,4)]], j, bd0+1, bd0)]
}
$min = @entropy[i] if @entropy[i] < $min
}
unless $min { @R=[] andthen last } # original behaviour
#unless $min { @R = bd0 xx N andthen redo } # if failure is not an option
 
my $d = 0;
for ^$c { @indices[$d++] = $_ if $min == @entropy[$_] }
my \ind = bd0 * my \ndx = @indices[ ^$d .roll ];
$d = 0;
for ^bd0 { @possible[$d++] = $_ if @wave[ind+$_] }
@R[@todo[ndx]] = @possible[ ^$d .roll ];
}
 
exit unless @R.Bool;
 
my @tile;
for ^td0 X ^bd1 X ^td1 X ^bd2 -> (\i0,\i1,\j0,\j1) {
@tile[XY(XY(j0, j1, bd2-1), XY(i0, i1, bd1-1), 1+td1*(bd2-1))] =
@blocks[XYZ(@R[XY(i0, j0, td1)], i1, j1, bd1, bd2)]
}
 
return @tile
}
 
my (@bdims,@size) := (5,3,3), (8,8);
 
my @tile = wfc @Blocks».List.flat, @bdims, @size ;
 
say .join.trans( [ '0', '1' ] => [ ' ', '# ' ] ) for @tile.rotor(17)</syntaxhighlight>
{{out}}
<pre>
# # #
# # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # #
# # # # #
# # # # # # # # # # #
# # # # #
# # # # # # # # # # #
# # # # #
# # # # # # # # # # # #
# # #
</pre>
 
 
=={{header|Wren}}==
{{trans|C}}
The following is a translation of the C version ''before'' macros were added. Wren doesn't support macros and, whilst I could use functions instead, I decided on efficiency grounds to leave it as it is.
<syntaxhighlight lang="wren">import "random" for Random
 
var rand = Random.new()
 
var blocks = [
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
1, 1, 1,
0, 1, 0,
0, 1, 0,
0, 1, 1,
0, 1, 0,
0, 1, 0,
1, 1, 1,
0, 0, 0,
0, 1, 0,
1, 1, 0,
0, 1, 0
]
 
var wfc = Fn.new { |blocks, tdim, target|
var N = target[0] * target[1]
var t0 = target[0]
var t1 = target[1]
var adj = List.filled(4*N, 0)
for (i in 0...t0) {
for (j in 0...t1) {
var k = j + t1*i
var m = 4 * k
adj[m ] = j + t1*((t0+i-1)%t0) /* above (1) */
adj[m+1] = (t1+j-1)%t1 + t1* i /* left (3) */
adj[m+2] = ( j+1)%t1 + t1* i /* right (5) */
adj[m+3] = j + t1*(( i+1)%t0) /* below (7) */
}
}
var td0 = tdim[0]
var td1 = tdim[1]
var td2 = tdim[2]
var horz = List.filled(td0*td0, 0)
for (i in 0...td0) {
for (j in 0...td0) {
horz[j+i*td0] = 1
for (k in 0...td1) {
if (blocks[0+td2*(k+td1*i)] != blocks[(td2-1)+td2*(k+td1*j)]) {
horz[j+i*td0] = 0
break
}
}
}
}
var vert = List.filled(td0*td0, 0)
for (i in 0...td0) {
for (j in 0...td0) {
vert[j+i*td0]= 1
for (k in 0...td2) {
if (blocks[k+td2*(0+td1*i)] != blocks[k+td2*((td2-1)+td1*j)]) {
vert[j+i*td0]= 0
break
}
}
}
}
var stride = (td0+1) * td0
var allow = List.filled(4 * stride, 1)
for (i in 0...td0) {
for (j in 0...td0) {
allow[ (i*td0)+j] = vert[(j*td0)+i] /* above (north) */
allow[ stride +(i*td0)+j] = horz[(j*td0)+i] /* left (west) */
allow[(2*stride)+(i*td0)+j] = horz[(i*td0)+j] /* right (east) */
allow[(3*stride)+(i*td0)+j] = vert[(i*td0)+j] /* below (south) */
}
}
var R = List.filled(N, td0)
var todo = List.filled(N, 0)
var wave = List.filled(N*td0, 0)
var entropy = List.filled(N, 0)
var indices = List.filled(N, 0)
var min = 0
var possible = List.filled(td0, 0)
while (true) {
var c = 0
for (i in 0...N) {
if (td0 == R[i]) {
todo[c] = i
c = c + 1
}
}
if (c == 0) break
min = td0
for (i in 0...c) {
entropy[i] = 0
for (j in 0...td0) {
var K = 4*todo[i]
wave[i*td0 + j] = allow[ td0*R[adj[K ]]+j] & /* above */
allow[ stride +td0*R[adj[K+1]]+j] & /* left */
allow[(2*stride)+td0*R[adj[K+2]]+j] & /* right */
allow[(3*stride)+td0*R[adj[K+3]]+j] /* below */
entropy[i] = entropy[i] + wave[i*td0 + j]
}
if (entropy[i] < min) min = entropy[i]
}
if (min == 0) {
R = null
break
}
var d = 0
for (i in 0...c) {
if (min == entropy[i]) {
indices[d] = i
d = d + 1
}
}
var ndx = indices[rand.int(0, d)]
var ind = ndx * td0
d = 0
for (i in 0...td0) {
if (wave[ind+i] != 0) {
possible[d] = i
d = d + 1
}
}
R[todo[ndx]] = possible[rand.int(0, d)]
}
if (!R) return null
var tile = List.filled((1+t0*(td1-1))*(1+t1*(td2-1)), 0)
for (i0 in 0...t0) {
for (i1 in 0...td1) {
for (j0 in 0...t1) {
for (j1 in 0...td2) {
var t = j1 + (td2-1)*j0 + (1+t1*(td2-1))*(i1 + (td1-1)*i0)
tile[t] = blocks[j1 + td2*(i1 + td1*R[j0+t1*i0])]
}
}
}
}
return tile
}
 
var tdims = [5, 3, 3]
var size = [8, 8]
var tile = wfc.call(blocks, tdims, size)
if (!tile) return
for (i in 0..16) {
for (j in 0..16) {
System.write("%(" #"[tile[j+i*17]]) ")
}
System.print()
}</syntaxhighlight>
 
{{out}}
Sample output:
<pre>
# # # # #
# # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # #
# # # #
# # # # # # # # # #
# # # # # #
# # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # #
# # # # #
</pre>
9,483

edits