Home | History | Annotate | Line # | Download | only in PSD.doc
life.c revision 1.5
      1 .\"	$NetBSD: life.c,v 1.5 1999/07/02 16:11:15 simonb Exp $
      2 .\"
      3 .\" Copyright (c) 1980, 1993
      4 .\"	 The Regents of the University of California.  All rights reserved.
      5 .\"
      6 .\" Redistribution and use in source and binary forms, with or without
      7 .\" modification, are permitted provided that the following conditions
      8 .\" are met:
      9 .\" 1. Redistributions of source code must retain the above copyright
     10 .\"    notice, this list of conditions and the following disclaimer.
     11 .\" 2. Redistributions in binary form must reproduce the above copyright
     12 .\"    notice, this list of conditions and the following disclaimer in the
     13 .\"    documentation and/or other materials provided with the distribution.
     14 .\" 3. All advertising materials mentioning features or use of this software
     15 .\"    must display the following acknowledgement:
     16 .\"	This product includes software developed by the University of
     17 .\"	California, Berkeley and its contributors.
     18 .\" 4. Neither the name of the University nor the names of its contributors
     19 .\"    may be used to endorse or promote products derived from this software
     20 .\"    without specific prior written permission.
     21 .\"
     22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32 .\" SUCH DAMAGE.
     33 .\"
     34 .\"	@(#)life.c	8.1 (Berkeley) 6/8/93
     35 .\"
     36 # include	<curses.h>
     37 # include	<signal.h>
     38 
     39 /*
     40  *	Run a life game.  This is a demonstration program for
     41  * the Screen Updating section of the -lcurses cursor package.
     42  */
     43 
     44 typedef struct lst_st {			/* linked list element */
     45 	int		y, x;		/* (y, x) position of piece */
     46 	struct lst_st	*next, *last;	/* doubly linked */
     47 } LIST;
     48 
     49 LIST	*Head;			/* head of linked list */
     50 
     51 int	die();
     52 
     53 main(ac, av)
     54 int	ac;
     55 char	*av[];
     56 {
     57 	evalargs(ac, av);		/* evaluate arguments */
     58 
     59 	initscr();			/* initialize screen package */
     60 	signal(SIGINT, die);		/* set to restore tty stats */
     61 	cbreak();			/* set for char-by-char */
     62 	noecho();			/*	input */
     63 	nonl();				/* for optimization */
     64 
     65 	getstart();			/* get starting position */
     66 	for (;;) {
     67 		prboard();		/* print out current board */
     68 		update();		/* update board position */
     69 	}
     70 }
     71 
     72 /*
     73  * This is the routine which is called when rubout is hit.
     74  * It resets the tty stats to their original values.  This
     75  * is the normal way of leaving the program.
     76  */
     77 die()
     78 {
     79 	signal(SIGINT, SIG_IGN);		/* ignore rubouts */
     80 	mvcur(0, COLS - 1, LINES - 1, 0);	/* go to bottom of screen */
     81 	endwin();				/* set terminal to good state */
     82 	exit(0);
     83 }
     84 
     85 /*
     86  * Get the starting position from the user.  They keys u, i, o, j, l,
     87  * m, ,, and . are used for moving their relative directions from the
     88  * k key.  Thus, u move diagonally up to the left, , moves directly down,
     89  * etc.  x places a piece at the current position, " " takes it away.
     90  * The input can also be from a file.  The list is built after the
     91  * board setup is ready.
     92  */
     93 getstart()
     94 {
     95 	reg char	c;
     96 	reg int		x, y;
     97 	auto char	buf[100];
     98 
     99 	box(stdscr, '|', '_');		/* box in the screen */
    100 	move(1, 1);			/* move to upper left corner */
    101 
    102 	for (;;) {
    103 		refresh();		/* print current position */
    104 		if ((c = getch()) == 'q')
    105 			break;
    106 		switch (c) {
    107 		  case 'u':
    108 		  case 'i':
    109 		  case 'o':
    110 		  case 'j':
    111 		  case 'l':
    112 		  case 'm':
    113 		  case ',':
    114 		  case '.':
    115 			adjustyx(c);
    116 			break;
    117 		  case 'f':
    118 			mvaddstr(0, 0, "File name: ");
    119 			getstr(buf);
    120 			readfile(buf);
    121 			break;
    122 		  case 'x':
    123 			addch('X');
    124 			break;
    125 		  case ' ':
    126 			addch(' ');
    127 			break;
    128 		}
    129 	}
    130 
    131 	if (Head != NULL)			/* start new list */
    132 		dellist(Head);
    133 	Head = malloc(sizeof (LIST));
    134 
    135 	/*
    136 	 * loop through the screen looking for 'x's, and add a list
    137 	 * element for each one
    138 	 */
    139 	for (y = 1; y < LINES - 1; y++)
    140 		for (x = 1; x < COLS - 1; x++) {
    141 			move(y, x);
    142 			if (inch() == 'x')
    143 				addlist(y, x);
    144 		}
    145 }
    146 
    147 /*
    148  * Print out the current board position from the linked list
    149  */
    150 prboard() {
    151 
    152 	reg LIST	*hp;
    153 
    154 	erase();			/* clear out last position */
    155 	box(stdscr, '|', '_');		/* box in the screen */
    156 
    157 	/*
    158 	 * go through the list adding each piece to the newly
    159 	 * blank board
    160 	 */
    161 	for (hp = Head; hp; hp = hp->next)
    162 		mvaddch(hp->y, hp->x, 'X');
    163 
    164 	refresh();
    165 }
    166