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