X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/ec692e1a8ed1405fbc87924553af75d00ca4b851..da6a856c31649c6fc011d45d51542876e1b13539:/tetris/tetris.c diff --git a/tetris/tetris.c b/tetris/tetris.c index 82517b2c..286e7c45 100644 --- a/tetris/tetris.c +++ b/tetris/tetris.c @@ -1,4 +1,4 @@ -/* $NetBSD: tetris.c,v 1.9 1999/03/22 06:12:23 abs Exp $ */ +/* $NetBSD: tetris.c,v 1.33 2020/07/21 02:42:05 nia Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -15,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -40,8 +36,8 @@ #include #ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\ - The Regents of the University of California. All rights reserved.\n"); +__COPYRIGHT("@(#) Copyright (c) 1992, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ /* @@ -50,6 +46,8 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\ #include +#include +#include #include #include #include @@ -61,11 +59,27 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\ #include "screen.h" #include "tetris.h" -static void elide __P((void)); -static void setup_board __P((void)); - int main __P((int, char **)); - void onintr __P((int)) __attribute__((__noreturn__)); - void usage __P((void)) __attribute__((__noreturn__)); +cell board[B_SIZE]; /* 1 => occupied, 0 => empty */ + +int Rows, Cols; /* current screen size */ +int Offset; /* used to center board & shapes */ + +static const struct shape *curshape; +const struct shape *nextshape; + +long fallrate; /* less than 1 million; smaller => faster */ + +int score; /* the obvious thing */ +gid_t gid, egid; + +char key_msg[100]; +int showpreview; +int nocolor; + +static void elide(void); +static void setup_board(void); +static void onintr(int) __dead; +static void usage(void) __dead; /* * Set up the initial board. The bottom display row is completely set, @@ -73,24 +87,24 @@ static void setup_board __P((void)); * right edges are set. */ static void -setup_board() +setup_board(void) { - register int i; - register cell *p; + int i; + cell *p; p = board; for (i = B_SIZE; i; i--) - *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2; + *p++ = (i <= (2 * B_COLS) || (i % B_COLS) < 2) ? 7 : 0; } /* * Elide any full active rows. */ static void -elide() +elide(void) { - register int i, j, base; - register cell *p; + int i, j, base; + cell *p; for (i = A_FIRST; i < A_LAST; i++) { base = i * B_COLS + 1; @@ -103,6 +117,8 @@ elide() tsleep(); while (--base != 0) board[base + B_COLS] = board[base]; + /* don't forget to clear 0th row */ + memset(&board[1], 0, B_COLS - 2); scr_update(); tsleep(); break; @@ -112,31 +128,41 @@ elide() } int -main(argc, argv) - int argc; - char *argv[]; +main(int argc, char *argv[]) { - register int pos, c; - register char *keys; - register int level = 2; - char key_write[6][10]; + int pos, c; + const char *keys; + int level = 2; +#define NUMKEYS 7 + char key_write[NUMKEYS][10]; int ch, i, j; + int fd; + + gid = getgid(); + egid = getegid(); + setegid(gid); - keys = "jkl pq"; + fd = open("/dev/null", O_RDONLY); + if (fd < 3) + exit(1); + close(fd); - while ((ch = getopt(argc, argv, "k:l:ps")) != -1) + keys = "jkl pqn"; + + while ((ch = getopt(argc, argv, "bk:l:ps")) != -1) switch(ch) { + case 'b': + nocolor = 1; + break; case 'k': - if (strlen(keys = optarg) != 6) + if (strlen(keys = optarg) != NUMKEYS) usage(); break; case 'l': level = atoi(optarg); if (level < MINLEVEL || level > MAXLEVEL) { - (void)fprintf(stderr, - "tetris: level must be from %d to %d\n", - MINLEVEL, MAXLEVEL); - exit(1); + errx(1, "level must be from %d to %d", + MINLEVEL, MAXLEVEL); } break; case 'p': @@ -158,13 +184,10 @@ main(argc, argv) fallrate = 1000000 / level; - for (i = 0; i <= 5; i++) { - for (j = i+1; j <= 5; j++) { + for (i = 0; i <= (NUMKEYS-1); i++) { + for (j = i+1; j <= (NUMKEYS-1); j++) { if (keys[i] == keys[j]) { - (void)fprintf(stderr, - "%s: duplicate command keys specified.\n", - argv[0]); - exit (1); + errx(1, "duplicate command keys specified."); } } if (keys[i] == ' ') @@ -175,16 +198,15 @@ main(argc, argv) } } - sprintf(key_msg, -"%s - left %s - rotate %s - right %s - drop %s - pause %s - quit", + snprintf(key_msg, sizeof(key_msg), +"%s - left %s - rotate %s - right %s - drop %s - pause %s - quit %s - down", key_write[0], key_write[1], key_write[2], key_write[3], - key_write[4], key_write[5]); + key_write[4], key_write[5], key_write[6]); (void)signal(SIGINT, onintr); scr_init(); setup_board(); - srandom(getpid()); scr_set(); pos = A_FIRST*B_COLS + (B_COLS/2)-1; @@ -244,7 +266,7 @@ main(argc, argv) scr_msg(key_msg, 0); scr_msg(msg, 1); (void) fflush(stdout); - } while (rwait((struct timeval *)NULL) == -1); + } while (rwait(NULL) == -1); scr_msg(msg, 0); scr_msg(key_msg, 1); place(curshape, pos, 0); @@ -258,7 +280,7 @@ main(argc, argv) } if (c == keys[1]) { /* turn */ - struct shape *new = &shapes[curshape->rot]; + const struct shape *new = &shapes[curshape->rot]; if (fits_in(new, pos)) curshape = new; @@ -278,6 +300,14 @@ main(argc, argv) } continue; } + if (c == keys[6]) { + /* move down */ + if (fits_in(curshape, pos + B_COLS)) { + pos += B_COLS; + score++; + } + continue; + } if (c == '\f') { scr_clear(); scr_msg(key_msg, 1); @@ -302,18 +332,18 @@ main(argc, argv) exit(0); } -void -onintr(signo) - int signo; +static void +onintr(int signo __unused) { scr_clear(); scr_end(); exit(0); } -void -usage() +static void +usage(void) { - (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n"); + (void)fprintf(stderr, "usage: %s [-bps] [-k keys] [-l level]\n", + getprogname()); exit(1); }