Wave function collapse: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 4: Line 4:


;Reference [https://www.boristhebrave.com/2020/04/13/wave-function-collapse-explained/ WFC explained] and [https://github.com/mxgmn/WaveFunctionCollapse another WFC explained]
;Reference [https://www.boristhebrave.com/2020/04/13/wave-function-collapse-explained/ WFC explained] and [https://github.com/mxgmn/WaveFunctionCollapse another WFC explained]

=={{header|J}}==

Implementation:<lang J>tiles=: 0,(|.@|:)^:(i.4)0,1 1 1,:0 1 0
wfc=: {{
M=. #.,"2 tiles
opts=: (<each, <@<)M
adj=: y#.y|"1(y#:,i.y)+"1/0,<:3 3#:1 3 5 7
horz=: ({."1 -:"1/ {:"1) m
vert=: ({."2 -:"1/ {:"2) m
id=: <@I.(=i.#tiles),1 NB. identity constraint
north=: <@I.vert,1 NB. adj 1 constraint
south=: <@I.(|:vert),1 NB. adj 7 constraint
west=: <@I.horz,1 NB. adj 3 constrint
east=: <@I.(|:horz),1 NB. adj 5 constraint
allow=: id,north,west,east,:south
i=: (?#m) (?#i)} i=: ,y$_1
while. #todo=: I._1=i do.
wave=: ([-.-.)L:0/"1 (adj{i){"0 1"1 2 allow
entropy=: #@>wave
min=. <./ E=: todo{entropy
if. 0=min do. EMPTY return. end.
ndx=: todo{~({~ ?@#) I.min=E
i=: (({~?@#)ndx{::wave) ndx} i
end.
,/"2,/0 2 1 3|:(y$i){m
}}</lang>

Task example:<lang J> 1j1#"1 ' #'{~ tiles wfc 8 8
# # # #
# # # # # # # # # # # # # # # # #
# # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # #
# # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # #
# # # # # # # # # # # # # # #
# # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # #
# # # #
# # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # #
1j1#"1 ' #'{~ tiles wfc 8 8
# # # #
# # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # #
# # # # # # # # # # # # # # #
# # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # #
# # # # # # # # # # #
# # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # #
# # # # # # # # # # # # # # # # #
# # # # # #
# # # # #
# # # # # # # # # # # # # #
# # # # # </lang>

Revision as of 09:57, 10 July 2022

Wave function collapse is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
This task has been flagged for clarification. Code on this page in its current state may be flagged incorrect once this task has been clarified. See this page's Talk page for discussion.

Write the solution for Wave Function Collapse based on the Coding Challenge 171: Wave Function Collapse and create new map 8x8 tiles with fourth T-blocks with variously directions (┤ ┴ ┬ ├) or blank tiles (space).

Reference WFC explained and another WFC explained

J

Implementation:<lang J>tiles=: 0,(|.@|:)^:(i.4)0,1 1 1,:0 1 0 wfc=: Template:M=.</lang>

Task example:<lang J> 1j1#"1 ' #'{~ tiles wfc 8 8

       #           #     #                 #   
     # #   # # #   # # # #   # # # # # #   # # 
       #     #     #     #     #     #     #   
 #           #                 #     #         
  1. # # # # # # # # # # # # # # # # # # # # # # #
       #           #     #                 #   
 #           #     #     #                 #   
 # #       # #   # # # # # # # # # # # # # # # 
 #           #                 #     #         
 #     #     #     #           #     #     #   
  1. # # # # # # # # # # # # # # # # # # #
 #           #     #     #                 #   
       #     #     #           #     #     #   
  1. # # # # # # # # # # # # # #
 #     #     #     #           #     #     #   
 #                       #     #     #         
  1. # # # # # # # # # # # # # # # # # # # # #
       #     #     #           #     #     #   
 #     #     #     #     #     #     #         
  1. # # # # # # # # # # # # # # # #
 #                       #     #     #         
       #     #     #     #     #     #     #   
  1. # # # # # # # # # # # # # # # # #
 #     #     #     #     #     #     #         
  1j1#"1 ' #'{~ tiles wfc 8 8
 #     #                       #           #   
  1. # # # # # # # # # # # # # # # # # #
             #     #     #     #           #   
 #     #     #     #     #           #         
 # # # #   # # # # # # # # # # # # # # # # # # 
 #     #                       #           #   
 #     #     #     #     #     #     #         
  1. # # # # # # # # # # # # # #
 #     #     #     #     #           #         
 #     #                       #     #     #   
 # # # #   # # # # # # # # #   # # # #   # # # 
 #     #     #     #     #     #     #         
 #     #                       #           #   
  1. # # # # # # # # # #
 #     #                       #     #     #   
 #     #     #     #     #           #         
 # # # #   # # # # # # # # # # # # # # # # # # 
 #     #                       #           #   
             #     #     #           #     #   
  1. # # # # # # # # # # # # # # # #
 #     #     #     #     #           #         
             #     #     #     #           #   
           # #     # # # #   # # # # # #   # # 
             #     #     #           #     #   </lang>