Langton's ant: Difference between revisions
→{{header|APL}}: Add comments
m (→{{header|APL}}: Formatting) |
(→{{header|APL}}: Add comments) |
||
Line 502:
=={{header|APL}}==
{{works with|Dyalog 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 ← {
grid ant
dir←1+4|dir+2×grid[⊂ant]▼
⍝ Turn left or right based on grid cell
⍝ Toggle cell coloer
grid[⊂ant]←1-grid[⊂ant]
⍝ coordinates are matrix order: row,col. So up is -1 0, right is 0 1, down is 1 0, and left is 0 -1▼
⍝ Advance along dir. Since coordinates are matrix order (row,col),
▲ ⍝
ant+←(4 2⍴¯1 0 0 1 1 0 0 ¯1)[dir;]
grid ant dir
|