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