]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/tetris.c
1 /* $NetBSD: tetris.c,v 1.5 1998/09/13 15:27:30 hubertf Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)tetris.c 8.1 (Berkeley) 5/31/93
41 #include <sys/cdefs.h>
43 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
44 The Regents of the University of California. All rights reserved.\n");
48 * Tetris (or however it is spelled).
64 static void elide
__P((void));
65 static void setup_board
__P((void));
66 int main
__P((int, char **));
67 void onintr
__P((int)) __attribute__((__noreturn__
));
68 void usage
__P((void)) __attribute__((__noreturn__
));
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
--)
84 *p
++ = i
<= (2 * B_COLS
) || (i
% B_COLS
) < 2;
85 #else /* work around compiler bug */
86 *p
++ = i
<= (2 * B_COLS
) || (i
% B_COLS
) < 2 ? 1 : 0;
91 * Elide any full active rows.
96 register int i
, j
, base
;
99 for (i
= A_FIRST
; i
< A_LAST
; i
++) {
100 base
= i
* B_COLS
+ 1;
102 for (j
= B_COLS
- 2; *p
++ != 0;) {
104 /* this row is to be elided */
105 memset(&board
[base
], 0, B_COLS
- 2);
109 board
[base
+ B_COLS
] = board
[base
];
124 register struct shape
*curshape
;
126 register int level
= 2;
127 char key_write
[6][10];
132 while ((ch
= getopt(argc
, argv
, "k:l:s")) != -1)
135 if (strlen(keys
= optarg
) != 6)
139 level
= atoi(optarg
);
140 if (level
< MINLEVEL
|| level
> MAXLEVEL
) {
141 (void)fprintf(stderr
,
142 "tetris: level must be from %d to %d",
161 fallrate
= 1000000 / level
;
163 for (i
= 0; i
<= 5; i
++) {
164 for (j
= i
+1; j
<= 5; j
++) {
165 if (keys
[i
] == keys
[j
]) {
166 (void)fprintf(stderr
,
167 "%s: Duplicate command keys specified.\n",
173 strcpy(key_write
[i
], "<space>");
175 key_write
[i
][0] = keys
[i
];
176 key_write
[i
][1] = '\0';
181 "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
182 key_write
[0], key_write
[1], key_write
[2], key_write
[3],
183 key_write
[4], key_write
[5]);
185 (void)signal(SIGINT
, onintr
);
192 pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
193 curshape
= randshape();
198 place(curshape
, pos
, 1);
200 place(curshape
, pos
, 0);
204 * Timeout. Move down if possible.
206 if (fits_in(curshape
, pos
+ B_COLS
)) {
212 * Put up the current shape `permanently',
213 * bump score, and elide any full rows.
215 place(curshape
, pos
, 1);
220 * Choose a new shape. If it does not fit,
223 curshape
= randshape();
224 pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
225 if (!fits_in(curshape
, pos
))
231 * Handle command keys.
239 "paused - press RETURN to continue";
241 place(curshape
, pos
, 1);
246 (void) fflush(stdout
);
247 } while (rwait((struct timeval
*)NULL
) == -1);
250 place(curshape
, pos
, 0);
255 if (fits_in(curshape
, pos
- 1))
261 struct shape
*new = &shapes
[curshape
->rot
];
263 if (fits_in(new, pos
))
269 if (fits_in(curshape
, pos
+ 1))
275 while (fits_in(curshape
, pos
+ B_COLS
)) {
288 (void)printf("Your score: %d point%s x level %d = %d\n",
289 score
, score
== 1 ? "" : "s", level
, score
* level
);
292 printf("\nHit RETURN to see high scores, ^C to skip.\n");
294 while ((i
= getchar()) != '\n')
315 (void)fprintf(stderr
, "usage: tetris [-s] [-l level] [-keys]\n");