4.1cBSD/usr/doc/curses/life.gr

'-F
.ds =F life.c
.ds =M Nov  7 19:03 1980
'wh 0 vH
'wh -1i vF
\*(+K#\*(-K \*(+Kinclude\*(-K\h'|21n'<curses\&.h>
\*(+K#\*(-K \*(+Kinclude\*(-K\h'|21n'<signal\&.h>

\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C

 \fI*\fP\h'|11n'Run a life game\&.  This is a demonstration program \*(+Kfor\*(-K
 \fI*\fP the Screen Updating section of the \*-lcurses cursor package\&.
 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C


\*(+Kstruct\*(-K lst\*_st \*(+K{\*(-K\h'|41n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 linked list element \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'\*(+Kint\*(-K\h'|31n'y, x;\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 (y, x) position of piece \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'\*(+Kstruct\*(-K lst\*_st\h'|31n'\fI*\fPnext, \fI*\fPlast;\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 doubly linked \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\*(+K}\*(-K;

\*(+Ktypedef\*(-K \*(+Kstruct\*(-K lst\*_st\h'|31n'LIST;

LIST\h'|11n'\fI*\fPHead;\h'|41n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 head of linked list \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C


'FN main
main(ac, av)
\*(+Kint\*(-K\h'|11n'ac;
\*(+Kchar\*(-K\h'|11n'\fI*\fPav[]; \*(+K{\*(-K

\h'|11n'\*(+Kint\*(-K\h'|21n'die();

\h'|11n'evalargs(ac, av);\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 evaluate arguments \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C


\h'|11n'initscr();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 initialize screen package \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'signal(SIGINT, die);\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 set to restore tty stats \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'crmode();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 set for char\*-by\*-char \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'noecho();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
\h'|61n'input \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'nonl();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 for optimization \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C


\h'|11n'getstart();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 get starting position \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'\*(+Kfor\*(-K (;;) \*(+K{\*(-K
\h'|21n'prboard();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 print out current board \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|21n'update();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 update board position \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'\*(+K}\*(-K
\*(+K}\*(-K\c\c
'-F


\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C

 \fI*\fP This is the routine which is called when rubout is hit\&.
 \fI*\fP It resets the tty stats to their original values\&.  This
 \fI*\fP is the normal way of leaving the program\&.
 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

'FN die
die() \*(+K{\*(-K

\h'|11n'signal(SIGINT, SIG\*_IGN);\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 ignore rubouts \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'mvcur(0, COLS\*-1, LINES\*-1, 0);\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 go to bottom of screen \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'endwin();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 set terminal to initial state \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'exit(0);
\*(+K}\*(-K\c\c
'-F


\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C

 \fI*\fP Get the starting position from the user\&.  They keys u, i, o, j, l,
 \fI*\fP m, ,, and \&. are used \*(+Kfor\*(-K moving their relative directions from the
 \fI*\fP k key\&.  Thus, u move diagonally up to the left, , moves directly down,
 \fI*\fP etc\&.  x places a piece at the current position, " " takes it away\&.
 \fI*\fP The input can also be from a file\&.  The list is built after the
 \fI*\fP board setup is ready\&.
 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

'FN getstart
getstart() \*(+K{\*(-K

\h'|11n'reg \*(+Kchar\*(-K\h'|31n'c;
\h'|11n'reg \*(+Kint\*(-K\h'|31n'x, y;

\h'|11n'box(stdscr, \'|\', \'\*_\');\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 box in the screen \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'move(1, 1);\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 move to upper left corner \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C


\h'|11n'\*(+Kdo\*(-K \*(+K{\*(-K
\h'|21n'refresh();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 print current position \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|21n'\*(+Kif\*(-K ((c=getch()) == \'q\')
\h'|31n'\*(+Kbreak\*(-K;
\h'|21n'\*(+Kswitch\*(-K (c) \*(+K{\*(-K
\h'|21n'  \*(+Kcase\*(-K \'u\':
\h'|21n'  \*(+Kcase\*(-K \'i\':
\h'|21n'  \*(+Kcase\*(-K \'o\':
\h'|21n'  \*(+Kcase\*(-K \'j\':
\h'|21n'  \*(+Kcase\*(-K \'l\':
\h'|21n'  \*(+Kcase\*(-K \'m\':
\h'|21n'  \*(+Kcase\*(-K \',\':
\h'|21n'  \*(+Kcase\*(-K \'\&.\':
\h'|31n'adjustyx(c);
\h'|31n'\*(+Kbreak\*(-K;
\h'|21n'  \*(+Kcase\*(-K \'f\':
\h'|31n'mvaddstr(0, 0, "File name: ");
\h'|31n'getstr(buf);
\h'|31n'readfile(buf);
\h'|31n'\*(+Kbreak\*(-K;
\h'|21n'  \*(+Kcase\*(-K \'x\':
\h'|31n'addch(\'X\');
\h'|31n'\*(+Kbreak\*(-K;
\h'|21n'  \*(+Kcase\*(-K \' \':
\h'|31n'addch(\' \');
\h'|31n'\*(+Kbreak\*(-K;
\h'|21n'\*(+K}\*(-K
\h'|11n'\*(+K}\*(-K

\h'|11n'\*(+Kif\*(-K (Head != NULL)\h'|61n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 start new list \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|21n'dellist(Head);
\h'|11n'Head = malloc(\*(+Ksizeof\*(-K (LIST)); 

\h'|11n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C

\h'|11n' \fI*\fP loop through the screen looking \*(+Kfor\*(-K \'x\'s, and add a list
\h'|11n' \fI*\fP element \*(+Kfor\*(-K each one
\h'|11n' \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'\*(+Kfor\*(-K (y = 1; y < LINES \*- 1; y++)
\h'|21n'\*(+Kfor\*(-K (x = 1; x < COLS \*- 1; x++) \*(+K{\*(-K
\h'|31n'move(y, x);
\h'|31n'\*(+Kif\*(-K (inch() == \'x\')
\h'|41n'addlist(y, x);
\h'|21n'\*(+K}\*(-K
\*(+K}\*(-K\c\c
'-F


\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C

 \fI*\fP Print out the current board position from the linked list
 \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

'FN prboard
prboard() \*(+K{\*(-K

\h'|11n'reg LIST\h'|31n'\fI*\fPhp;

\h'|11n'erase();\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 clear out last position \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'box(stdscr, \'|\', \'\*_\');\h'|51n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C
 box in the screen \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C


\h'|11n'\fI\h'\w' 'u-\w'/'u'/\fP\fI*\fP\c\c
'+C

\h'|11n' \fI*\fP go through the list adding each piece to the newly
\h'|11n' \fI*\fP blank board
\h'|11n' \fI*\fP\fI\h'\w' 'u-\w'/'u'/\fP\c
'-C

\h'|11n'\*(+Kfor\*(-K (hp = Head; hp; hp = hp\*->next)
\h'|21n'mvaddch(hp\*->y, hp\*->x, \'X\');

\h'|11n'refresh();
\*(+K}\*(-K\c\c
'-F