1 .\" $NetBSD
: life
.c
,v
1.5 1999/07/02 16:11:15 simonb Exp $
3 .\" Copyright (c
) 1980, 1993
4 .\" The Regents of the University of California
. All rights reserved
.
6 .\" Redistribution
and use in source
and binary forms
, with
or without
7 .\" modification
, are permitted provided that the following conditions
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
.
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
30 .\" @
(#)life.c 8.1 (Berkeley) 6/8/93
36 * Run a life game. This is a demonstration program for
37 * the Screen Updating section of the -lcurses cursor package.
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 */
45 LIST
*Head
; /* head of linked list */
53 evalargs(ac
, av
); /* evaluate arguments */
55 initscr(); /* initialize screen package */
56 signal(SIGINT
, die
); /* set to restore tty stats */
57 cbreak(); /* set for char-by-char */
59 nonl(); /* for optimization */
61 getstart(); /* get starting position */
63 prboard(); /* print out current board */
64 update(); /* update board position */
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.
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 */
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.
95 box(stdscr
, '|', '_'); /* box in the screen */
96 move(1, 1); /* move to upper left corner */
99 refresh(); /* print current position */
100 if ((c
= getch()) == 'q')
114 mvaddstr(0, 0, "File name: ");
127 if (Head
!= NULL
) /* start new list */
129 Head
= malloc(sizeof (LIST
));
132 * loop through the screen looking for 'x's, and add a list
133 * element for each one
135 for (y
= 1; y
< LINES
- 1; y
++)
136 for (x
= 1; x
< COLS
- 1; x
++) {
144 * Print out the current board position from the linked list
150 erase(); /* clear out last position */
151 box(stdscr
, '|', '_'); /* box in the screen */
154 * go through the list adding each piece to the newly
157 for (hp
= Head
; hp
; hp
= hp
->next
)
158 mvaddch(hp
->y
, hp
->x
, 'X');