Wave function collapse: Difference between revisions

→‎{{header|Wren}}: Updated in line with C version of which it is a translation.
m (→‎{{header|J}}: grammar)
(→‎{{header|Wren}}: Updated in line with C version of which it is a translation.)
Line 268:
 
=={{header|Wren}}==
{{incomplete||the rendering stage of the C version was incorrect (fixed now)}}
{{trans|C}}
Well, I don't know whether this task is going to be deleted or not andthough, asgiven Ithe don'teffort fullyRdm understandhas itput anywayinto understanding it, I can'td saybe I'min favor now of letting it stand. It is after all an interesting application of an algorithm inspired by quantum mechanics botheredto eithergenerating wayimages.
 
The following is a translation of his C version.
However, as Rdm has obviously put a lot of effort into understanding it and has written a C version, I thought I'd have a go at translating it into Wren.
 
It's difficult with something like this where the results are random to know whether you've translated it correctly or not but one thing I did notice about the C version is that, in the final set of loops in the ''wfc'' function, the indexing is overflowing the allocated size of the ''tiled'' array (256) getting up to 272. I don't know whether the intention was to wrap around (as ''tiled'' is a ''char*'') but, for now, I've simply ignored indices above 255 in the Wren version.
<lang ecmascript>import "random" for Random
 
var rand = Random.new()
 
var tilesblocks = [
0, 0, 0,
0, 0, 0,
Line 297 ⟶ 294:
]
 
var wfc = Fn.new { |tilesblocks, tdim, target|
var N = target[0] * target[1]
var t0 = target[0]
Line 320 ⟶ 317:
horz[j+i*td0] = 1
for (k in 0...td1) {
if (tilesblocks[0+td2*(k+td1*i)] != tilesblocks[(td2-1)+td2*(k+td1*j)]) {
horz[j+i*td0] = 0
break
Line 332 ⟶ 329:
vert[j+i*td0]= 1
for (k in 0...td2) {
if (tilesblocks[k+td2*(0+td1*i)] != tilesblocks[k+td2*((td2-1)+td1*j)]) {
vert[j+i*td0]= 0
break
Line 343 ⟶ 340:
for (i in 0...td0) {
for (j in 0...td0) {
allow[ (i*td0)+j] = vert[(ij*td0)+ji] /* above (north) */
allow[ stride +(i*td0)+j] = horz[(ij*td0)+ji] /* left (west) */
allow[(2*stride)+(i*td0)+j] = horz[(ji*td0)+ij] /* right (east) */
allow[(3*stride)+(i*td0)+j] = vert[(ji*td0)+ij] /* below (south) */
}
}
Line 394 ⟶ 391:
for (i in 0...td0) {
if (wave[ind+i] != 0) {
possible[d] = i
d = d + 1
}
Line 401 ⟶ 398:
}
if (!R) return null
var tiledtile = List.filled((1+t0*t1*(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))
iftile[t] = blocks[j1 + td2*(ti1 <+ tiled.counttd1*R[j0+t1*i0]) {]
tiled[t] = tiles[j1 + td1*(i1 + td2*R[j0+t1*i0])]
}
}
}
}
}
return tiledtile
}
 
var tdims = [5, 3, 3]
var size = [8, 8]
var tiledtile = wfc.call(tilesblocks, tdims, size)
if (!tiledtile) return
for (i in 0..1516) {
for (j in 0..1516) {
System.write("%(" #"[tiledtile[j+i*1617]]) ")
}
System.print()
Line 431 ⟶ 426:
Sample output:
<pre>
# # # # #
# # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # #
# # # # # }
</pre>
9,483

edits