157276Sbostic /*-
2*60854Sbostic * Copyright (c) 1992, 1993
3*60854Sbostic * The Regents of the University of California. All rights reserved.
457276Sbostic *
557288Sbostic * This code is derived from software contributed to Berkeley by
657288Sbostic * Chris Torek and Darren F. Provine.
757288Sbostic *
857276Sbostic * %sccs.include.redist.c%
957276Sbostic *
10*60854Sbostic * @(#)tetris.c 8.1 (Berkeley) 05/31/93
1157276Sbostic */
1257276Sbostic
1357276Sbostic #ifndef lint
14*60854Sbostic static char copyright[] =
15*60854Sbostic "@(#) Copyright (c) 1992, 1993\n\
16*60854Sbostic The Regents of the University of California. All rights reserved.\n";
1757276Sbostic #endif /* not lint */
1857276Sbostic
1957276Sbostic /*
2057276Sbostic * Tetris (or however it is spelled).
2157276Sbostic */
2257276Sbostic
2357276Sbostic #include <sys/time.h>
2457276Sbostic
2557289Sbostic #include <signal.h>
2657276Sbostic #include <stdio.h>
2757276Sbostic #include <stdlib.h>
2857276Sbostic #include <string.h>
2957276Sbostic #include <unistd.h>
3057276Sbostic
3157276Sbostic #include "input.h"
3257276Sbostic #include "scores.h"
3357276Sbostic #include "screen.h"
3457276Sbostic #include "tetris.h"
3557276Sbostic
3657289Sbostic void onintr __P((int));
3757289Sbostic void usage __P((void));
3857289Sbostic
3957276Sbostic /*
4057289Sbostic * Set up the initial board. The bottom display row is completely set,
4157289Sbostic * along with another (hidden) row underneath that. Also, the left and
4257289Sbostic * right edges are set.
4357276Sbostic */
4457276Sbostic static void
setup_board()4557276Sbostic setup_board()
4657276Sbostic {
4757276Sbostic register int i;
4857276Sbostic register cell *p;
4957276Sbostic
5057276Sbostic p = board;
5157276Sbostic for (i = B_SIZE; i; i--)
5257276Sbostic #ifndef mips
5357276Sbostic *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
5457276Sbostic #else /* work around compiler bug */
5557276Sbostic *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2 ? 1 : 0;
5657276Sbostic #endif
5757276Sbostic }
5857276Sbostic
5957276Sbostic /*
6057276Sbostic * Elide any full active rows.
6157276Sbostic */
6257276Sbostic static void
elide()6357276Sbostic elide()
6457276Sbostic {
6557276Sbostic register int i, j, base;
6657276Sbostic register cell *p;
6757276Sbostic
6857276Sbostic for (i = A_FIRST; i < A_LAST; i++) {
6957276Sbostic base = i * B_COLS + 1;
7057276Sbostic p = &board[base];
7157276Sbostic for (j = B_COLS - 2; *p++ != 0;) {
7257276Sbostic if (--j <= 0) {
7357276Sbostic /* this row is to be elided */
7457276Sbostic bzero(&board[base], B_COLS - 2);
7557276Sbostic scr_update();
7657276Sbostic tsleep();
7757276Sbostic while (--base != 0)
7857276Sbostic board[base + B_COLS] = board[base];
7957276Sbostic scr_update();
8057276Sbostic tsleep();
8157276Sbostic break;
8257276Sbostic }
8357276Sbostic }
8457276Sbostic }
8557276Sbostic }
8657276Sbostic
8757276Sbostic int
main(argc,argv)8857276Sbostic main(argc, argv)
8957276Sbostic int argc;
9057289Sbostic char *argv[];
9157276Sbostic {
9257276Sbostic register int pos, c;
9357276Sbostic register struct shape *curshape;
9457276Sbostic register char *keys;
9557276Sbostic register int level = 2;
9657276Sbostic char key_write[6][10];
9757289Sbostic int ch, i, j;
9857276Sbostic
9957276Sbostic keys = "jkl pq";
10057276Sbostic
10157289Sbostic while ((ch = getopt(argc, argv, "k:l:s")) != EOF)
10257289Sbostic switch(ch) {
10357289Sbostic case 'k':
10457289Sbostic if (strlen(keys = optarg) != 6)
10557289Sbostic usage();
10657289Sbostic break;
10757289Sbostic case 'l':
10857289Sbostic level = atoi(optarg);
10957289Sbostic if (level < MINLEVEL || level > MAXLEVEL) {
11057276Sbostic (void)fprintf(stderr,
11157289Sbostic "tetris: level must be from %d to %d",
11257289Sbostic MINLEVEL, MAXLEVEL);
11357276Sbostic exit(1);
11457276Sbostic }
11557276Sbostic break;
11657289Sbostic case 's':
11757289Sbostic showscores(0);
11857289Sbostic exit(0);
11957289Sbostic case '?':
12057276Sbostic default:
12157289Sbostic usage();
12257276Sbostic }
12357276Sbostic
12457289Sbostic argc -= optind;
12557289Sbostic argv += optind;
12657289Sbostic
12757289Sbostic if (argc)
12857289Sbostic usage();
12957289Sbostic
13057276Sbostic fallrate = 1000000 / level;
13157276Sbostic
13257276Sbostic for (i = 0; i <= 5; i++) {
13357276Sbostic for (j = i+1; j <= 5; j++) {
13457276Sbostic if (keys[i] == keys[j]) {
13557276Sbostic (void)fprintf(stderr,
13657276Sbostic "%s: Duplicate command keys specified.\n",
13757276Sbostic argv[0]);
13857276Sbostic exit (1);
13957276Sbostic }
14057276Sbostic }
14157276Sbostic if (keys[i] == ' ')
14257276Sbostic strcpy(key_write[i], "<space>");
14357276Sbostic else {
14457276Sbostic key_write[i][0] = keys[i];
14557276Sbostic key_write[i][1] = '\0';
14657276Sbostic }
14757276Sbostic }
14857276Sbostic
14957276Sbostic sprintf(key_msg,
15057276Sbostic "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
15157276Sbostic key_write[0], key_write[1], key_write[2], key_write[3],
15257276Sbostic key_write[4], key_write[5]);
15357276Sbostic
15457289Sbostic (void)signal(SIGINT, onintr);
15557276Sbostic scr_init();
15657276Sbostic setup_board();
15757276Sbostic
15857276Sbostic srandom(getpid());
15957276Sbostic scr_set();
16057276Sbostic
16157276Sbostic pos = A_FIRST*B_COLS + (B_COLS/2)-1;
16257276Sbostic curshape = randshape();
16357276Sbostic
16457276Sbostic scr_msg(key_msg, 1);
16557276Sbostic
16657276Sbostic for (;;) {
16757276Sbostic place(curshape, pos, 1);
16857276Sbostic scr_update();
16957276Sbostic place(curshape, pos, 0);
17057276Sbostic c = tgetchar();
17157276Sbostic if (c < 0) {
17257276Sbostic /*
17357276Sbostic * Timeout. Move down if possible.
17457276Sbostic */
17557276Sbostic if (fits_in(curshape, pos + B_COLS)) {
17657276Sbostic pos += B_COLS;
17757276Sbostic continue;
17857276Sbostic }
17957276Sbostic
18057276Sbostic /*
18157276Sbostic * Put up the current shape `permanently',
18257276Sbostic * bump score, and elide any full rows.
18357276Sbostic */
18457276Sbostic place(curshape, pos, 1);
18557276Sbostic score++;
18657276Sbostic elide();
18757276Sbostic
18857276Sbostic /*
18957276Sbostic * Choose a new shape. If it does not fit,
19057276Sbostic * the game is over.
19157276Sbostic */
19257276Sbostic curshape = randshape();
19357276Sbostic pos = A_FIRST*B_COLS + (B_COLS/2)-1;
19457276Sbostic if (!fits_in(curshape, pos))
19557276Sbostic break;
19657276Sbostic continue;
19757276Sbostic }
19857276Sbostic
19957276Sbostic /*
20057276Sbostic * Handle command keys.
20157276Sbostic */
20257276Sbostic if (c == keys[5]) {
20357276Sbostic /* quit */
20457276Sbostic break;
20557276Sbostic }
20657276Sbostic if (c == keys[4]) {
20757276Sbostic static char msg[] =
20857276Sbostic "paused - press RETURN to continue";
20957276Sbostic
21057276Sbostic place(curshape, pos, 1);
21157276Sbostic do {
21257276Sbostic scr_update();
21357276Sbostic scr_msg(key_msg, 0);
21457276Sbostic scr_msg(msg, 1);
21557276Sbostic (void) fflush(stdout);
21657276Sbostic } while (rwait((struct timeval *)NULL) == -1);
21757276Sbostic scr_msg(msg, 0);
21857276Sbostic scr_msg(key_msg, 1);
21957276Sbostic place(curshape, pos, 0);
22057276Sbostic continue;
22157276Sbostic }
22257276Sbostic if (c == keys[0]) {
22357276Sbostic /* move left */
22457276Sbostic if (fits_in(curshape, pos - 1))
22557276Sbostic pos--;
22657276Sbostic continue;
22757276Sbostic }
22857276Sbostic if (c == keys[1]) {
22957276Sbostic /* turn */
23057276Sbostic struct shape *new = &shapes[curshape->rot];
23157276Sbostic
23257276Sbostic if (fits_in(new, pos))
23357276Sbostic curshape = new;
23457276Sbostic continue;
23557276Sbostic }
23657276Sbostic if (c == keys[2]) {
23757276Sbostic /* move right */
23857276Sbostic if (fits_in(curshape, pos + 1))
23957276Sbostic pos++;
24057276Sbostic continue;
24157276Sbostic }
24257276Sbostic if (c == keys[3]) {
24357276Sbostic /* move to bottom */
24457276Sbostic while (fits_in(curshape, pos + B_COLS)) {
24557276Sbostic pos += B_COLS;
24657276Sbostic score++;
24757276Sbostic }
24857276Sbostic continue;
24957276Sbostic }
25057276Sbostic if (c == '\f')
25157276Sbostic scr_clear();
25257276Sbostic }
25357276Sbostic
25457276Sbostic scr_clear();
25557276Sbostic scr_end();
25657276Sbostic
25757276Sbostic (void)printf("Your score: %d point%s x level %d = %d\n",
25857276Sbostic score, score == 1 ? "" : "s", level, score * level);
25957276Sbostic savescore(level);
26057276Sbostic
26157276Sbostic printf("\nHit RETURN to see high scores, ^C to skip.\n");
26257276Sbostic
26357276Sbostic while ((i = getchar()) != '\n')
26457276Sbostic if (i == EOF)
26557276Sbostic break;
26657276Sbostic
26757276Sbostic showscores(level);
26857276Sbostic
26957276Sbostic exit(0);
27057276Sbostic }
27157289Sbostic
27257289Sbostic void
onintr(signo)27357289Sbostic onintr(signo)
27457289Sbostic int signo;
27557289Sbostic {
27657289Sbostic scr_clear();
27757289Sbostic scr_end();
27857289Sbostic exit(0);
27957289Sbostic }
28057289Sbostic
28157289Sbostic void
usage()28257289Sbostic usage()
28357289Sbostic {
28457289Sbostic (void)fprintf(stderr, "usage: tetris [-s] [-l level] [-keys]\n");
28557289Sbostic exit(1);
28657289Sbostic }
287