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