tetris.c revision 1.7 1 /* $NetBSD: tetris.c,v 1.7 1999/01/03 17:13:51 hubertf Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)tetris.c 8.1 (Berkeley) 5/31/93
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
44 The Regents of the University of California. All rights reserved.\n");
45 #endif /* not lint */
46
47 /*
48 * Tetris (or however it is spelled).
49 */
50
51 #include <sys/time.h>
52
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include "input.h"
60 #include "scores.h"
61 #include "screen.h"
62 #include "tetris.h"
63
64 static void elide __P((void));
65 static void setup_board __P((void));
66 int main __P((int, char **));
67 void onintr __P((int)) __attribute__((__noreturn__));
68 void usage __P((void)) __attribute__((__noreturn__));
69
70 /*
71 * Set up the initial board. The bottom display row is completely set,
72 * along with another (hidden) row underneath that. Also, the left and
73 * right edges are set.
74 */
75 static void
76 setup_board()
77 {
78 register int i;
79 register cell *p;
80
81 p = board;
82 for (i = B_SIZE; i; i--)
83 #ifndef mips
84 *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
85 #else /* work around compiler bug */
86 *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2 ? 1 : 0;
87 #endif
88 }
89
90 /*
91 * Elide any full active rows.
92 */
93 static void
94 elide()
95 {
96 register int i, j, base;
97 register cell *p;
98
99 for (i = A_FIRST; i < A_LAST; i++) {
100 base = i * B_COLS + 1;
101 p = &board[base];
102 for (j = B_COLS - 2; *p++ != 0;) {
103 if (--j <= 0) {
104 /* this row is to be elided */
105 memset(&board[base], 0, B_COLS - 2);
106 scr_update();
107 tsleep();
108 while (--base != 0)
109 board[base + B_COLS] = board[base];
110 scr_update();
111 tsleep();
112 break;
113 }
114 }
115 }
116 }
117
118 int
119 main(argc, argv)
120 int argc;
121 char *argv[];
122 {
123 register int pos, c;
124 register char *keys;
125 register int level = 2;
126 char key_write[6][10];
127 int ch, i, j;
128
129 keys = "jkl pq";
130
131 while ((ch = getopt(argc, argv, "k:l:ps")) != -1)
132 switch(ch) {
133 case 'k':
134 if (strlen(keys = optarg) != 6)
135 usage();
136 break;
137 case 'l':
138 level = atoi(optarg);
139 if (level < MINLEVEL || level > MAXLEVEL) {
140 (void)fprintf(stderr,
141 "tetris: level must be from %d to %d",
142 MINLEVEL, MAXLEVEL);
143 exit(1);
144 }
145 break;
146 case 'p':
147 showpreview = 1;
148 break;
149 case 's':
150 showscores(0);
151 exit(0);
152 case '?':
153 default:
154 usage();
155 }
156
157 argc -= optind;
158 argv += optind;
159
160 if (argc)
161 usage();
162
163 fallrate = 1000000 / level;
164
165 for (i = 0; i <= 5; i++) {
166 for (j = i+1; j <= 5; j++) {
167 if (keys[i] == keys[j]) {
168 (void)fprintf(stderr,
169 "%s: Duplicate command keys specified.\n",
170 argv[0]);
171 exit (1);
172 }
173 }
174 if (keys[i] == ' ')
175 strcpy(key_write[i], "<space>");
176 else {
177 key_write[i][0] = keys[i];
178 key_write[i][1] = '\0';
179 }
180 }
181
182 sprintf(key_msg,
183 "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
184 key_write[0], key_write[1], key_write[2], key_write[3],
185 key_write[4], key_write[5]);
186
187 (void)signal(SIGINT, onintr);
188 scr_init();
189 setup_board();
190
191 srandom(getpid());
192 scr_set();
193
194 pos = A_FIRST*B_COLS + (B_COLS/2)-1;
195 nextshape = randshape();
196 curshape = randshape();
197
198 scr_msg(key_msg, 1);
199
200 for (;;) {
201 place(curshape, pos, 1);
202 scr_update();
203 place(curshape, pos, 0);
204 c = tgetchar();
205 if (c < 0) {
206 /*
207 * Timeout. Move down if possible.
208 */
209 if (fits_in(curshape, pos + B_COLS)) {
210 pos += B_COLS;
211 continue;
212 }
213
214 /*
215 * Put up the current shape `permanently',
216 * bump score, and elide any full rows.
217 */
218 place(curshape, pos, 1);
219 score++;
220 elide();
221
222 /*
223 * Choose a new shape. If it does not fit,
224 * the game is over.
225 */
226 curshape = nextshape;
227 nextshape = randshape();
228 pos = A_FIRST*B_COLS + (B_COLS/2)-1;
229 if (!fits_in(curshape, pos))
230 break;
231 continue;
232 }
233
234 /*
235 * Handle command keys.
236 */
237 if (c == keys[5]) {
238 /* quit */
239 break;
240 }
241 if (c == keys[4]) {
242 static char msg[] =
243 "paused - press RETURN to continue";
244
245 place(curshape, pos, 1);
246 do {
247 scr_update();
248 scr_msg(key_msg, 0);
249 scr_msg(msg, 1);
250 (void) fflush(stdout);
251 } while (rwait((struct timeval *)NULL) == -1);
252 scr_msg(msg, 0);
253 scr_msg(key_msg, 1);
254 place(curshape, pos, 0);
255 continue;
256 }
257 if (c == keys[0]) {
258 /* move left */
259 if (fits_in(curshape, pos - 1))
260 pos--;
261 continue;
262 }
263 if (c == keys[1]) {
264 /* turn */
265 struct shape *new = &shapes[curshape->rot];
266
267 if (fits_in(new, pos))
268 curshape = new;
269 continue;
270 }
271 if (c == keys[2]) {
272 /* move right */
273 if (fits_in(curshape, pos + 1))
274 pos++;
275 continue;
276 }
277 if (c == keys[3]) {
278 /* move to bottom */
279 while (fits_in(curshape, pos + B_COLS)) {
280 pos += B_COLS;
281 score++;
282 }
283 continue;
284 }
285 if (c == '\f') {
286 scr_clear();
287 scr_msg(key_msg, 1);
288 }
289 }
290
291 scr_clear();
292 scr_end();
293
294 (void)printf("Your score: %d point%s x level %d = %d\n",
295 score, score == 1 ? "" : "s", level, score * level);
296 savescore(level);
297
298 printf("\nHit RETURN to see high scores, ^C to skip.\n");
299
300 while ((i = getchar()) != '\n')
301 if (i == EOF)
302 break;
303
304 showscores(level);
305
306 exit(0);
307 }
308
309 void
310 onintr(signo)
311 int signo;
312 {
313 scr_clear();
314 scr_end();
315 exit(0);
316 }
317
318 void
319 usage()
320 {
321 (void)fprintf(stderr, "usage: tetris [-ps] [-l level] [-keys]\n");
322 exit(1);
323 }
324