Welcome, guest | Sign In | My Account | Store | Cart

(Generalized) Langton's Ant Automaton.

Langton's ant is a 2D Turing machine. In this version, at each new x,y location the ant reads the byte value and replaces it w/ another, and chooses a new direction (one of 8 possible directions) to move.

Byte replacement and direction values are read from random arrays.

Java, 119 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// <applet code="Ant" width=100 height=100>
// <PARAM NAME="refresh" VALUE="60">
// <PARAM NAME="rndPixels" VALUE="100">
// </applet>
// (Generalized) Langton's Ant Automaton
// http://en.wikipedia.org/wiki/Langton%27s_ant
// FB - 201102163
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.util.Arrays;

public class Ant extends Applet
{
    Image img;
    int pix[],mat[];
    int red[],green[],blue[];
    int w,h;
    long ctm;
    int rep[],dir[]; // byte replacement and direction arrays
    int[] dirx={1,1,0,-1,-1,-1,0,1};
    int[] diry={0,1,1,1,0,-1,-1,-1};
    int x,y; // location of the ant
    int refresh; // refresh period in seconds
    int rndPixels; // # of initial random pixels on the screen

    private void putPixel(int x,int y,int c)
    {
        int alpha=255;
        pix[w*y+x]=(alpha<<24)|(red[c]<<16)|(green[c]<<8)|blue[c];
        mat[w*y+x]=c;
    }
    
    public void init()
    {
        refresh=Integer.parseInt(getParameter("refresh"))*1000;
        rndPixels=Integer.parseInt(getParameter("rndPixels"));
        w=getSize().width;
        h=getSize().height;
        pix=new int[w*h]; 
        mat=new int[w*h];
        Arrays.fill(pix,2147483647);
        
        // create a color palette (RGBY)
        red=new int[256];
        green=new int[256];
        blue=new int[256];
        
        for(int i=0;i<256;i++)
        {
            switch((int)(i/64))
            {
                case 0:
                    red[i]=(i%64)*3+64;
                    green[i]=0;
                    blue[i]=0;
                    break;
                case 1:
                    red[i]=0;
                    green[i]=(i%64)*3+64;
                    blue[i]=0;
                    break;
                case 2:
                    red[i]=0;
                    green[i]=0;
                    blue[i]=(i%64)*3+64;
                    break;
                case 3:
                    red[i]=(i%64)*3+64;
                    green[i]=(i%64)*3+64;
                    blue[i]=0;
                    break;
            }
        }

        // create byte replacement and direction arrays
        rep=new int[256];
        dir=new int[256];
        
        for(int i=0;i<256;i++)
        {
            rep[i]=(int)(Math.random()*256);
            dir[i]=(int)(Math.random()*8);
        }
        
        // put random pixels onto screen and start the ant from the last one
        for(int i=0;i<rndPixels;i++)
        {
            x=(int)(Math.random()*w);
            y=(int)(Math.random()*h);
            putPixel(x,y,(int)(Math.random()*256));
        }

        ctm=System.currentTimeMillis();        
    }

    public void paint(Graphics g)
    {
        // restart the applet periodically
        if((System.currentTimeMillis()-ctm)>=refresh) {init();}

        // run the ant 1 step
        int c=mat[w*y+x];
        putPixel(x,y,rep[c]);
        x+=dirx[dir[c]];
        y+=diry[dir[c]];
        // keep the ant in the screen
        if(x<0) x+=w;
        if(x>w-1) x-=w;
        if(y<0) y+=h;
        if(y>h-1) y-=h;
        
        img=createImage(new MemoryImageSource(w,h,pix,0,w));
        g.drawImage(img,0,0,this);
        repaint();
    }

    public void update(Graphics g) { paint(g); } // to prevent flickering
}
Created by FB36 on Thu, 17 Feb 2011 (MIT)
Java recipes (20)
FB36's recipes (148)

Required Modules

  • (none specified)

Other Information and Tasks