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