home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / curses-1.4.lha / Curses-1.4 / examples / odds / worms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-30  |  5.9 KB  |  269 lines

  1. /*
  2.  
  3.      @@@        @@@    @@@@@@@@@@     @@@@@@@@@@@    @@@@@@@@@@@@
  4.      @@@        @@@   @@@@@@@@@@@@    @@@@@@@@@@@@   @@@@@@@@@@@@@
  5.      @@@        @@@  @@@@      @@@@   @@@@           @@@@ @@@  @@@@
  6.      @@@   @@   @@@  @@@        @@@   @@@            @@@  @@@   @@@
  7.      @@@  @@@@  @@@  @@@        @@@   @@@            @@@  @@@   @@@
  8.      @@@@ @@@@ @@@@  @@@        @@@   @@@            @@@  @@@   @@@
  9.       @@@@@@@@@@@@   @@@@      @@@@   @@@            @@@  @@@   @@@
  10.        @@@@  @@@@     @@@@@@@@@@@@    @@@            @@@  @@@   @@@
  11.         @@    @@       @@@@@@@@@@     @@@            @@@  @@@   @@@
  12.  
  13.                  Eric P. Scott
  14.               Caltech High Energy Physics
  15.                  October, 1980
  16.  
  17. */
  18. #define MINICURSES
  19. #include <curses.h>
  20. #include <signal.h>
  21. #define cursor(col,row) move(row,col)
  22. int Wrap;
  23. short *ref[128];
  24. static char flavor[]={
  25.     'O', '*', '#', '$', '%', '0'
  26. };
  27. static short xinc[]={
  28.      1,  1,  1,  0, -1, -1, -1,  0
  29. }, yinc[]={
  30.     -1,  0,  1,  1,  1,  0, -1, -1
  31. };
  32. static struct worm {
  33.     int orientation, head;
  34.     short *xpos, *ypos;
  35. } worm[40];
  36. static char *field;
  37. static int length=16, number=3, trail=' ';
  38. static struct options {
  39.     int nopts;
  40.     int opts[3];
  41. } normal[8]={
  42.     { 3, { 7, 0, 1 } },
  43.     { 3, { 0, 1, 2 } },
  44.     { 3, { 1, 2, 3 } },
  45.     { 3, { 2, 3, 4 } },
  46.     { 3, { 3, 4, 5 } },
  47.     { 3, { 4, 5, 6 } },
  48.     { 3, { 5, 6, 7 } },
  49.     { 3, { 6, 7, 0 } }
  50. }, upper[8]={
  51.     { 1, { 1, 0, 0 } },
  52.     { 2, { 1, 2, 0 } },
  53.     { 0, { 0, 0, 0 } },
  54.     { 0, { 0, 0, 0 } },
  55.     { 0, { 0, 0, 0 } },
  56.     { 2, { 4, 5, 0 } },
  57.     { 1, { 5, 0, 0 } },
  58.     { 2, { 1, 5, 0 } }
  59. }, left[8]={
  60.     { 0, { 0, 0, 0 } },
  61.     { 0, { 0, 0, 0 } },
  62.     { 0, { 0, 0, 0 } },
  63.     { 2, { 2, 3, 0 } },
  64.     { 1, { 3, 0, 0 } },
  65.     { 2, { 3, 7, 0 } },
  66.     { 1, { 7, 0, 0 } },
  67.     { 2, { 7, 0, 0 } }
  68. }, right[8]={
  69.     { 1, { 7, 0, 0 } },
  70.     { 2, { 3, 7, 0 } },
  71.     { 1, { 3, 0, 0 } },
  72.     { 2, { 3, 4, 0 } },
  73.     { 0, { 0, 0, 0 } },
  74.     { 0, { 0, 0, 0 } },
  75.     { 0, { 0, 0, 0 } },
  76.     { 2, { 6, 7, 0 } }
  77. }, lower[8]={
  78.     { 0, { 0, 0, 0 } },
  79.     { 2, { 0, 1, 0 } },
  80.     { 1, { 1, 0, 0 } },
  81.     { 2, { 1, 5, 0 } },
  82.     { 1, { 5, 0, 0 } },
  83.     { 2, { 5, 6, 0 } },
  84.     { 0, { 0, 0, 0 } },
  85.     { 0, { 0, 0, 0 } }
  86. }, upleft[8]={
  87.     { 0, { 0, 0, 0 } },
  88.     { 0, { 0, 0, 0 } },
  89.     { 0, { 0, 0, 0 } },
  90.     { 0, { 0, 0, 0 } },
  91.     { 0, { 0, 0, 0 } },
  92.     { 1, { 3, 0, 0 } },
  93.     { 2, { 1, 3, 0 } },
  94.     { 1, { 1, 0, 0 } }
  95. }, upright[8]={
  96.     { 2, { 3, 5, 0 } },
  97.     { 1, { 3, 0, 0 } },
  98.     { 0, { 0, 0, 0 } },
  99.     { 0, { 0, 0, 0 } },
  100.     { 0, { 0, 0, 0 } },
  101.     { 0, { 0, 0, 0 } },
  102.     { 0, { 0, 0, 0 } },
  103.     { 1, { 5, 0, 0 } }
  104. }, lowleft[8]={
  105.     { 3, { 7, 0, 1 } },
  106.     { 0, { 0, 0, 0 } },
  107.     { 0, { 0, 0, 0 } },
  108.     { 1, { 1, 0, 0 } },
  109.     { 2, { 1, 7, 0 } },
  110.     { 1, { 7, 0, 0 } },
  111.     { 0, { 0, 0, 0 } },
  112.     { 0, { 0, 0, 0 } }
  113. }, lowright[8]={
  114.     { 0, { 0, 0, 0 } },
  115.     { 1, { 7, 0, 0 } },
  116.     { 2, { 5, 7, 0 } },
  117.     { 1, { 5, 0, 0 } },
  118.     { 0, { 0, 0, 0 } },
  119.     { 0, { 0, 0, 0 } },
  120.     { 0, { 0, 0, 0 } },
  121.     { 0, { 0, 0, 0 } }
  122. };
  123. main(argc,argv)
  124. int argc;
  125. char *argv[];
  126. {
  127.     char *malloc();
  128.     int onsig();
  129.     float ranf();
  130.     register int x, y;
  131.     register int n;
  132.     register struct worm *w;
  133.     register struct options *op;
  134.     register int h;
  135.     register short *ip;
  136.     int last, bottom;
  137.  
  138.     setbuf(stdout,malloc(BUFSIZ));
  139.  
  140.     for (x=1;x<argc;x++) {
  141.     register char *p;
  142.     p=argv[x];
  143.     if (*p=='-') p++;
  144.     switch (*p) {
  145.     case 'f':
  146.         field="WORM";
  147.         break;
  148.     case 'l':
  149.         if (++x==argc) goto usage;
  150.         if ((length=atoi(argv[x]))<2||length>1024) {
  151.         fprintf(stderr,"%s: Invalid length\n",*argv);
  152.         exit(1);
  153.         }
  154.         break;
  155.     case 'n':
  156.         if (++x==argc) goto usage;
  157.         if ((number=atoi(argv[x]))<1||number>40) {
  158.         fprintf(stderr,"%s: Invalid number of worms\n",*argv);
  159.         exit(1);
  160.         }
  161.         break;
  162.     case 't':
  163.         trail='.';
  164.         break;
  165.     default:
  166.     usage:
  167.         fprintf(stderr,
  168.         "usage: %s [-field] [-length #] [-number #] [-trail]\n",*argv);
  169.         exit(1);
  170.         break;
  171.     }
  172.     }
  173.  
  174.     signal(SIGINT, onsig);
  175.     initscr();
  176.     bottom = LINES-1;
  177.     last = COLS-1;
  178.  
  179.     ip=(short *)malloc(LINES*COLS*sizeof (short));
  180.  
  181.     for (n=0;n<LINES;) {
  182.     ref[n++]=ip; ip+=COLS;
  183.     }
  184.     for (ip=ref[0],n=LINES*COLS;--n>=0;) *ip++=0;
  185.     ref[bottom][last]=1;
  186.     for (n=number, w= &worm[0];--n>=0;w++) {
  187.     w->orientation=w->head=0;
  188.     if (!(ip=(short *)malloc(length*sizeof (short)))) {
  189.         fprintf(stderr,"%s: out of memory\n",*argv);
  190.         exit(1);
  191.     }
  192.     w->xpos=ip;
  193.     for (x=length;--x>=0;) *ip++ = -1;
  194.     if (!(ip=(short *)malloc(length*sizeof (short)))) {
  195.         fprintf(stderr,"%s: out of memory\n",*argv);
  196.         exit(1);
  197.     }
  198.     w->ypos=ip;
  199.     for (y=length;--y>=0;) *ip++ = -1;
  200.     }
  201.     if (field) {
  202.     register char *p;
  203.     p=field;
  204.     for (y=bottom;--y>=0;) {
  205.         for (x=COLS;--x>=0;) {
  206.         addch(*p++);
  207.         if (!*p) p=field;
  208.         }
  209.             addch('\n');
  210.         }
  211.     }
  212.     refresh();
  213.  
  214.     for (;;) {
  215.     for (n=0,w= &worm[0];n<number;n++,w++) {
  216.         if ((x=w->xpos[h=w->head])<0) {
  217.         cursor(x=w->xpos[h]=0,y=w->ypos[h]=bottom);
  218.         addch(flavor[n%6]);
  219.         ref[y][x]++;
  220.         }
  221.         else y=w->ypos[h];
  222.         if (++h==length) h=0;
  223.         if (w->xpos[w->head=h]>=0) {
  224.         register int x1, y1;
  225.         x1=w->xpos[h]; y1=w->ypos[h];
  226.         if (--ref[y1][x1]==0) {
  227.             cursor(x1,y1); addch(trail);
  228.         }
  229.         }
  230.             op= &(x==0 ? (y==0 ? upleft : (y==bottom ? lowleft : left)) :
  231.                 (x==last ? (y==0 ? upright : (y==bottom ? lowright : right)) :
  232.         (y==0 ? upper : (y==bottom ? lower : normal))))[w->orientation];
  233.         switch (op->nopts) {
  234.         case 0:
  235.         refresh();
  236.         endwin();
  237.         abort();
  238.         return;
  239.         case 1:
  240.         w->orientation=op->opts[0];
  241.         break;
  242.         default:
  243.         w->orientation=op->opts[(int)(ranf()*(float)op->nopts)];
  244.         }
  245.         cursor(x+=xinc[w->orientation], y+=yinc[w->orientation]);
  246.         if (!Wrap||x!=last||y!=bottom) addch(flavor[n%6]);
  247.         ref[w->ypos[h]=y][w->xpos[h]=x]++;
  248.     }
  249.     refresh();
  250.     }
  251. }
  252.  
  253. onsig()
  254. {
  255.     endwin();
  256.     exit(0);
  257. }
  258.  
  259. float
  260. ranf()
  261. {
  262.     float rv;
  263.     long r = rand();
  264.  
  265.     r &= 077777;
  266.     rv =((float)r/32767.);
  267.     return rv;
  268. }
  269.