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