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