Langton's ant: Difference between revisions

Content added Content deleted
(→‎{{header|APL}}: Add comments)
Line 507: Line 507:
⍝ If rows not specified, set equal to columns
⍝ If rows not specified, set equal to columns
⍺ ← ⍵
⍺ ← ⍵

⍝ 0=white, 1=black. Start with all white
⍝ 0=white, 1=black. Start with all white
grid ← ⍺ ⍵ ⍴ 0
grid ← ⍺ ⍵ ⍴ 0

⍝ Start the ant in the middle
⍝ Start the ant in the middle
ant ← 2 ÷⍨ ⍺ ⍵
ant ← 2 ÷⍨ ⍺ ⍵

⍝ Aimed ina random direction
⍝ Aimed in a random direction
dir ← ?4
dir ← ?4

⍝ return everything in a tuple
⍝ return everything in a tuple
grid ant dir
grid ant dir
Line 524: Line 528:
dir ← 1 + 4|dir+2×grid[⊂ant]
dir ← 1 + 4|dir+2×grid[⊂ant]


⍝ Toggle cell coloer
⍝ Toggle cell color
grid[⊂ant]←1-grid[⊂ant]
grid[⊂ant] ← 1 - grid[⊂ant]


⍝ Advance along dir. Since coordinates are matrix order (row,col),
⍝ Advance along dir. Since coordinates are matrix order (row,col),
⍝ up is -1 0, right is 0 1, down is 1 0, and left is 0 -1
⍝ up is -1 0, right is 0 1, down is 1 0, and left is 0 -1
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
}
}