* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Chris Torek and Darren F. Provine.
* %sccs.include.redist.c%
* @(#)tetris.c 8.1 (Berkeley) %G%
static char copyright
[] =
"@(#) Copyright (c) 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n";
* Tetris (or however it is spelled).
* Set up the initial board. The bottom display row is completely set,
* along with another (hidden) row underneath that. Also, the left and
*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;
* Elide any full active rows.
for (i
= A_FIRST
; i
< A_LAST
; i
++) {
for (j
= B_COLS
- 2; *p
++ != 0;) {
/* this row is to be elided */
bzero(&board
[base
], B_COLS
- 2);
board
[base
+ B_COLS
] = board
[base
];
register struct shape
*curshape
;
while ((ch
= getopt(argc
, argv
, "k:l:s")) != EOF
)
if (strlen(keys
= optarg
) != 6)
if (level
< MINLEVEL
|| level
> MAXLEVEL
) {
"tetris: level must be from %d to %d",
fallrate
= 1000000 / level
;
for (i
= 0; i
<= 5; i
++) {
for (j
= i
+1; j
<= 5; j
++) {
if (keys
[i
] == keys
[j
]) {
"%s: Duplicate command keys specified.\n",
strcpy(key_write
[i
], "<space>");
key_write
[i
][0] = keys
[i
];
"%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
key_write
[0], key_write
[1], key_write
[2], key_write
[3],
key_write
[4], key_write
[5]);
(void)signal(SIGINT
, onintr
);
pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
* Timeout. Move down if possible.
if (fits_in(curshape
, pos
+ B_COLS
)) {
* Put up the current shape `permanently',
* bump score, and elide any full rows.
* Choose a new shape. If it does not fit,
pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
if (!fits_in(curshape
, pos
))
"paused - press RETURN to continue";
} while (rwait((struct timeval
*)NULL
) == -1);
if (fits_in(curshape
, pos
- 1))
struct shape
*new = &shapes
[curshape
->rot
];
if (fits_in(curshape
, pos
+ 1))
while (fits_in(curshape
, pos
+ B_COLS
)) {
(void)printf("Your score: %d point%s x level %d = %d\n",
score
, score
== 1 ? "" : "s", level
, score
* level
);
printf("\nHit RETURN to see high scores, ^C to skip.\n");
while ((i
= getchar()) != '\n')
(void)fprintf(stderr
, "usage: tetris [-s] [-l level] [-keys]\n");