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