Home | History | Annotate | Line # | Download | only in tetris
tetris.c revision 1.4
      1 /*	$NetBSD: tetris.c,v 1.4 1998/08/10 02:25:45 perry 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));
     68 	void	usage __P((void));
     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 struct shape *curshape;
    125 	register char *keys;
    126 	register int level = 2;
    127 	char key_write[6][10];
    128 	int ch, i, j;
    129 
    130 	keys = "jkl pq";
    131 
    132 	while ((ch = getopt(argc, argv, "k:l:s")) != -1)
    133 		switch(ch) {
    134 		case 'k':
    135 			if (strlen(keys = optarg) != 6)
    136 				usage();
    137 			break;
    138 		case 'l':
    139 			level = atoi(optarg);
    140 			if (level < MINLEVEL || level > MAXLEVEL) {
    141 				(void)fprintf(stderr,
    142 				    "tetris: level must be from %d to %d",
    143 				    MINLEVEL, MAXLEVEL);
    144 				exit(1);
    145 			}
    146 			break;
    147 		case 's':
    148 			showscores(0);
    149 			exit(0);
    150 		case '?':
    151 		default:
    152 			usage();
    153 		}
    154 
    155 	argc -= optind;
    156 	argv += optind;
    157 
    158 	if (argc)
    159 		usage();
    160 
    161 	fallrate = 1000000 / level;
    162 
    163 	for (i = 0; i <= 5; i++) {
    164 		for (j = i+1; j <= 5; j++) {
    165 			if (keys[i] == keys[j]) {
    166 				(void)fprintf(stderr,
    167 				    "%s: Duplicate command keys specified.\n",
    168 				    argv[0]);
    169 				exit (1);
    170 			}
    171 		}
    172 		if (keys[i] == ' ')
    173 			strcpy(key_write[i], "<space>");
    174 		else {
    175 			key_write[i][0] = keys[i];
    176 			key_write[i][1] = '\0';
    177 		}
    178 	}
    179 
    180 	sprintf(key_msg,
    181 "%s - left   %s - rotate   %s - right   %s - drop   %s - pause   %s - quit",
    182 		key_write[0], key_write[1], key_write[2], key_write[3],
    183 		key_write[4], key_write[5]);
    184 
    185 	(void)signal(SIGINT, onintr);
    186 	scr_init();
    187 	setup_board();
    188 
    189 	srandom(getpid());
    190 	scr_set();
    191 
    192 	pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    193 	curshape = randshape();
    194 
    195 	scr_msg(key_msg, 1);
    196 
    197 	for (;;) {
    198 		place(curshape, pos, 1);
    199 		scr_update();
    200 		place(curshape, pos, 0);
    201 		c = tgetchar();
    202 		if (c < 0) {
    203 			/*
    204 			 * Timeout.  Move down if possible.
    205 			 */
    206 			if (fits_in(curshape, pos + B_COLS)) {
    207 				pos += B_COLS;
    208 				continue;
    209 			}
    210 
    211 			/*
    212 			 * Put up the current shape `permanently',
    213 			 * bump score, and elide any full rows.
    214 			 */
    215 			place(curshape, pos, 1);
    216 			score++;
    217 			elide();
    218 
    219 			/*
    220 			 * Choose a new shape.  If it does not fit,
    221 			 * the game is over.
    222 			 */
    223 			curshape = randshape();
    224 			pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    225 			if (!fits_in(curshape, pos))
    226 				break;
    227 			continue;
    228 		}
    229 
    230 		/*
    231 		 * Handle command keys.
    232 		 */
    233 		if (c == keys[5]) {
    234 			/* quit */
    235 			break;
    236 		}
    237 		if (c == keys[4]) {
    238 			static char msg[] =
    239 			    "paused - press RETURN to continue";
    240 
    241 			place(curshape, pos, 1);
    242 			do {
    243 				scr_update();
    244 				scr_msg(key_msg, 0);
    245 				scr_msg(msg, 1);
    246 				(void) fflush(stdout);
    247 			} while (rwait((struct timeval *)NULL) == -1);
    248 			scr_msg(msg, 0);
    249 			scr_msg(key_msg, 1);
    250 			place(curshape, pos, 0);
    251 			continue;
    252 		}
    253 		if (c == keys[0]) {
    254 			/* move left */
    255 			if (fits_in(curshape, pos - 1))
    256 				pos--;
    257 			continue;
    258 		}
    259 		if (c == keys[1]) {
    260 			/* turn */
    261 			struct shape *new = &shapes[curshape->rot];
    262 
    263 			if (fits_in(new, pos))
    264 				curshape = new;
    265 			continue;
    266 		}
    267 		if (c == keys[2]) {
    268 			/* move right */
    269 			if (fits_in(curshape, pos + 1))
    270 				pos++;
    271 			continue;
    272 		}
    273 		if (c == keys[3]) {
    274 			/* move to bottom */
    275 			while (fits_in(curshape, pos + B_COLS)) {
    276 				pos += B_COLS;
    277 				score++;
    278 			}
    279 			continue;
    280 		}
    281 		if (c == '\f')
    282 			scr_clear();
    283 	}
    284 
    285 	scr_clear();
    286 	scr_end();
    287 
    288 	(void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
    289 	    score, score == 1 ? "" : "s", level, score * level);
    290 	savescore(level);
    291 
    292 	printf("\nHit RETURN to see high scores, ^C to skip.\n");
    293 
    294 	while ((i = getchar()) != '\n')
    295 		if (i == EOF)
    296 			break;
    297 
    298 	showscores(level);
    299 
    300 	exit(0);
    301 }
    302 
    303 void
    304 onintr(signo)
    305 	int signo;
    306 {
    307 	scr_clear();
    308 	scr_end();
    309 	exit(0);
    310 }
    311 
    312 void
    313 usage()
    314 {
    315 	(void)fprintf(stderr, "usage: tetris [-s] [-l level] [-keys]\n");
    316 	exit(1);
    317 }
    318