// PROGRAM ca1 // one-dimensional Boolean cellular automata #include "TrueBASIC.h" int main(); void setrule(int update[]); void initial(int site[], int *L, int *tmax); void iterate(int site[], int L, int update[], int tmax); #define MAXWIDTH 1600 int main() { int L, site[MAXWIDTH], tmax, update[8], Itmp1_; for(Itmp1_ = 0; Itmp1_ < MAXWIDTH; ++Itmp1_) site[Itmp1_] = 0; GWopen(0); setrule(update); initial(site, &L, &tmax); iterate(site, L, update, tmax); GWquit(); return 0; } void setrule(int update[]) { int bit0, bit1, bit2, i, rule = 90; char Stmp1_[_LBUFF_]; if(GWinput("rule number = ", Stmp1_, _LBUFF_)) sscanf(Stmp1_, "%d", &rule); printf("rule number = %d\n", rule); for(i = 7; i >= 0; --i) { update[i] = (rule >> i); // find binary representation rule = rule - (update[i] << i); bit2 = i/4; bit1 = (i - 4*bit2)/2; bit0 = i - 4*bit2 - 2*bit1; // show possible neighborhoods printf("%1d %1d %1d ", bit2, bit1, bit0); printf(" "); } printf("\n"); for(i = 7; i >= 0; --i) { printf(" %2d ", update[i]); // print rules printf(" "); } printf("\n"); } void initial(int site[], int *L, int *tmax) { int px, py; GWcolor(BLACK, CLR_BACKGRND); GWsize(1, &px, &py); GWindow(0.0f, (float)(py-1), (float)(px-1), 0.0f); (*L) = px/4; (*tmax) = py/4; site[(*L)/2] = 1; // center site GWsrect((float)(4*(*L)/2), 0.0f, (float)(4*(*L)/2+4), 4.0f, YELLOW); // each site 4 x 4 pixels } void iterate(int site[], int L, int update[], int tmax) { int i, index, sitenew[MAXWIDTH], t, Itmp1_; // update lattice // need to introduce additional array, sitenew, to temporarily // store values of newly updated sites for(t = 1; t <= tmax; ++t) { for(i = 1; i <= L; ++i) { index = 4*site[i - 1] + 2*site[i] + site[i + 1]; sitenew[i] = update[index]; if(sitenew[i] == 1) GWsrect((float)(4*i), (float)(4*t), (float)(4*i+4), (float)(4*t+4), YELLOW); } for(Itmp1_ = 0; Itmp1_ < MAXWIDTH; ++Itmp1_) site[Itmp1_] = sitenew[Itmp1_]; site[0] = site[L]; // periodic boundary conditions site[L + 1] = site[1]; } }