The game of chaos (source code) for the 3b1

John Campbell jdc at naucse.UUCP
Thu Aug 10 13:58:56 AEST 1989


Ok, what did we decide?  Here is a single shar file that contains the
game of chaos as described in a recent NOVA special.  Am I wrong in
posting to both comp.sys.att and unix-pc.sources?  What should I do
with my next 6 part shar posting?

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file". e.g..  If this archive is complete, you
# will see the following message at the end:
#               "End of archive."
# Contents:  Makefile chaos.c
# Wrapped by jdc at Thunder on Wed Aug  9 21:05:50 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f Makefile -a "${1}" != "-c" ; then
  echo shar: Will not over-write existing file \"Makefile\"
else
echo shar: Extracting \"Makefile\" \(153 characters\)
cat >Makefile <<'END_OF_Makefile'
#  Makefile for the game of chaos--demonstrated on the 8/4/89 NOVA special
#
CFLAGS=-O
chaos:	chaos.o
	ld /lib/crt0s.o /lib/shlib.ifile chaos.o -o chaos
END_OF_Makefile
# end of overwriting check
fi
if test -f chaos.c -a "${1}" != "-c" ; then
  echo shar: Will not over-write existing file \"chaos.c\"
else
echo shar: Extracting \"chaos.c\" \(7151 characters\)
cat >chaos.c <<'END_OF_chaos.c'
#include <stdio.h>
/*
From: John Campbell (...!arizona!naucse!jdc)

This is a quick hack of the chaos game that was presented during the NOVA
special a few days ago (8/4/89).  The chaos game is played as follows:
take a piece of paper and put down three points for the vertices of a
triangle.  Next pick a point anywhere at random on the page.  Now, from
this point randomly pick one of vertices and move half the distance to
that point.  Repeat.

The NOVA special was pretty good I thought.  They explained that a
"chaotic", or turbulent, system was one that had a periodic element
and a non-linear element.  This program demonstrates one of the more
interesting phenomena of chaotic systems--a pattern that distinguishes
a chaotic system from a random system.  Don't expect a better explanation
from me, however, I'm just an arm-chair tourist.

Build with ``make'' and run by typing in ``chaos'' (or ``chaos 4'',
``chaos 5'') and wait for the pattern to stablize.  If the mouse arrow
is no longer flashing then no more new points are being plotted.  The
only way to leave is to hit the interrupt character...
*/

#include <sys/window.h>   /* Started with tam.h--too much trouble. */
#include <sys/signal.h>
#include <errno.h>

typedef unsigned short Scr_type;
typedef unsigned char Scr_kluge;

#define uPC_XMAX 720
#define uPC_YMAX 300

#define uPC_XSIZE       45 /* Short ints. */
#define uPC_YSIZE uPC_YMAX

Scr_type uPC_display[uPC_YSIZE][uPC_XSIZE];

extern errno, sys_nerr;
extern char *sys_errlist[];

#define IfErrOut(e1,e2,s1,s2) if (e1 e2) {\
uPC_fixwind(0);\
fprintf(stderr, "%s:: %s %s\n", sys_errlist[errno], s1, s2);\
exit(-1);}

static struct urdata uPC_ur = {(unsigned short *)uPC_display, 2*uPC_XSIZE, 0, 
                        0, 0, 0, 0, 0, uPC_XMAX, uPC_YMAX, SRCSRC, DSTOR, 0};

#define uPC_flush() IfErrOut (ioctl(0, WIOCRASTOP, &uPC_ur), <0, \
      "ioctl failed", "WIOCRASTOP")

uPC_init()
{
/* This routine will ioctl to change 0 size */
   int i;
   struct uwdata uw;
   int uPC_fixwind();

/* Check that we are on the bitmapped window. */
   if (iswind() != 0) {
      fprintf (stderr, "Sorry--must run from the bitmapped terminal\n");
      exit(-1);
   }
   for (i=1; i<=16; i++) {
         signal (i, uPC_fixwind);
   }

/* Increase the screen size */
   uw.uw_x = 0;
   uw.uw_y = 0;   /* Leave room for top status line. */
   uw.uw_width = uPC_XMAX;      /* 720 */
   uw.uw_height = uPC_YMAX;     /* 288 normal--we clobber 12 (top row)*/
   uw.uw_uflags = 1;         /* Creates with no border */

   IfErrOut (ioctl(0, WIOCSETD, &uw), <0, "ioctl failed on", "WIOCSETD");
}


uPC_clear()
{
/* This routine will clear the uPC_display buffer and window. */
   register Scr_type *j;
   register int i;

   j = (Scr_type *)uPC_display;
   i = uPC_YSIZE*uPC_XSIZE + 1;

   while (--i)
      *j++ = 0;

   uPC_ur.ur_dstop = DSTSRC;   /* replace (clear screen). */
   uPC_flush();
   uPC_ur.ur_dstop = DSTOR;   /* Or in (show text) */
}



#define uPC_PLOT(a,b)   (*(a)|=(b))

uPC_plot(x,y)
unsigned int x,y;
{
/* This routine plots x,y */
   register Scr_type mask, *a;
   static Scr_type lookup[] = {
      0x0001, 0x0002, 0x0004, 0x0008,
      0x0010, 0x0020, 0x0040, 0x0080,
      0x0100, 0x0200, 0x0400, 0x0800,
      0x1000, 0x2000, 0x4000, 0x8000,
   };

   a = &uPC_display[(uPC_YSIZE - 1) - y][x >> 4];
   mask = lookup[x & 0x0f];

   if (!(*(a) & mask)) {
      uPC_PLOT (a, mask);
      uPC_flush();
   }
}


