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