4 ms·
I'll wait for the 1-line APL/J/K implementation in the comments.
by comma_at 4y ago
I'll wait for the 1-line APL/J/K implementation in the comments.
- rak1507 4y agoChallenge Accepted! Here is the core: using UDLR unlike the article snake←{d←9 11○0J1\*'DRUL'⍳⍺ ⋄ n←(⍴⍵)|(⊣/c)+-⍣(d≡p)⊢d⊣p←-⍨/c←(⍴⍵)⊤2↑⍒,⍵ ⋄ b←(1+⌈/,⍵)@(⊂n)⊢⍵ ⋄ ¯1=n⌷⍵:1-⍨@(⊂(?∘≢⊃⊣)⍸0=⍵)⊢b ⋄ 1-⍨@(0∘<)⊢b} Here's a rudimentary display function that displays a list of moves: ∇display moves colours←↑(0 255 0)(0 0 0)(0 0 255)(255 0 0) ⍝ green black blue red 'b'⎕WC'Bitmap'('Bits'(0 0⍴0))('CMap'colours) ⍝ create bitmap 'f'⎕WC'Form' 'Snake demo'('Size' 500 500)('Coord' 'pixel')('Picture'b) ⍝ create form with b as the background s←1 ¯1@(⍉↓10 10⊤2?100)⊢10 10⍴0 ⍝ start position b.Bits←50/50⌿(¯1 0 1,⌈/,s)⍸s ⍝ display snake :For move :In moves ⍝ loop over moves s←move snake s ⍝ update using 'snake' function b.Bits←50/50⌿(¯1 0 1,⌈/,s)⍸s ⍝ update bitmap ⎕DL÷50 ⍝ delay by 1/50 s :EndFor ∇ Here's a video of the result: https://twitter.com/rak_1507/status/1557869849631670274?s=20 https://twitter.com/rak_1507/status/1557869849631670274?s=20