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