Home | History | Annotate | Line # | Download | only in tetris
tetris.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1992, 1993
      3  1.1  cgd  *	The Regents of the University of California.  All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Chris Torek and Darren F. Provine.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  *
     36  1.1  cgd  *	@(#)tetris.c	8.1 (Berkeley) 5/31/93
     37  1.1  cgd  */
     38  1.1  cgd 
     39  1.1  cgd #ifndef lint
     40  1.1  cgd static char copyright[] =
     41  1.1  cgd "@(#) Copyright (c) 1992, 1993\n\
     42  1.1  cgd 	The Regents of the University of California.  All rights reserved.\n";
     43  1.1  cgd #endif /* not lint */
     44  1.1  cgd 
     45  1.1  cgd /*
     46  1.1  cgd  * Tetris (or however it is spelled).
     47  1.1  cgd  */
     48  1.1  cgd 
     49  1.1  cgd #include <sys/time.h>
     50  1.1  cgd 
     51  1.1  cgd #include <signal.h>
     52  1.1  cgd #include <stdio.h>
     53  1.1  cgd #include <stdlib.h>
     54  1.1  cgd #include <string.h>
     55  1.1  cgd #include <unistd.h>
     56  1.1  cgd 
     57  1.1  cgd #include "input.h"
     58  1.1  cgd #include "scores.h"
     59  1.1  cgd #include "screen.h"
     60  1.1  cgd #include "tetris.h"
     61  1.1  cgd 
     62  1.1  cgd void onintr __P((int));
     63  1.1  cgd void usage __P((void));
     64  1.1  cgd 
     65  1.1  cgd /*
     66  1.1  cgd  * Set up the initial board.  The bottom display row is completely set,
     67  1.1  cgd  * along with another (hidden) row underneath that.  Also, the left and
     68  1.1  cgd  * right edges are set.
     69  1.1  cgd  */
     70  1.1  cgd static void
     71  1.1  cgd setup_board()
     72  1.1  cgd {
     73  1.1  cgd 	register int i;
     74  1.1  cgd 	register cell *p;
     75  1.1  cgd 
     76  1.1  cgd 	p = board;
     77  1.1  cgd 	for (i = B_SIZE; i; i--)
     78  1.1  cgd #ifndef mips
     79  1.1  cgd 		*p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
     80  1.1  cgd #else /* work around compiler bug */
     81  1.1  cgd 		*p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2 ? 1 : 0;
     82  1.1  cgd #endif
     83  1.1  cgd }
     84  1.1  cgd 
     85  1.1  cgd /*
     86  1.1  cgd  * Elide any full active rows.
     87  1.1  cgd  */
     88  1.1  cgd static void
     89  1.1  cgd elide()
     90  1.1  cgd {
     91  1.1  cgd 	register int i, j, base;
     92  1.1  cgd 	register cell *p;
     93  1.1  cgd 
     94  1.1  cgd 	for (i = A_FIRST; i < A_LAST; i++) {
     95  1.1  cgd 		base = i * B_COLS + 1;
     96  1.1  cgd 		p = &board[base];
     97  1.1  cgd 		for (j = B_COLS - 2; *p++ != 0;) {
     98  1.1  cgd 			if (--j <= 0) {
     99  1.1  cgd 				/* this row is to be elided */
    100  1.1  cgd 				bzero(&board[base], B_COLS - 2);
    101  1.1  cgd 				scr_update();
    102  1.1  cgd 				tsleep();
    103  1.1  cgd 				while (--base != 0)
    104  1.1  cgd 					board[base + B_COLS] = board[base];
    105  1.1  cgd 				scr_update();
    106  1.1  cgd 				tsleep();
    107  1.1  cgd 				break;
    108  1.1  cgd 			}
    109  1.1  cgd 		}
    110  1.1  cgd 	}
    111  1.1  cgd }
    112  1.1  cgd 
    113  1.1  cgd int
    114  1.1  cgd main(argc, argv)
    115  1.1  cgd 	int argc;
    116  1.1  cgd 	char *argv[];
    117  1.1  cgd {
    118  1.1  cgd 	register int pos, c;
    119  1.1  cgd 	register struct shape *curshape;
    120  1.1  cgd 	register char *keys;
    121  1.1  cgd 	register int level = 2;
    122  1.1  cgd 	char key_write[6][10];
    123  1.1  cgd 	int ch, i, j;
    124  1.1  cgd 
    125  1.1  cgd 	keys = "jkl pq";
    126  1.1  cgd 
    127  1.1  cgd 	while ((ch = getopt(argc, argv, "k:l:s")) != EOF)
    128  1.1  cgd 		switch(ch) {
    129  1.1  cgd 		case 'k':
    130  1.1  cgd 			if (strlen(keys = optarg) != 6)
    131  1.1  cgd 				usage();
    132  1.1  cgd 			break;
    133  1.1  cgd 		case 'l':
    134  1.1  cgd 			level = atoi(optarg);
    135  1.1  cgd 			if (level < MINLEVEL || level > MAXLEVEL) {
    136  1.1  cgd 				(void)fprintf(stderr,
    137  1.1  cgd 				    "tetris: level must be from %d to %d",
    138  1.1  cgd 				    MINLEVEL, MAXLEVEL);
    139  1.1  cgd 				exit(1);
    140  1.1  cgd 			}
    141  1.1  cgd 			break;
    142  1.1  cgd 		case 's':
    143  1.1  cgd 			showscores(0);
    144  1.1  cgd 			exit(0);
    145  1.1  cgd 		case '?':
    146  1.1  cgd 		default:
    147  1.1  cgd 			usage();
    148  1.1  cgd 		}
    149  1.1  cgd 
    150  1.1  cgd 	argc -= optind;
    151  1.1  cgd 	argv += optind;
    152  1.1  cgd 
    153  1.1  cgd 	if (argc)
    154  1.1  cgd 		usage();
    155  1.1  cgd 
    156  1.1  cgd 	fallrate = 1000000 / level;
    157  1.1  cgd 
    158  1.1  cgd 	for (i = 0; i <= 5; i++) {
    159  1.1  cgd 		for (j = i+1; j <= 5; j++) {
    160  1.1  cgd 			if (keys[i] == keys[j]) {
    161  1.1  cgd 				(void)fprintf(stderr,
    162  1.1  cgd 				    "%s: Duplicate command keys specified.\n",
    163  1.1  cgd 				    argv[0]);
    164  1.1  cgd 				exit (1);
    165  1.1  cgd 			}
    166  1.1  cgd 		}
    167  1.1  cgd 		if (keys[i] == ' ')
    168  1.1  cgd 			strcpy(key_write[i], "<space>");
    169  1.1  cgd 		else {
    170  1.1  cgd 			key_write[i][0] = keys[i];
    171  1.1  cgd 			key_write[i][1] = '\0';
    172  1.1  cgd 		}
    173  1.1  cgd 	}
    174  1.1  cgd 
    175  1.1  cgd 	sprintf(key_msg,
    176  1.1  cgd "%s - left   %s - rotate   %s - right   %s - drop   %s - pause   %s - quit",
    177  1.1  cgd 		key_write[0], key_write[1], key_write[2], key_write[3],
    178  1.1  cgd 		key_write[4], key_write[5]);
    179  1.1  cgd 
    180  1.1  cgd 	(void)signal(SIGINT, onintr);
    181  1.1  cgd 	scr_init();
    182  1.1  cgd 	setup_board();
    183  1.1  cgd 
    184  1.1  cgd 	srandom(getpid());
    185  1.1  cgd 	scr_set();
    186  1.1  cgd 
    187  1.1  cgd 	pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    188  1.1  cgd 	curshape = randshape();
    189  1.1  cgd 
    190  1.1  cgd 	scr_msg(key_msg, 1);
    191  1.1  cgd 
    192  1.1  cgd 	for (;;) {
    193  1.1  cgd 		place(curshape, pos, 1);
    194  1.1  cgd 		scr_update();
    195  1.1  cgd 		place(curshape, pos, 0);
    196  1.1  cgd 		c = tgetchar();
    197  1.1  cgd 		if (c < 0) {
    198  1.1  cgd 			/*
    199  1.1  cgd 			 * Timeout.  Move down if possible.
    200  1.1  cgd 			 */
    201  1.1  cgd 			if (fits_in(curshape, pos + B_COLS)) {
    202  1.1  cgd 				pos += B_COLS;
    203  1.1  cgd 				continue;
    204  1.1  cgd 			}
    205  1.1  cgd 
    206  1.1  cgd 			/*
    207  1.1  cgd 			 * Put up the current shape `permanently',
    208  1.1  cgd 			 * bump score, and elide any full rows.
    209  1.1  cgd 			 */
    210  1.1  cgd 			place(curshape, pos, 1);
    211  1.1  cgd 			score++;
    212  1.1  cgd 			elide();
    213  1.1  cgd 
    214  1.1  cgd 			/*
    215  1.1  cgd 			 * Choose a new shape.  If it does not fit,
    216  1.1  cgd 			 * the game is over.
    217  1.1  cgd 			 */
    218  1.1  cgd 			curshape = randshape();
    219  1.1  cgd 			pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    220  1.1  cgd 			if (!fits_in(curshape, pos))
    221  1.1  cgd 				break;
    222  1.1  cgd 			continue;
    223  1.1  cgd 		}
    224  1.1  cgd 
    225  1.1  cgd 		/*
    226  1.1  cgd 		 * Handle command keys.
    227  1.1  cgd 		 */
    228  1.1  cgd 		if (c == keys[5]) {
    229  1.1  cgd 			/* quit */
    230  1.1  cgd 			break;
    231  1.1  cgd 		}
    232  1.1  cgd 		if (c == keys[4]) {
    233  1.1  cgd 			static char msg[] =
    234  1.1  cgd 			    "paused - press RETURN to continue";
    235  1.1  cgd 
    236  1.1  cgd 			place(curshape, pos, 1);
    237  1.1  cgd 			do {
    238  1.1  cgd 				scr_update();
    239  1.1  cgd 				scr_msg(key_msg, 0);
    240  1.1  cgd 				scr_msg(msg, 1);
    241  1.1  cgd 				(void) fflush(stdout);
    242  1.1  cgd 			} while (rwait((struct timeval *)NULL) == -1);
    243  1.1  cgd 			scr_msg(msg, 0);
    244  1.1  cgd 			scr_msg(key_msg, 1);
    245  1.1  cgd 			place(curshape, pos, 0);
    246  1.1  cgd 			continue;
    247  1.1  cgd 		}
    248  1.1  cgd 		if (c == keys[0]) {
    249  1.1  cgd 			/* move left */
    250  1.1  cgd 			if (fits_in(curshape, pos - 1))
    251  1.1  cgd 				pos--;
    252  1.1  cgd 			continue;
    253  1.1  cgd 		}
    254  1.1  cgd 		if (c == keys[1]) {
    255  1.1  cgd 			/* turn */
    256  1.1  cgd 			struct shape *new = &shapes[curshape->rot];
    257  1.1  cgd 
    258  1.1  cgd 			if (fits_in(new, pos))
    259  1.1  cgd 				curshape = new;
    260  1.1  cgd 			continue;
    261  1.1  cgd 		}
    262  1.1  cgd 		if (c == keys[2]) {
    263  1.1  cgd 			/* move right */
    264  1.1  cgd 			if (fits_in(curshape, pos + 1))
    265  1.1  cgd 				pos++;
    266  1.1  cgd 			continue;
    267  1.1  cgd 		}
    268  1.1  cgd 		if (c == keys[3]) {
    269  1.1  cgd 			/* move to bottom */
    270  1.1  cgd 			while (fits_in(curshape, pos + B_COLS)) {
    271  1.1  cgd 				pos += B_COLS;
    272  1.1  cgd 				score++;
    273  1.1  cgd 			}
    274  1.1  cgd 			continue;
    275  1.1  cgd 		}
    276  1.1  cgd 		if (c == '\f')
    277  1.1  cgd 			scr_clear();
    278  1.1  cgd 	}
    279  1.1  cgd 
    280  1.1  cgd 	scr_clear();
    281  1.1  cgd 	scr_end();
    282  1.1  cgd 
    283  1.1  cgd 	(void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
    284  1.1  cgd 	    score, score == 1 ? "" : "s", level, score * level);
    285  1.1  cgd 	savescore(level);
    286  1.1  cgd 
    287  1.1  cgd 	printf("\nHit RETURN to see high scores, ^C to skip.\n");
    288  1.1  cgd 
    289  1.1  cgd 	while ((i = getchar()) != '\n')
    290  1.1  cgd 		if (i == EOF)
    291  1.1  cgd 			break;
    292  1.1  cgd 
    293  1.1  cgd 	showscores(level);
    294  1.1  cgd 
    295  1.1  cgd 	exit(0);
    296  1.1  cgd }
    297  1.1  cgd 
    298  1.1  cgd void
    299  1.1  cgd onintr(signo)
    300  1.1  cgd 	int signo;
    301  1.1  cgd {
    302  1.1  cgd 	scr_clear();
    303  1.1  cgd 	scr_end();
    304  1.1  cgd 	exit(0);
    305  1.1  cgd }
    306  1.1  cgd 
    307  1.1  cgd void
    308  1.1  cgd usage()
    309  1.1  cgd {
    310  1.1  cgd 	(void)fprintf(stderr, "usage: tetris [-s] [-l level] [-keys]\n");
    311  1.1  cgd 	exit(1);
    312  1.1  cgd }
    313