-/* $NetBSD: tetris.c,v 1.16 2003/08/07 09:37:48 agc Exp $ */
+/* $NetBSD: tetris.c,v 1.33 2020/07/21 02:42:05 nia Exp $ */
/*-
* Copyright (c) 1992, 1993
#include <sys/cdefs.h>
#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 */
/*
cell board[B_SIZE]; /* 1 => occupied, 0 => empty */
int Rows, Cols; /* current screen size */
+int Offset; /* used to center board & shapes */
-const struct shape *curshape;
+static const struct shape *curshape;
const struct shape *nextshape;
long fallrate; /* less than 1 million; smaller => faster */
char key_msg[100];
int showpreview;
+int nocolor;
-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__));
+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,
* right edges are set.
*/
static void
-setup_board()
+setup_board(void)
{
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)
{
int i, j, base;
cell *p;
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;
}
int
-main(argc, argv)
- int argc;
- char *argv[];
+main(int argc, char *argv[])
{
int pos, c;
const char *keys;
int level = 2;
- char key_write[6][10];
+#define NUMKEYS 7
+ char key_write[NUMKEYS][10];
int ch, i, j;
int fd;
exit(1);
close(fd);
- keys = "jkl pq";
+ keys = "jkl pqn";
- while ((ch = getopt(argc, argv, "k:l:ps")) != -1)
+ 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':
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]) {
errx(1, "duplicate command keys specified.");
}
}
}
- 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;
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);
}
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);
exit(0);
}
-void
-onintr(signo)
- int signo __attribute__((__unused__));
+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);
}