back to the House Of Dave

Fun Programs

None of these are original! They're all off the net. The Othello player was a finalist, I believe, in one of the obfuscated C programming contests (try a net search on that subject). It particularly annoyed me, as I'm a decent Othello player, and it beats me handily if I set its depth to 4 or 5, or higher.

1) A program that calculates pi to 800 decimal places:

int a=10000,b,c=2800,d,e,f[2801],g; main() { for(;b-c;) f[b++]=a/5; for(;d=0,g=c*2;c -=14,printf("%.4d",e+d/a),e=d%a) for(b=c; d+=f[b]*a,f[b]=d%--g,d/=g--,--b; d*=b) ; }

2) A program that does the same for e:

int digits[1000], i, d, carry;

main() { for(i = 1; i < 1000; i++) digits[i] = 1; digits[1] = 2; while (1) { putchar(digits[1] + '0'); digits[1] = 0; carry = 0; for(i = 999; i > 1; i--) { d = digits[i] * 10 + carry; digits[i] = d % i; carry = d / i; } digits[1] = carry; } }

3) Othello player:

#define D define

#D Y return

#D R for

#D e while

#D I printf

#D l int

#D W if

#D C y=v+111;H(x,v)*y++= *x

#D H(a,b)R(a=b+11;a<b+89;a++)

#D s(a)t=scanf("%d",&a)

#D U Z I #D Z I("123\ 45678\n");H(x,V){putchar(".XO"[*x]);W((x-V)%10==8){x+=2;I("%d\n",(x-V)/10-1);}} l V[1600],u,r[]={-1,-11,-10,-9,1,11,10,9},h[]={11,18,81,88},ih[]={22,27,72,77}, bz,lv=60,*x,*y,m,t;S(d,v,f,_,a,b)l*v;{l c=0,*n=v+100,j=d<u-1?a:-9000,w,z,i,g,q= 3-f;W(d>u){R(w=i=0;i<4;i++)w+=(m=v[h[i]])==f?300:m==q?-300:(t=v[ih[i]])==f?-50: t==q?50:0;Y w;}H(z,0){W(E(v,z,f,100)){c++;w= -S(d+1,n,q,0,-b,-j);W(w>j){g=bz=z; j=w;W(w>=b||w>=8003)Y w;}}}W(!c){g=0;W(_){H(x,v)c+= *x==f?1:*x==3-f?-1:0;Y c>0? 8000+c:c-8000;}C;j= -S(d+1,n,q,1,-b,-j);}bz=g;Y d>=u-1?j+(c<<3):j;}main(){R(;t< 1600;t+=100)R(m=0;m<100;m++)V[t+m]=m<11||m>88||(m+1)%10<2?3:0;I("Level:");V[44] =V[55]=1;V[45]=V[54]=2;s(u);e(lv>0){Z do{I("You:");s(m);}e(!E(V,m,2,0)&&m!=99); W(m!=99)lv--;W(lv<15&&u<10)u+=2;U("Wait\n");I("Value:%d\n",S(0,V,1,0,-9000,9000 ));I("move: %d\n",(lv-=E(V,bz,1,0),bz));}}E(v,z,f,o)l*v;{l*j,q=3-f,g=0,i,w,*k=v +z;W(*k==0)R(i=7;i>=0;i--){j=k+(w=r[i]);e(*j==q)j+=w;W(*j==f&&j-w!=k){W(!g){g=1 ;C;}e(j!=k)*((j-=w)+o)=f;}}Y g;}

4) Sentence.c, a program that finds self-referential sentences.

#include <stdio.h> #define MAX_NUM 70 char * nums[] = { "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" , "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" , "twenty" , "twenty one" , "twenty two" , "twenty three" , "twenty four" , "twenty five" , "twenty six" , "twenty seven" , "twenty eight" , "twenty nine" , "thirty" , "thirty one" , "thirty two" , "thirty three" , "thirty four" , "thirty five" , "thirty six" , "thirty seven" , "thirty eight" , "thirty nine" , "forty" , "forty one" , "forty two" , "forty three" , "forty four" , "forty five" , "forty six" , "forty seven" , "forty eight" , "forty nine" , "fifty" , "fifty one" , "fifty two" , "fifty three" , "fifty four" , "fifty five" , "fifty six" , "fifty seven" , "fifty eight" , "fifty nine" , "sixty" , "sixty one" , "sixty two" , "sixty three" , "sixty four" , "sixty five" , "sixty six" , "sixty seven" , "sixty eight" , "sixty nine" , "seventy" };

make_sentence(char * buf, int * counts) { int i; int c; int mark_it;

c = sprintf(buf, "See if you can write a different sentence which contains ");

for (i=0; i<26; i++) { if (counts[i] < 0) { counts[i] = 0-counts[i]; mark_it = 1; } else mark_it = 0;

if (counts[i] > MAX_NUM) { printf("ooops! count[%d] = %d\n",i,counts[i]); exit(-1); }

if (!mark_it) c += sprintf(buf+c, "%s %c", nums[counts[i]], 'a'+i); else c += sprintf(buf+c, "%s %c", nums[counts[i]], 'A'+i);

if (counts[i] != 1) c+= sprintf(buf+c, "'s");

if (i<25) c+=sprintf(buf+c, ", ");

if (i==24) c+=sprintf(buf+c, "and "); }

c += sprintf(buf+c, "."); }

int count(char* buf, int* counts) { int ret = 0; int i; int p; char c; int newcounts[26];

for (i=0; i<26; i++) { newcounts[i] = 0; } p=0;

while (c = buf[p]) { if (isalpha(c)) { c = tolower(c); newcounts[c-'a']++; } p++; }

for(i=0; i<26; i++) { if (counts[i] != newcounts[i]) { counts[i] = 0-newcounts[i]; ret++; } } return ret; }

void randomize_count(int * c) { int i; for (i=0; i<26; i++) { c[i] = rand() % MAX_NUM; } }

main() { char cur_sent[1024]; int c[26]; int i; int diff; long sent_count=0; int mod;

srand(time(NULL));

for (i=0; i<26; i++) c[i] = 1;

make_sentence(cur_sent, c); sent_count++; printf("%s\n\n", cur_sent);

while(diff = count(cur_sent, c)) { make_sentence(cur_sent, c); sent_count++;

if ((sent_count % 10000)==1) { printf("%ld %d: %s\n\n", sent_count, diff, cur_sent); }

if ((sent_count % 25) == 0) { randomize_count(c); make_sentence(cur_sent, c); } } printf("Final: %ld %d: %s\n\n", sent_count, diff, cur_sent); }