| 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 | .\" from: @(#)life.c 6.3 (Berkeley) 4/17/91 |
| 33 | .\" life.c,v 1.2 1993/08/01 18:23:17 mycroft Exp |
| 34 | .\" |
| 35 | # include <curses.h> |
| 36 | # include <signal.h> |
| 37 | |
| 38 | /* |
| 39 | * Run a life game. This is a demonstration program for |
| 40 | * the Screen Updating section of the -lcurses cursor package. |
| 41 | */ |
| 42 | |
| 43 | typedef struct lst_st { /* linked list element */ |
| 44 | int y, x; /* (y, x) position of piece */ |
| 45 | struct lst_st *next, *last; /* doubly linked */ |
| 46 | } LIST; |
| 47 | |
| 48 | LIST *Head; /* head of linked list */ |
| 49 | |
| 50 | int die(); |
| 51 | |
| 52 | main(ac, av) |
| 53 | int ac; |
| 54 | char *av[]; |
| 55 | { |
| 56 | evalargs(ac, av); /* evaluate arguments */ |
| 57 | |
| 58 | initscr(); /* initialize screen package */ |
| 59 | signal(SIGINT, die); /* set to restore tty stats */ |
| 60 | cbreak(); /* set for char-by-char */ |
| 61 | noecho(); /* input */ |
| 62 | nonl(); /* for optimization */ |
| 63 | |
| 64 | getstart(); /* get starting position */ |
| 65 | for (;;) { |
| 66 | prboard(); /* print out current board */ |
| 67 | update(); /* update board position */ |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * This is the routine which is called when rubout is hit. |
| 73 | * It resets the tty stats to their original values. This |
| 74 | * is the normal way of leaving the program. |
| 75 | */ |
| 76 | die() |
| 77 | { |
| 78 | signal(SIGINT, SIG_IGN); /* ignore rubouts */ |
| 79 | mvcur(0, COLS - 1, LINES - 1, 0); /* go to bottom of screen */ |
| 80 | endwin(); /* set terminal to good state */ |
| 81 | exit(0); |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Get the starting position from the user. They keys u, i, o, j, l, |
| 86 | * m, ,, and . are used for moving their relative directions from the |
| 87 | * k key. Thus, u move diagonally up to the left, , moves directly down, |
| 88 | * etc. x places a piece at the current position, " " takes it away. |
| 89 | * The input can also be from a file. The list is built after the |
| 90 | * board setup is ready. |
| 91 | */ |
| 92 | getstart() |
| 93 | { |
| 94 | reg char c; |
| 95 | reg int x, y; |
| 96 | auto char buf[100]; |
| 97 | |
| 98 | box(stdscr, '|', '_'); /* box in the screen */ |
| 99 | move(1, 1); /* move to upper left corner */ |
| 100 | |
| 101 | for (;;) { |
| 102 | refresh(); /* print current position */ |
| 103 | if ((c = getch()) == 'q') |
| 104 | break; |
| 105 | switch (c) { |
| 106 | case 'u': |
| 107 | case 'i': |
| 108 | case 'o': |
| 109 | case 'j': |
| 110 | case 'l': |
| 111 | case 'm': |
| 112 | case ',': |
| 113 | case '.': |
| 114 | adjustyx(c); |
| 115 | break; |
| 116 | case 'f': |
| 117 | mvaddstr(0, 0, "File name: "); |
| 118 | getstr(buf); |
| 119 | readfile(buf); |
| 120 | break; |
| 121 | case 'x': |
| 122 | addch('X'); |
| 123 | break; |
| 124 | case ' ': |
| 125 | addch(' '); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (Head != NULL) /* start new list */ |
| 131 | dellist(Head); |
| 132 | Head = malloc(sizeof (LIST)); |
| 133 | |
| 134 | /* |
| 135 | * loop through the screen looking for 'x's, and add a list |
| 136 | * element for each one |
| 137 | */ |
| 138 | for (y = 1; y < LINES - 1; y++) |
| 139 | for (x = 1; x < COLS - 1; x++) { |
| 140 | move(y, x); |
| 141 | if (inch() == 'x') |
| 142 | addlist(y, x); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Print out the current board position from the linked list |
| 148 | */ |
| 149 | prboard() { |
| 150 | |
| 151 | reg LIST *hp; |
| 152 | |
| 153 | erase(); /* clear out last position */ |
| 154 | box(stdscr, '|', '_'); /* box in the screen */ |
| 155 | |
| 156 | /* |
| 157 | * go through the list adding each piece to the newly |
| 158 | * blank board |
| 159 | */ |
| 160 | for (hp = Head; hp; hp = hp->next) |
| 161 | mvaddch(hp->y, hp->x, 'X'); |
| 162 | |
| 163 | refresh(); |
| 164 | } |