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