Langton's ant: Difference between revisions
Content added Content deleted
m (→{{header|APL}}: Formatting) |
(→{{header|APL}}: Add comments) |
||
Line 502: | Line 502: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
<syntaxhighlight lang="apl"> |
<syntaxhighlight lang="apl"> |
||
⍝ initialize a Langton's Ant setup with a grid of size left x right (square by default) |
|||
langton ← { |
|||
⍝ If rows not specified, set equal to columns |
|||
⍺ ← ⍵ |
|||
⍝ 0=white, 1=black. Start with all white |
|||
grid ← ⍺ ⍵ ⍴ 0 |
|||
⍝ Start the ant in the middle |
|||
ant ← 2 ÷⍨ ⍺ ⍵ |
|||
⍝ Aimed ina random direction |
|||
dir ← ?4 |
|||
⍝ return everything in a tuple |
|||
grid ant dir |
|||
} |
|||
⍝ iterate one step: takes and return state as created by langton function |
|||
step ← { |
step ← { |
||
grid ant |
grid ant dir ← ⍵ |
||
⚫ | |||
⍝ Turn left or right based on grid cell |
|||
⚫ | |||
⍝ Toggle cell coloer |
|||
grid[⊂ant]←1-grid[⊂ant] |
grid[⊂ant]←1-grid[⊂ant] |
||
⚫ | |||
⍝ Advance along dir. Since coordinates are matrix order (row,col), |
|||
⚫ | |||
ant+←(4 2⍴¯1 0 0 1 1 0 0 ¯1)[dir;] |
ant+←(4 2⍴¯1 0 0 1 1 0 0 ¯1)[dir;] |
||
grid ant dir |
grid ant dir |