Wave function collapse: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added a quick translation of the python ditty on the talk page, why not.)
m (syntax highlighting fixup automation)
Line 29:
 
=={{header|C}}==
{{trans|J}}<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 190:
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 (other than <code>m</code>, <code>y</code> and <code>i</code>, the comments on [[#J|the j implementation]] should be directly relevant here). Also, when assembling the result at the end, it was convenient to treat the block overlap issue during indexing.
Line 217:
=={{header|J}}==
 
Implementation:<langsyntaxhighlight Jlang="j">blocks=: 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
Line 238:
lap=. {{ y#~(+0=i.@#)-.;m$<n{.1 }}
({:y)lap({:$m)"1 ({.y)lap({:$m),/"2,/0 2 1 3|:(y$i){m
}}</langsyntaxhighlight>
 
We work with the 3x3 partial tiles, and the larger 17x17 tile which we are randomly generating. (17x17 because every 3x3 block contributes 2x2 pixels to the result and along a horizontal and vertical edge row and column of the tile, the 3x3 blocks contribute an additional row and column of pixels.)
Line 254:
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 tiles and three runs of wave function collapse (three, to illustrate randomness):<langsyntaxhighlight Jlang="j"> (<"2) 1j1#"1 ' #'{~ tiles
┌──────┬──────┬──────┬──────┬──────┐
│ │ │ # │ # │ # │
Line 281:
│ # # # # # # │# # # # # # # # # # # # # # # │# # # # # # # # # # # │
│ # # # # │ # # # # # # │ # # # # # │
└──────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┘</langsyntaxhighlight>
 
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use v5.36;
use experimental 'for_list';
 
Line 388:
my @bdims = (5,3,3);
my @size = (8,8);
say wfc(\@Blocks, \@bdims, \@size);</langsyntaxhighlight>
{{out}}
<pre> # # # #
Line 412:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/wfc.htm here].<br>
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\WaveFunctionCollapse.exw
Line 690:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=== trivial ditty ===
Line 699:
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)
<!--<langsyntaxhighlight Phixlang="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>
Line 729:
<span style="color: #000000;">make_rows</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Raku}}==
{{trans|C}}
<syntaxhighlight lang="raku" perl6line># 20220728 Raku programming solution
 
my @Blocks = ( [ <0 0 0>,
Line 841:
my @tile = wfc @Blocks».List.flat, @bdims, @size ;
 
say .join.trans( [ '0', '1' ] => [ ' ', '# ' ] ) for @tile.rotor(17)</langsyntaxhighlight>
{{out}}
<pre>
Line 869:
 
The following is a translation of his 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.
<langsyntaxhighlight lang="ecmascript">import "random" for Random
 
var rand = Random.new()
Line 1,018:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
10,333

edits