Home | History | Annotate | Line # | Download | only in tetris
tetris.c revision 1.13
      1 /*	$NetBSD: tetris.c,v 1.13 2000/01/01 10:15:17 jsm 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 <fcntl.h>
     54 #include <signal.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 
     60 #include "input.h"
     61 #include "scores.h"
     62 #include "screen.h"
     63 #include "tetris.h"
     64 
     65 cell	board[B_SIZE];		/* 1 => occupied, 0 => empty */
     66 
     67 int	Rows, Cols;		/* current screen size */
     68 
     69 const struct shape *curshape;
     70 const struct shape *nextshape;
     71 
     72 long	fallrate;		/* less than 1 million; smaller => faster */
     73 
     74 int	score;			/* the obvious thing */
     75 gid_t	gid, egid;
     76 
     77 char	key_msg[100];
     78 int	showpreview;
     79 
     80 static	void	elide __P((void));
     81 static	void	setup_board __P((void));
     82 	int	main __P((int, char **));
     83 	void	onintr __P((int)) __attribute__((__noreturn__));
     84 	void	usage __P((void)) __attribute__((__noreturn__));
     85 
     86 /*
     87  * Set up the initial board.  The bottom display row is completely set,
     88  * along with another (hidden) row underneath that.  Also, the left and
     89  * right edges are set.
     90  */
     91 static void
     92 setup_board()
     93 {
     94 	register int i;
     95 	register cell *p;
     96 
     97 	p = board;
     98 	for (i = B_SIZE; i; i--)
     99 		*p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
    100 }
    101 
    102 /*
    103  * Elide any full active rows.
    104  */
    105 static void
    106 elide()
    107 {
    108 	register int i, j, base;
    109 	register cell *p;
    110 
    111 	for (i = A_FIRST; i < A_LAST; i++) {
    112 		base = i * B_COLS + 1;
    113 		p = &board[base];
    114 		for (j = B_COLS - 2; *p++ != 0;) {
    115 			if (--j <= 0) {
    116 				/* this row is to be elided */
    117 				memset(&board[base], 0, B_COLS - 2);
    118 				scr_update();
    119 				tsleep();
    120 				while (--base != 0)
    121 					board[base + B_COLS] = board[base];
    122 				scr_update();
    123 				tsleep();
    124 				break;
    125 			}
    126 		}
    127 	}
    128 }
    129 
    130 int
    131 main(argc, argv)
    132 	int argc;
    133 	char *argv[];
    134 {
    135 	register int pos, c;
    136 	register const char *keys;
    137 	register int level = 2;
    138 	char key_write[6][10];
    139 	int ch, i, j;
    140 	int fd;
    141 
    142 	gid = getgid();
    143 	egid = getegid();
    144 	setegid(gid);
    145 
    146 	fd = open("/dev/null", O_RDONLY);
    147 	if (fd < 3)
    148 		exit(1);
    149 	close(fd);
    150 
    151 	keys = "jkl pq";
    152 
    153 	while ((ch = getopt(argc, argv, "k:l:ps")) != -1)
    154 		switch(ch) {
    155 		case 'k':
    156 			if (strlen(keys = optarg) != 6)
    157 				usage();
    158 			break;
    159 		case 'l':
    160 			level = atoi(optarg);
    161 			if (level < MINLEVEL || level > MAXLEVEL) {
    162 				(void)fprintf(stderr,
    163 				    "tetris: level must be from %d to %d\n",
    164 				    MINLEVEL, MAXLEVEL);
    165 				exit(1);
    166 			}
    167 			break;
    168 		case 'p':
    169 			showpreview = 1;
    170 			break;
    171 		case 's':
    172 			showscores(0);
    173 			exit(0);
    174 		case '?':
    175 		default:
    176 			usage();
    177 		}
    178 
    179 	argc -= optind;
    180 	argv += optind;
    181 
    182 	if (argc)
    183 		usage();
    184 
    185 	fallrate = 1000000 / level;
    186 
    187 	for (i = 0; i <= 5; i++) {
    188 		for (j = i+1; j <= 5; j++) {
    189 			if (keys[i] == keys[j]) {
    190 				(void)fprintf(stderr,
    191 				    "%s: duplicate command keys specified.\n",
    192 				    argv[0]);
    193 				exit (1);
    194 			}
    195 		}
    196 		if (keys[i] == ' ')
    197 			strcpy(key_write[i], "<space>");
    198 		else {
    199 			key_write[i][0] = keys[i];
    200 			key_write[i][1] = '\0';
    201 		}
    202 	}
    203 
    204 	sprintf(key_msg,
    205 "%s - left   %s - rotate   %s - right   %s - drop   %s - pause   %s - quit",
    206 		key_write[0], key_write[1], key_write[2], key_write[3],
    207 		key_write[4], key_write[5]);
    208 
    209 	(void)signal(SIGINT, onintr);
    210 	scr_init();
    211 	setup_board();
    212 
    213 	srandom(getpid());
    214 	scr_set();
    215 
    216 	pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    217 	nextshape = randshape();
    218 	curshape = randshape();
    219 
    220 	scr_msg(key_msg, 1);
    221 
    222 	for (;;) {
    223 		place(curshape, pos, 1);
    224 		scr_update();
    225 		place(curshape, pos, 0);
    226 		c = tgetchar();
    227 		if (c < 0) {
    228 			/*
    229 			 * Timeout.  Move down if possible.
    230 			 */
    231 			if (fits_in(curshape, pos + B_COLS)) {
    232 				pos += B_COLS;
    233 				continue;
    234 			}
    235 
    236 			/*
    237 			 * Put up the current shape `permanently',
    238 			 * bump score, and elide any full rows.
    239 			 */
    240 			place(curshape, pos, 1);
    241 			score++;
    242 			elide();
    243 
    244 			/*
    245 			 * Choose a new shape.  If it does not fit,
    246 			 * the game is over.
    247 			 */
    248 			curshape = nextshape;
    249 			nextshape = randshape();
    250 			pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    251 			if (!fits_in(curshape, pos))
    252 				break;
    253 			continue;
    254 		}
    255 
    256 		/*
    257 		 * Handle command keys.
    258 		 */
    259 		if (c == keys[5]) {
    260 			/* quit */
    261 			break;
    262 		}
    263 		if (c == keys[4]) {
    264 			static char msg[] =
    265 			    "paused - press RETURN to continue";
    266 
    267 			place(curshape, pos, 1);
    268 			do {
    269 				scr_update();
    270 				scr_msg(key_msg, 0);
    271 				scr_msg(msg, 1);
    272 				(void) fflush(stdout);
    273 			} while (rwait((struct timeval *)NULL) == -1);
    274 			scr_msg(msg, 0);
    275 			scr_msg(key_msg, 1);
    276 			place(curshape, pos, 0);
    277 			continue;
    278 		}
    279 		if (c == keys[0]) {
    280 			/* move left */
    281 			if (fits_in(curshape, pos - 1))
    282 				pos--;
    283 			continue;
    284 		}
    285 		if (c == keys[1]) {
    286 			/* turn */
    287 			const struct shape *new = &shapes[curshape->rot];
    288 
    289 			if (fits_in(new, pos))
    290 				curshape = new;
    291 			continue;
    292 		}
    293 		if (c == keys[2]) {
    294 			/* move right */
    295 			if (fits_in(curshape, pos + 1))
    296 				pos++;
    297 			continue;
    298 		}
    299 		if (c == keys[3]) {
    300 			/* move to bottom */
    301 			while (fits_in(curshape, pos + B_COLS)) {
    302 				pos += B_COLS;
    303 				score++;
    304 			}
    305 			continue;
    306 		}
    307 		if (c == '\f') {
    308 			scr_clear();
    309 			scr_msg(key_msg, 1);
    310 		}
    311 	}
    312 
    313 	scr_clear();
    314 	scr_end();
    315 
    316 	(void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
    317 	    score, score == 1 ? "" : "s", level, score * level);
    318 	savescore(level);
    319 
    320 	printf("\nHit RETURN to see high scores, ^C to skip.\n");
    321 
    322 	while ((i = getchar()) != '\n')
    323 		if (i == EOF)
    324 			break;
    325 
    326 	showscores(level);
    327 
    328 	exit(0);
    329 }
    330 
    331 void
    332 onintr(signo)
    333 	int signo __attribute__((__unused__));
    334 {
    335 	scr_clear();
    336 	scr_end();
    337 	exit(0);
    338 }
    339 
    340 void
    341 usage()
    342 {
    343 	(void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n");
    344 	exit(1);
    345 }
    346