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