Turmites

Table of contents

  1. Langton's ant

Langton's ant

use graphics: canvas, sleep

function turmite(argm={})
   {speed,skip,w=120,h=80} = argm
   a = list(h).map(|y| w*[0])
   d=0; px=w//2; py=h//2
   c = canvas(4*w,4*h)
   for k in cycle(1..skip)
      if a[py][px]==0
        d = (d+1)%4
        a[py][px] = 1
        c.rgb(0.6,0.6,0.4)
      else
        d = (d-1)%4
        a[py][px] = 0
        c.rgb(0.0,0.1,0.1)
      end
      c.fill(4*px,4*py,3,3);
      if k==1
        c.flush()
        sleep(1/speed)
        if c.key()=="q" then break end
      end
      if   d==0 then px = (px+1)%w
      elif d==1 then py = (py-1)%h
      elif d==2 then px = (px-1)%w
      else py = (py+1)%h
      end
   end
end

turmite(speed=50,skip=40)