BSD 4_1_snap development
[unix-history] / usr / doc / curses / life.c
CommitLineData
73b86790
KA
1# include <curses.h>
2# include <signal.h>
3
4/*
5 * Run a life game. This is a demonstration program for
6 * the Screen Updating section of the -lcurses cursor package.
7 */
8
9struct lst_st { /* linked list element */
10 int y, x; /* (y, x) position of piece */
11 struct lst_st *next, *last; /* doubly linked */
12};
13
14typedef struct lst_st LIST;
15
16LIST *Head; /* head of linked list */
17
18main(ac, av)
19int ac;
20char *av[]; {
21
22 int die();
23
24 evalargs(ac, av); /* evaluate arguments */
25
26 initscr(); /* initialize screen package */
27 signal(SIGINT, die); /* set to restore tty stats */
28 crmode(); /* set for char-by-char */
29 noecho(); /* input */
30 nonl(); /* for optimization */
31
32 getstart(); /* get starting position */
33 for (;;) {
34 prboard(); /* print out current board */
35 update(); /* update board position */
36 }
37}
38
39/*
40 * This is the routine which is called when rubout is hit.
41 * It resets the tty stats to their original values. This
42 * is the normal way of leaving the program.
43 */
44die() {
45
46 signal(SIGINT, SIG_IGN); /* ignore rubouts */
47 mvcur(0, COLS-1, LINES-1, 0); /* go to bottom of screen */
48 endwin(); /* set terminal to initial state */
49 exit(0);
50}
51
52/*
53 * Get the starting position from the user. They keys u, i, o, j, l,
54 * m, ,, and . are used for moving their relative directions from the
55 * k key. Thus, u move diagonally up to the left, , moves directly down,
56 * etc. x places a piece at the current position, " " takes it away.
57 * The input can also be from a file. The list is built after the
58 * board setup is ready.
59 */
60getstart() {
61
62 reg char c;
63 reg int x, y;
64
65 box(stdscr, '|', '_'); /* box in the screen */
66 move(1, 1); /* move to upper left corner */
67
68 do {
69 refresh(); /* print current position */
70 if ((c=getch()) == 'q')
71 break;
72 switch (c) {
73 case 'u':
74 case 'i':
75 case 'o':
76 case 'j':
77 case 'l':
78 case 'm':
79 case ',':
80 case '.':
81 adjustyx(c);
82 break;
83 case 'f':
84 mvaddstr(0, 0, "File name: ");
85 getstr(buf);
86 readfile(buf);
87 break;
88 case 'x':
89 addch('X');
90 break;
91 case ' ':
92 addch(' ');
93 break;
94 }
95 }
96
97 if (Head != NULL) /* start new list */
98 dellist(Head);
99 Head = malloc(sizeof (LIST));
100
101 /*
102 * loop through the screen looking for 'x's, and add a list
103 * element for each one
104 */
105 for (y = 1; y < LINES - 1; y++)
106 for (x = 1; x < COLS - 1; x++) {
107 move(y, x);
108 if (inch() == 'x')
109 addlist(y, x);
110 }
111}
112
113/*
114 * Print out the current board position from the linked list
115 */
116prboard() {
117
118 reg LIST *hp;
119
120 erase(); /* clear out last position */
121 box(stdscr, '|', '_'); /* box in the screen */
122
123 /*
124 * go through the list adding each piece to the newly
125 * blank board
126 */
127 for (hp = Head; hp; hp = hp->next)
128 mvaddch(hp->y, hp->x, 'X');
129
130 refresh();
131}