-/* $NetBSD: tetris.c,v 1.21 2009/05/25 04:33:53 dholland Exp $ */
+/* $NetBSD: tetris.c,v 1.33 2020/07/21 02:42:05 nia Exp $ */
/*-
* Copyright (c) 1992, 1993
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(void);
-static void setup_board(void);
- int main(int, char **);
- void onintr(int) __dead;
- void usage(void) __dead;
+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,
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;
}
/*
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 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.");
}
}
snprintf(key_msg, sizeof(key_msg),
-"%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
+"%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
+static void
onintr(int signo __unused)
{
scr_clear();
exit(0);
}
-void
+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);
}