]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - tetris/tetris.c
#netbsd - where monks recite their internal dialogue while debugging.
[bsdgames-darwin.git] / tetris / tetris.c
index 63265bc6b7541c435dca4f326ffbd1fd64edcdec..058335e6d994caf8c19448fd6cb3ce81be778280 100644 (file)
@@ -1,3 +1,5 @@
+/*     $NetBSD: tetris.c,v 1.14 2000/01/21 02:10:57 jsm Exp $  */
+
 /*-
  * Copyright (c) 1992, 1993
  *     The Regents of the University of California.  All rights reserved.
 /*-
  * Copyright (c) 1992, 1993
  *     The Regents of the University of California.  All rights reserved.
  *     @(#)tetris.c    8.1 (Berkeley) 5/31/93
  */
 
  *     @(#)tetris.c    8.1 (Berkeley) 5/31/93
  */
 
+#include <sys/cdefs.h>
 #ifndef lint
 #ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1992, 1993\n\
-       The Regents of the University of California.  All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
+       The Regents of the University of California.  All rights reserved.\n");
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -48,6 +50,8 @@ static char copyright[] =
 
 #include <sys/time.h>
 
 
 #include <sys/time.h>
 
+#include <err.h>
+#include <fcntl.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -59,8 +63,26 @@ static char copyright[] =
 #include "screen.h"
 #include "tetris.h"
 
 #include "screen.h"
 #include "tetris.h"
 
-void onintr __P((int));
-void usage __P((void));
+cell   board[B_SIZE];          /* 1 => occupied, 0 => empty */
+
+int    Rows, Cols;             /* current screen size */
+
+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;
+
+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__));
 
 /*
  * Set up the initial board.  The bottom display row is completely set,
 
 /*
  * Set up the initial board.  The bottom display row is completely set,
@@ -75,11 +97,7 @@ setup_board()
 
        p = board;
        for (i = B_SIZE; i; i--)
 
        p = board;
        for (i = B_SIZE; i; i--)
-#ifndef mips
                *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
                *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
-#else /* work around compiler bug */
-               *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2 ? 1 : 0;
-#endif
 }
 
 /*
 }
 
 /*
@@ -97,7 +115,7 @@ elide()
                for (j = B_COLS - 2; *p++ != 0;) {
                        if (--j <= 0) {
                                /* this row is to be elided */
                for (j = B_COLS - 2; *p++ != 0;) {
                        if (--j <= 0) {
                                /* this row is to be elided */
-                               bzero(&board[base], B_COLS - 2);
+                               memset(&board[base], 0, B_COLS - 2);
                                scr_update();
                                tsleep();
                                while (--base != 0)
                                scr_update();
                                tsleep();
                                while (--base != 0)
@@ -116,15 +134,24 @@ main(argc, argv)
        char *argv[];
 {
        register int pos, c;
        char *argv[];
 {
        register int pos, c;
-       register struct shape *curshape;
-       register char *keys;
+       register const char *keys;
        register int level = 2;
        char key_write[6][10];
        int ch, i, j;
        register int level = 2;
        char key_write[6][10];
        int ch, i, j;
+       int fd;
+
+       gid = getgid();
+       egid = getegid();
+       setegid(gid);
+
+       fd = open("/dev/null", O_RDONLY);
+       if (fd < 3)
+               exit(1);
+       close(fd);
 
        keys = "jkl pq";
 
 
        keys = "jkl pq";
 
-       while ((ch = getopt(argc, argv, "k:l:s")) != EOF)
+       while ((ch = getopt(argc, argv, "k:l:ps")) != -1)
                switch(ch) {
                case 'k':
                        if (strlen(keys = optarg) != 6)
                switch(ch) {
                case 'k':
                        if (strlen(keys = optarg) != 6)
@@ -133,12 +160,13 @@ main(argc, argv)
                case 'l':
                        level = atoi(optarg);
                        if (level < MINLEVEL || level > MAXLEVEL) {
                case 'l':
                        level = atoi(optarg);
                        if (level < MINLEVEL || level > MAXLEVEL) {
-                               (void)fprintf(stderr,
-                                   "tetris: level must be from %d to %d",
-                                   MINLEVEL, MAXLEVEL);
-                               exit(1);
+                               errx(1, "level must be from %d to %d",
+                                    MINLEVEL, MAXLEVEL);
                        }
                        break;
                        }
                        break;
+               case 'p':
+                       showpreview = 1;
+                       break;
                case 's':
                        showscores(0);
                        exit(0);
                case 's':
                        showscores(0);
                        exit(0);
@@ -158,10 +186,7 @@ main(argc, argv)
        for (i = 0; i <= 5; i++) {
                for (j = i+1; j <= 5; j++) {
                        if (keys[i] == keys[j]) {
        for (i = 0; i <= 5; i++) {
                for (j = i+1; j <= 5; 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] == ' ')
                        }
                }
                if (keys[i] == ' ')
@@ -185,6 +210,7 @@ main(argc, argv)
        scr_set();
 
        pos = A_FIRST*B_COLS + (B_COLS/2)-1;
        scr_set();
 
        pos = A_FIRST*B_COLS + (B_COLS/2)-1;
+       nextshape = randshape();
        curshape = randshape();
 
        scr_msg(key_msg, 1);
        curshape = randshape();
 
        scr_msg(key_msg, 1);
@@ -215,7 +241,8 @@ main(argc, argv)
                         * Choose a new shape.  If it does not fit,
                         * the game is over.
                         */
                         * Choose a new shape.  If it does not fit,
                         * the game is over.
                         */
-                       curshape = randshape();
+                       curshape = nextshape;
+                       nextshape = randshape();
                        pos = A_FIRST*B_COLS + (B_COLS/2)-1;
                        if (!fits_in(curshape, pos))
                                break;
                        pos = A_FIRST*B_COLS + (B_COLS/2)-1;
                        if (!fits_in(curshape, pos))
                                break;
@@ -253,7 +280,7 @@ main(argc, argv)
                }
                if (c == keys[1]) {
                        /* turn */
                }
                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;
 
                        if (fits_in(new, pos))
                                curshape = new;
@@ -273,8 +300,10 @@ main(argc, argv)
                        }
                        continue;
                }
                        }
                        continue;
                }
-               if (c == '\f')
+               if (c == '\f') {
                        scr_clear();
                        scr_clear();
+                       scr_msg(key_msg, 1);
+               }
        }
 
        scr_clear();
        }
 
        scr_clear();
@@ -297,7 +326,7 @@ main(argc, argv)
 
 void
 onintr(signo)
 
 void
 onintr(signo)
-       int signo;
+       int signo __attribute__((__unused__));
 {
        scr_clear();
        scr_end();
 {
        scr_clear();
        scr_end();
@@ -307,6 +336,6 @@ onintr(signo)
 void
 usage()
 {
 void
 usage()
 {
-       (void)fprintf(stderr, "usage: tetris [-s] [-l level] [-keys]\n");
+       (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n");
        exit(1);
 }
        exit(1);
 }