Skip to content

Instantly share code, notes, and snippets.

@andrewrcollins
Created January 5, 2012 03:29
Show Gist options
  • Save andrewrcollins/1563568 to your computer and use it in GitHub Desktop.
Save andrewrcollins/1563568 to your computer and use it in GitHub Desktop.
#TJHSST ~ Wolfram's One-Dimensional Cellular Automata
/*
Wolfram One-Dimensional Cellular Automata
Fast
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "graphics.h"
#include "conio.h"
#define MAXLEN 320
/* Define the neighorhood -2 -1 0 1 2 */
#define LOWER (-2)
#define UPPER 2
/* 2,4,5 and 2,4 are good ones */
int rules[10]= { 0,0,1,0,1,1,0,0,0,0 };
main()
{
int old[MAXLEN],new[MAXLEN];
int time,quit;
graphics();
do {
for(time=0;time<MAXLEN;time++)
new[time]=old[time]=random(time+1)%2;
time=0;
do {
display(old,time);
process(old,new);
time++;
display(new,time);
process(new,old);
time++;
} while(!kbhit()&&time!=200);
if(kbhit())
quit=(getch()==(char)'q');
else
quit=0;
cleardevice();
} while(!quit);
}
int neighbors(data,position)
int data[MAXLEN];
int position;
{
static int oldcnt,farleft;
if(position==0) {
int cnt;
oldcnt=0;
for(cnt=LOWER;cnt<=UPPER;cnt++)
oldcnt+=data[(cnt+MAXLEN)%MAXLEN];
}
else {
oldcnt-=farleft;
oldcnt+=data[(position+UPPER)%MAXLEN];
}
farleft=data[(position+LOWER+MAXLEN)%MAXLEN];
return oldcnt;
}
int process(old,new)
int old[MAXLEN],new[MAXLEN];
{
int position;
for(position=0;position<MAXLEN;position++)
new[position]=rules[neighbors(old,position)];
return 0;
}
int display(data,timestep)
int data[MAXLEN],timestep;
{
int position;
for(position=0;position<MAXLEN;position++) {
putpixel(position,(190-timestep)%190,data[position]%3+1);
putpixel(position,195,data[position]%3+1);
}
return 0;
}
int graphics()
{
int gdriver,gmode;
gdriver=CGA;
gmode=CGA;
initgraph(&gdriver,&gmode,"");
randomize();
}
/*
Wolfram One-Dimensional Cellular Automata
Slow
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "graphics.h"
#include "conio.h"
#define MAXLEN 320
/* Define the neighorhood -2 -1 0 1 2 */
#define LOWER (-2)
#define UPPER 2
/* 2,4,5 and 2,4 are good ones */
int rules[10]= { 0,0,1,0,1,1,0,0,0,0 };
main()
{
int old[MAXLEN],new[MAXLEN];
int time,quit;
graphics();
do {
for(time=0;time<MAXLEN;time++)
new[time]=old[time]=random(time+1)%2;
time=0;
do {
display(old,time);
process(old,new);
time++;
display(new,time);
process(new,old);
time++;
} while(!kbhit()&&time!=200);
if(kbhit())
quit=(getch()==(char)'q');
else
quit=0;
cleardevice();
} while(!quit);
}
int neighbors(data,position)
int data[MAXLEN];
int position;
{
/*
static int oldcnt,farleft;
if(position==0) {
int cnt;
oldcnt=0;
for(cnt=LOWER;cnt<=UPPER;cnt++)
oldcnt+=data[(cnt+MAXLEN)%MAXLEN];
}
else {
oldcnt-=farleft;
oldcnt+=data[(position+UPPER)%MAXLEN];
}
farleft=data[(position+LOWER+MAXLEN)%MAXLEN];
return oldcnt;
*/
int cnt=0,cnt2;
for(cnt2=LOWER;cnt2<=UPPER;cnt2++)
cnt+=data[(position+cnt2+MAXLEN)%MAXLEN];
return cnt;
}
int process(old,new)
int old[MAXLEN],new[MAXLEN];
{
int position;
for(position=0;position<MAXLEN;position++)
new[position]=rules[neighbors(old,position)];
return 0;
}
int display(data,timestep)
int data[MAXLEN],timestep;
{
int position;
for(position=0;position<MAXLEN;position++)
putpixel(position,200-timestep,data[position]%3+1);
return 0;
}
int graphics()
{
int gdriver,gmode;
gdriver=CGA;
gmode=CGA;
initgraph(&gdriver,&gmode,"");
randomize();
}
@andrewrcollins
Copy link
Author

Two C programs found on an old 5.25 floppy disk from when I was a nerd at Thomas Jefferson High School for Science and Technology between 1988 and 1992.

These program implement Wolfram's one-dimensional cellular automata.

http://en.wikipedia.org/wiki/Elementary_cellular_automaton

Anyone can do whatever they'd like to with this program--if anything.

I am now a two-dimensional nerd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment