1 /* $OpenBSD: tetris.c,v 1.32 2017/08/13 02:12:16 tedu Exp $ */
2 /* $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $ */
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Chris Torek and Darren F. Provine.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)tetris.c 8.1 (Berkeley) 5/31/93
39 * Tetris (or however it is spelled).
57 const struct shape
*curshape
;
58 const struct shape
*nextshape
;
62 int showpreview
, classic
;
64 static void elide(void);
65 __dead2
void onintr(int __unused
);
66 const struct shape
*randshape(void);
67 static void setup_board(void);
68 __dead2
void usage(void);
71 * Set up the initial board. The bottom display row is completely set,
72 * along with another (hidden) row underneath that. Also, the left and
73 * right edges are set.
82 for (i
= B_SIZE
; i
; i
--)
83 *p
++ = i
<= (2 * B_COLS
) || (i
% B_COLS
) < 2;
87 * Elide any full active rows.
96 for (i
= A_FIRST
; i
< A_LAST
; i
++) {
97 base
= i
* B_COLS
+ 1;
99 for (j
= B_COLS
- 2; *p
++ != 0;) {
101 /* this row is to be elided */
103 memset(&board
[base
], 0, B_COLS
- 2);
107 board
[base
+ B_COLS
] = board
[base
];
108 memset(&board
[1], 0, B_COLS
- 2);
136 const struct shape
*tmp
;
139 tmp
= &shapes
[arc4random_uniform(7)];
140 j
= arc4random_uniform(4);
141 for (i
= 0; i
< j
; i
++)
142 tmp
= &shapes
[classic
? tmp
->rotc
: tmp
->rot
];
149 main(int argc
, char *argv
[])
154 char key_write
[NUMKEYS
][10];
160 classic
= showpreview
= 0;
161 while ((ch
= getopt(argc
, argv
, "ck:l:ps")) != -1)
166 * - rotate the other way;
167 * - no reverse video.
172 if (strlen(keys
= optarg
) != NUMKEYS
)
176 level
= (int)strtonum(optarg
, MINLEVEL
, MAXLEVEL
,
179 errx(1, "level must be from %d to %d",
198 fallrate
= 1000000000L / level
;
200 for (i
= 0; i
<= (NUMKEYS
-1); i
++) {
201 for (j
= i
+1; j
<= (NUMKEYS
-1); j
++) {
202 if (keys
[i
] == keys
[j
])
203 errx(1, "duplicate command keys specified.");
206 strlcpy(key_write
[i
], "<space>", sizeof key_write
[i
]);
208 key_write
[i
][0] = keys
[i
];
209 key_write
[i
][1] = '\0';
213 snprintf(key_msg
, sizeof(key_msg
),
214 "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
215 key_write
[0], key_write
[1], key_write
[2], key_write
[3],
216 key_write
[4], key_write
[5]);
218 signal(SIGINT
, onintr
);
224 pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
225 nextshape
= randshape();
226 curshape
= randshape();
231 place(curshape
, pos
, 1);
233 place(curshape
, pos
, 0);
237 * Timeout. Move down if possible.
239 if (fits_in(curshape
, pos
+ B_COLS
)) {
245 * Put up the current shape `permanently',
246 * bump score, and elide any full rows.
248 place(curshape
, pos
, 1);
253 * Choose a new shape. If it does not fit,
256 curshape
= nextshape
;
257 nextshape
= randshape();
258 pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
259 if (!fits_in(curshape
, pos
))
265 * Handle command keys.
273 "paused - press RETURN to continue";
275 place(curshape
, pos
, 1);
281 } while (rwait(NULL
) == -1);
284 place(curshape
, pos
, 0);
289 if (fits_in(curshape
, pos
- 1))
295 const struct shape
*new = &shapes
[
296 classic
? curshape
->rotc
: curshape
->rot
];
298 if (fits_in(new, pos
))
304 if (fits_in(curshape
, pos
+ 1))
310 while (fits_in(curshape
, pos
+ B_COLS
)) {
325 if (showpreview
== 0)
326 printf("Your score: %d point%s x level %d = %d\n",
327 score
, score
== 1 ? "" : "s", level
, score
* level
);
329 printf("Your score: %d point%s x level %d x preview penalty %0.3f = %d\n",
330 score
, score
== 1 ? "" : "s", level
, (double)PRE_PENALTY
,
331 (int)(score
* level
* PRE_PENALTY
));
332 score
= score
* PRE_PENALTY
;
336 printf("\nHit RETURN to see high scores, ^C to skip.\n");
338 while ((i
= getchar()) != '\n')
348 onintr(int signo __unused
)
350 scr_clear(); /* XXX signal race */
351 scr_end(); /* XXX signal race */
358 fprintf(stderr
, "usage: %s [-cps] [-k keys] "
359 "[-l level]\n", getprogname());