uPC_reset()
{
/* Reset window to normal size. */
   uPC_fixwind (0);
}


uPC_fixwind (signo)
int signo;
{
   static struct uwdata wreset = { 0, 12, 720, 288, 0x1};
   struct utdata ut;
/* Reset the screen size. */
   ioctl (0, WIOCSETD, &wreset); 

/* Clear the title line. */
   ut.ut_text[0] = '\0';
   ut.ut_num =  WTXTSLK1;
   ioctl (0, WIOCSETTEXT, &ut);

/* Turn on the cursor */
   printf ("\033[=0C\n");

/* Position the cursor to the bottom of the screen. */
   printf ("\033[25;1H\n");
   fflush (stdin);

/* Rearm any signals to do normal default tasks. */
   if (signo) {
      if (signo == SIGILL || signo == SIGTRAP || signo == SIGPWR)
         signal (signo, SIG_DFL);
      kill (0,signo);  /* Redo the signal (as if we never trapped it). */
   }
}


#define hdist(l,p) ((l+p)/2)

main(argc, argv)
int argc;
char *argv[];
{
   unsigned int x=221,y=40;
   short rand();
   int type = 3;

   uPC_init();
   uPC_clear();
   srand(x*y);

   if (argc > 2) 
      usage();
   if (argc == 2) 
      type = atoi (argv[1]);

   switch (type) {
   case 3:
      do_triangle(x,y);
   break;
   case 4:
      do_rectangle(x,y);
   break;
   case 5:
      do_pentagon(x,y);
   break;
   default:
      usage();
   }
}

do_triangle(x,y)
unsigned int x,y;
{
   int d;
   for (;;) {
      uPC_plot (x,y); 
      d = rand() & 3;
      switch (d) {
      case 0:   /* Halfway to 186,0 */
         x = hdist (x, 186);
         y = hdist (y, 0);
      break;
         case 1:  /* Halfway to 532,0 */
         x = hdist (x, 532);
         y = hdist (y, 0);
      break;
      case 2:  /* Halfway to 359,299 */
         x = hdist (x, 359);
         y = hdist (y, 299);
      break;
      case 3:
         continue;
      break;
      default:
         uPC_reset();
         fprintf (stderr, "Illegal mask!\n");
         exit (1);
      }
#ifdef DEBUG
      if (x < 0 || x > 720) {
         uPC_reset();
         fprintf (stderr, "X: %d\n", x);
         exit(1);
      }
      if (y < 0 || y > 300) {
         uPC_reset();
         fprintf (stderr, "Y: %d\n", y);
         exit(1);
      }
#endif
   }
}

do_rectangle(x,y)
unsigned int x,y;
{
   int d;
   for (;;) {
      uPC_plot (x,y);
      d = rand() & 3;
      switch (d) {
      case 0:   /* Halfway to 186,0 */
         x = hdist (x, 186);
         y = hdist (y, 0);
      break;
         case 1:  /* Halfway to 532,0 */
         x = hdist (x, 532);
         y = hdist (y, 0);
      break;
      case 2:  /* Halfway to 186,299 */
         x = hdist (x, 186);
         y = hdist (y, 299);
      break;
      case 3:  /* Halfway to 523,299 */
         x = hdist (x, 523);
         y = hdist (y, 299);
      break;
      default:
         uPC_reset();
         fprintf (stderr, "Illegal mask!\n");
      }
   }
}


do_pentagon(x,y)
unsigned int x,y;
{
   int d;

   x = 321;
   y = 140;

   for (;;) {
      uPC_plot (x,y); 
      d = rand() & 7;
      switch (d) {
      case 0:   /* Halfway to 262,0 */
      case 5:
         x = hdist (x, 262);
         y = hdist (y, 0);
      break;
      case 1:  /* Halfway to 456,0 */
         x = hdist (x, 456);
         y = hdist (y, 0);
      break;
      case 2:  /* Halfway to 516,185 */
      case 6:
         x = hdist (x, 516);
         y = hdist (y, 185);
      break;
      case 3:  /* Halfway to 360,300 */
         x = hdist (x, 360);
         y = hdist (y, 300);
      break;
      case 4:  /* Halfway to 202,185 */
      case 7:
         x = hdist (x, 202);
         y = hdist (y, 185);
      break;
      default:
         uPC_reset();
         fprintf (stderr, "Illegal mask!\n");
         exit (1);
      }
   }
}

usage()
{
   fprintf (stderr, "chaos [3-4]\n");
   uPC_fixwind (0);
   exit(1);
}
END_OF_chaos.c
# end of overwriting check
fi
echo "shar: End of archive"
##	End of shell archive
exit 0

-------
	John Campbell               ...!arizona!naucse!jdc
                                    CAMPBELL at NAUVAX.bitnet
	unix?  Sure send me a dozen, all different colors.
-- 
	John Campbell               ...!arizona!naucse!jdc
                                    CAMPBELL at NAUVAX.bitnet
	unix?  Sure send me a dozen, all different colors.



More information about the Unix-pc.sources mailing list