#include "CImg.h"
using namespace cimg_library;
CImgDisplay screen;
CImg<unsigned char> surf;
#define WIDTH 256
#define HEIGHT 128
#define TOTAL WIDTH*HEIGHT
char board[WIDTH][HEIGHT];
class ant
{
public:
int x,y;
char d;
unsigned short color;
unsigned int unvisitted;
void init(int X, int Y,char D)
{
x=X;y=Y;d=D;
color=0;
unvisitted=WIDTH*HEIGHT;
}
void turn()
{
///Have to hard code for the rule set here.
///Make sure to set RULECOUNT to the number of rules!
#define RULECOUNT 9
//LRRRRRLLR
char get=board[x][y];
if(get==0||get==6||get==7){d+=1;}
else{d-=1;}
if(d<0){d=3;}
else if(d>3){d=0;}
}
void forward()
{
if(d==0){x++;}
else if(d==1){y--;}
else if(d==2){x--;}
else {y++;}
if(x<0){x=WIDTH-1;}
else if(x>=WIDTH){x=0;}
if(y<0){y=HEIGHT-1;}
else if(y>=HEIGHT){y=0;}
}
void draw()
{
if(board[x][y]==-1)
{
board[x][y]=0;
unsigned char c[3];
c[0]=255-((color&0x1F)*8);
c[2]=255-(((color>>5)&0x1F)*8);
c[1]=(((color>>10)&0x1F)*8);
surf.draw_point(x,y,c);
unvisitted--;
color++;
}
board[x][y]++;
if(board[x][y]==9board[x][y]==RULECOUNT){board[x][y]=0;}
}
void step()
{
draw();
turn();
forward();
//screen=surf;
//screen.wait(1);
}
};
void renderboard()
{
unsigned char white[]={200,190,180};
surf.draw_rectangle(0,0,WIDTH,HEIGHT,white);
for(int x=0;x<WIDTH;x++)
for(int y=0;y<HEIGHT;y++)
{
char get=board[x][y];
if(get==1){get=1;unsigned char c[]={255*get,255*get,255*get};
surf.draw_point(x,y,c);}
else if(get==0){get=0;unsigned char c[]={255*get,255*get,255*get};
surf.draw_point(x,y,c);}
}
}
int main(int argc, char** argv)
{
screen.assign(WIDTH*4WIDTH*3,HEIGHT*4HEIGHT*3);
surf.assign(WIDTH,HEIGHT,1,3);
ant a;
a.init(WIDTH/2,HEIGHT/2,2);
surf.fill(0);
for(int x=0;x<WIDTH;x++)
for(int y=0;y<HEIGHT;y++)
{
board[x][y]=-1;
}
while(a.unvisitted>0color<TOTAL)
{
a.step();
//renderboard();
}
screen=surf;
while(screen.is_closed()==false)
{
screen.wait();
}
surf.save_bmp("LangtonsRainbow.bmp");
renderboard();
surf.save_bmp("LangtonsBoard.bmp");
return 0;
}