CA.CPP
//
//+------------------------------------------------------------+
//+ Program Cellular_Automata (AUTOMATA.CPP)                   +
//+ By Ramiro Perez {RPEREZ@UTPVM1.BITNET}                     +
//+ and Fausto A. A. Barbuto {BJ06@C53000.PETROBRAS.ANRJ.BR}.  +
//+ C++ 3.1 programme's creator: Fausto A. A. Barbuto, 1993.   +
//+ After a TURBO BASIC program by Ramiro Perez, 1993.         +
//+ TRIANGLE and digital pattern added by Fausto Barbuto, 1993.+
//+                                                            +
//+ Requires VGA screen with 640 x 480 points, 16 colours.     +
//+ C++ and TURBO BASIC are trademarks of Borland Int., Inc.   +
//+------------------------------------------------------------+
//
#include 
#include 
#include 
#include 
#include 
 
void Create_Cellular(int, int, int [53][53]);
 
void main()
 
{
   int i, j, k, u, t, a[53][53], b[53][53], c[53][53], poly[8];
   int ipal[15] = {45,5,33,1,9,11,19,26,22,54,38,36,32,4,8};
   int nx, ny, a1, b1, irand1, irand2, option;
 
   int graphdriver=DETECT, graphmode;
 
   clrscr();
   printf("\n Cellular Automata of four sides");
   printf("\n By Ramiro Perez & Fausto A. A. Barbuto, 1993");
   printf("\n States = 12");
   printf("\n Select initial pattern");
   printf("\n\n 1 - CIRCLES");
   printf("\n 2 - SQUARES");
   printf("\n 3 - CENTRAL POINT");
   printf("\n 4 - RAMDOM POINTS");
   printf("\n 5 - TRIANGLE");
   printf("\n <1 or >5 - SURPRISE!!!");
   printf("\n (Press any key to stop the execution)");
   printf("\n\n");
   scanf("%d",&option);
 
   clrscr();
   t = 210;
   u = 70;
 
   initgraph(&graphdriver, &graphmode, "c:\\borlandc\\bgi");
   cleardevice();
 
//
// Initialization of vectors a[][], b[][], c[][]
//
   for (i=0;i<=52;i++) {
      for (j=0;j<=52;j++) {
         a[i][j] = 0;
         b[i][j] = 0;
         c[i][j] = 0;
      }
   }
 
   for (i=0;i<=14;i++) {
     setpalette(i,ipal[i]);
   }
   setbkcolor(3);
 
   k = 0;
//
// The initial pattern in defined below.
//
   switch(option) {
      case 1:  /* Circle */
         for (i=12;i>=1;i--) {
            k++;
            setcolor(k);
            circle(26,26,i);
            setfillstyle(SOLID_FILL,k);
            floodfill(26,26,k);
         }
         break;
      case 2: /* Square */
         for (i=12;i>=1;i--) {
            k++;
            setcolor(k);
            rectangle (26-i, 26+i, 26+i, 26-i);
         }
         break;
      case 3: /* Single Central Point */
         putpixel(26,26,12);
         break;
      case 4: /* Ramdomized Points */
         randomize();
         for (i=1;i<=12;i++) {
            irand1 = (int)(1.4e-3*rand());
            irand2 = (int)(1.4e-3*rand());
            putpixel(irand1,irand2,i);
         }
         break;
      case 5: /* Triangle */
         for (i=12;i>=1;i--) {
            k++;
            setcolor(k);
            poly[0] = 26-i;
            poly[1] = poly[0];
            poly[2] = 26+i;
            poly[3] = poly[0];
            poly[4] = 26;
            poly[5] = poly[2];
            poly[6] = poly[0];
            poly[7] = poly[1];
            drawpoly(4,poly);
            setfillstyle(SOLID_FILL,k);
         }
         break;
      default: /* Surprise! */
         printf("\n %d",option);
         break;
   }
 
   for (nx=0;nx<=52;nx++) {
      for (ny=0;ny<=52;ny++) {
         k = getpixel(nx,ny);
         a[nx][ny] = k;
         c[nx][ny] = k;
      }
   }
 
   Create_Cellular(t,u,c);     /* 1st call of Create_Cellular */
 
   do {
     for (i=1;i<=51;i++) {
        for (j=1;j<=51;j++) {
           a1 = a[i][j-1] + a[i][j+1] + a[i-1][j] + a[i+1][j] + a[i][j];
           b[i][j] = a1 % 13;
           c[i][j] = b[i][j];
        }
     }
 
     Create_Cellular(t,u,c);   /* 2nd call of Create_Cellular */
 
     for (i=1;i<=51;i++) {
        for (j=1;j<=51;j++) {
           b1 = b[i][j-1] + b[i][j+1] + b[i-1][j] + b[i+1][j] + b[i][j];
           a[i][j] = b1 % 13;
           c[i][j] = a[i][j];
        }
     }
 
     Create_Cellular(t,u,c);   /* 3rd call of Create_Cellular */
 
   } while (!kbhit());         /* Escape */
//
// Sound effects and clean-up.
//
   sound(740);
   delay(600);
   nosound();
   closegraph();
}
 
void Create_Cellular(int t, int u, int c[53][53])
 
{
   int i, k, nx, nx1, nx2, ny, ny1, ny2, cx, cy, kcolorx, kcolory;
 
   setcolor(4);
   for (nx=0;nx<=51;nx++) {
      for (ny=0;ny<=51;ny++) {
         k = c[nx][ny];
         nx1 = 4*nx;
         ny1 = 4*ny;
         if (k !=0) {
           for (i=1;i<=k;i++) {
              nx2 = nx1 - i + t;
              ny2 = ny1 - i + u;
              setcolor(14);
              line (nx2, ny2+3, nx2+3, ny2+3);
              setcolor(13);
              line (nx2+3, ny2, nx2+3, ny2+3);
           }
           setcolor(k);
           cx = nx1-k+t+1;
           cy = ny1-k+u+1;
           rectangle (nx1-k+t, ny1-k+u, nx1+3-k+t, ny1+3-k+u);
           kcolorx = getpixel(nx1-k+t,ny1-k+u);
           setfillstyle(SOLID_FILL,k);
           floodfill(cx,cy,kcolorx);
         }
         else {
           setcolor(1);
           cx = nx1+t+1;
           cy = ny1+u+1;
           rectangle (nx1+t, ny1+u, nx1+3+t, ny1+3+u);
           kcolorx = getpixel(nx1-k+t,ny1-k+u);
           setfillstyle(SOLID_FILL,1);
           floodfill(cx,cy,kcolorx);
         }
      }
   }
   return;
}